def upgrade_to_ghost(self, content): container = content.get_container() if not IGhostFolder.providedBy(container): return None if container.get_link_status() is not None: logger.warning( u"Invalid Ghost Folder, not transforming: %s.", content_path(content)) return None content_id = content.getId() container_haunted = container.get_haunted() content_haunted = container_haunted._getOb(content_id, None) if not IImage.providedBy(content_haunted): logger.warning( u"Original found for %s, but is not an image, not transforming it.", content_path(content)) return None # Prevent quota system to fail if the image is an old one. # XXX Quota will be out of sync after the upgrade. if (content.hires_image is not None and content.hires_image.meta_type != 'Silva File'): content.hires_image = None ghost = get_factory(content_haunted)( ghost=content, container=container, auto_delete=True, auto_publish=True).modify( content_haunted, content_id).verify() logger.info( u"Image converted to ghost asset: %s.", content_path(content)) return ghost
def upgrade_to_ghost(self, content): container = content.get_container() if not IGhostFolder.providedBy(container): return None if container.get_link_status() is not None: logger.warning( u"Invalid Ghost Folder, not transforming: %s.", content_path(content)) return None file_id = content.getId() container_haunted = container.get_haunted() file_haunted = container_haunted._getOb(file_id, None) if not IFile.providedBy(file_haunted): logger.warning( u"Original found for %s, but is not an file, not transforming it.", content_path(content)) return None ghost = get_factory(file_haunted)( ghost=content, container=container, auto_delete=True, auto_publish=True).modify(file_haunted, file_id).verify() logger.info( u"File converted to ghost asset: %s.", content_path(content)) return ghost
def upgrade(self, ghost): target_path = ghost._content_path if target_path: try: target = ghost.get_root().unrestrictedTraverse(target_path) except (AttributeError, KeyError, NotFound, TypeError): logger.error( u'Unexisting target for Ghost %s: %s.', content_path(ghost), "/".join(target_path)) return ghost target = aq_inner(target) if not ISilvaObject.providedBy(target): logger.error( u'Ghost target is not a Silva object for: %s.', content_path(ghost)) return ghost try: [o for o in aq_iter(target, error=RuntimeError)] except RuntimeError: logger.error( u'Invalid target for Ghost %s: %s.', content_path(ghost), '/'.join(target_path)) return ghost if target is not None and ISilvaObject.providedBy(target): logger.info( u'Upgrading Ghost target for: %s.', "/".join(ghost.getPhysicalPath())) container = aq_parent(ghost).get_container() ghost.set_haunted( target, auto_delete=IGhostFolder.providedBy(container)) del ghost._content_path return ghost
def upgrade(self, content): identifier = content.getId() container = content.aq_parent if not IContainer.providedBy(container): logger.error(u'Invalid file: %s', content_path(content)) # Self-autodestruct file. container._delObject(identifier) raise StopIteration ghost = self.upgrade_to_ghost(content) if ghost is not None: return ghost new_file = BlobFile(identifier) tmp_identifier = identifier + 'conv_storage' container._setObject(tmp_identifier, new_file) new_file = container._getOb(tmp_identifier) self.replace_references(content, new_file) self.replace(content, new_file) new_file.set_file( content.get_file_fd(), content_type=content.get_content_type(), content_encoding=content.get_content_encoding()) container._delObject(identifier) container.manage_renameObject(tmp_identifier, identifier) logger.info(u"File rebuilt: %s.", content_path(new_file)) return new_file
def upgrade(self, obj): service = getUtility(IReferenceService) fields = obj.service_find.getSearchSchema().getFields() fields = filter(lambda x: IPathCriterionField.providedBy(x), fields) root = obj.get_root() root_path = root.getPhysicalPath() for field in fields: field_name = field.getName() if obj.searchValues.has_key(field_name): value = obj.searchValues[field_name] if value: path = value.split('/') if tuple(path[:len(root_path)]) == root_path: traverse_path = path[len(root_path):] target = root.unrestrictedTraverse(traverse_path, None) if target: ref = service.new_reference( obj, name=unicode(field_name)) ref.set_target(target) logger.info('reference created for field %s of ' 'silva find at %s' % (field_name, content_path(obj))) else: logger.warn('silva find target at %s ' 'not found' % value) else: logger.warn('silva find target at %s ' 'outside of silva root' % value) del obj.searchValues[field_name] return obj
def upgrade(self, obj): service = getUtility(IReferenceService) fields = obj.service_find.getSearchSchema().getFields() fields = [x for x in fields if IPathCriterionField.providedBy(x)] root = obj.get_root() root_path = root.getPhysicalPath() for field in fields: field_name = field.getName() if field_name in obj.searchValues: value = obj.searchValues[field_name] if value: path = value.split('/') if tuple(path[:len(root_path)]) == root_path: traverse_path = path[len(root_path):] target = root.unrestrictedTraverse(traverse_path, None) if target: ref = service.new_reference(obj, name=str(field_name)) ref.set_target(target) logger.info('reference created for field %s of ' 'silva find at %s' % (field_name, content_path(obj))) else: logger.warn('silva find target at %s ' 'not found' % value) else: logger.warn('silva find target at %s ' 'outside of silva root' % value) del obj.searchValues[field_name] return obj
def upgrade(self, content): logger.info(u"Upgrade News Filter %s.", content_path(content)) root = content.get_root() for source in content._sources: try: target = root.unrestrictedTraverse(source) except (AttributeError, KeyError, NotFound, TypeError): logger.error(u"Could not find content %s for News Filter %s.", source, content_path(content)) continue if INewsPublication.providedBy(target): content.add_source(target) else: logger.error( u"Content type %s is not an allowed source for %s.", content_path(target), content_path(content) ) del content._sources return content
def upgrade(self, content): logger.info(u"Upgrade News Viewer %s.", content_path(content)) root = content.get_root() for flt in content._filters: try: target = root.unrestrictedTraverse(flt) except (AttributeError, KeyError, NotFound, TypeError): logger.error(u"Could not find content %s for News Viewer %s.", flt, content_path(content)) continue if INewsItemFilter.providedBy(target): content.add_filter(target) else: logger.error( u"Content type %s is not an allowed filter for %s.", content_path(target), content_path(content) ) del content._filters return content
def upgrade(self, content): logger.info(u"Upgrade News Viewer %s.", content_path(content)) root = content.get_root() for flt in content._filters: try: target = root.unrestrictedTraverse(flt) except (AttributeError, KeyError, NotFound, TypeError): logger.error(u'Could not find content %s for News Viewer %s.', flt, content_path(content)) continue if INewsItemFilter.providedBy(target): content.add_filter(target) else: logger.error( u'Content type %s is not an allowed filter for %s.', content_path(target), content_path(content)) del content._filters return content
def upgrade(self, content): logger.info(u"Upgrade News Filter %s.", content_path(content)) root = content.get_root() for source in content._sources: try: target = root.unrestrictedTraverse(source) except (AttributeError, KeyError, NotFound, TypeError): logger.error(u'Could not find content %s for News Filter %s.', source, content_path(content)) continue if INewsPublication.providedBy(target): content.add_source(target) else: logger.error( u'Content type %s is not an allowed source for %s.', content_path(target), content_path(content)) del content._sources return content
def upgrade(self, content): annotations = content.__annotations__ from_ = annotations[self.ns] value = from_[self.key] del from_[self.key] to = annotations.get(self.to_ns, None) if to is None: to = annotations[self.to_ns] = PersistentMapping() to[self.key] = value logger.info("Migrated hide_from_tocs for: %s.", content_path(content)) return content
def upgrade(self, container): logger.info(u'Upgrading container order in: %s.', content_path(container)) manager = IOrderManager(container) get_id = manager._get_id order = [] for identifier in container._ordered_ids: content = container._getOb(identifier, None) if content is not None: order.append(get_id(content)) manager.order = order del container._ordered_ids return container
def upgrade(self, content): logger.info(u'Update subscription in: %s.', content_path(content)) annotations = IAnnotations(content) data = SubscribableData(content.__subscribability__) annotations['silva.app.subscriptions'] = data if hasattr(aq_base(content), '__subscriptions__'): data.subscriptions = set(content.__subscriptions__.keys()) delattr(content, '__subscriptions__') if hasattr(aq_base(content), '__pending_subscription_tokens__'): delattr(content, '__pending_subscription_tokens__') delattr(content, '__subscribability__') return content
def upgrade(self, version): link_path = content_path(version) url, target, fragment = resolve_path( version._url, link_path, version.get_container()) if target: logger.info(u'Upgrade link %s.', link_path) version.set_relative(True) version.set_target(target) version._url = u'' else: version._url = url return version
def upgrade(self, doc): # The 3.0 upgrader only upgrade the published, working and # last closed version. Only apply this upgrader on thoses. for version_id in [doc.get_public_version(), doc.get_unapproved_version(), doc.get_last_closed_version()]: if version_id is None: continue version = doc._getOb(version_id, None) if version is None: continue if not isinstance(version.content, ParsedXML): logger.info(u"Upgrade xmlattribute for %s.", content_path(version)) parsed_xml = version.content._content version.content = parsed_xml return doc
def _upgrade_citations(self, version, doc_el): cites = doc_el.getElementsByTagName('cite') if cites: logger.info(u'Upgrading CITE Elements in %s', content_path(version)) for c in cites: author = source = '' citation = [] #html isn't currently allowed in author, source, so # we don't need to "sanity" check them! for node in c.childNodes: if not node.firstChild: continue val = node.firstChild.\ writeStream().getvalue().replace('<','<') if node.nodeType == node.ELEMENT_NODE: if node.nodeName == 'author': author = val elif node.nodeName == 'source': source = val else: citation.append(val) else: citation.append(val) citation = ''.join(citation) cs = doc_el.createElement('source') cs.setAttribute('id', 'cs_citation') p = doc_el.createElement('parameter') p.setAttribute('type', 'string') p.setAttribute('key', 'source') p.appendChild(doc_el.createTextNode(source)) cs.appendChild(p) p = doc_el.createElement('parameter') p.setAttribute('type', 'string') p.setAttribute('key', 'author') p.appendChild(doc_el.createTextNode(author)) cs.appendChild(p) p = doc_el.createElement('parameter') p.setAttribute('type', 'string') p.setAttribute('key', 'citation') p.appendChild(doc_el.createTextNode(citation)) cs.appendChild(p) c.parentNode.replaceChild(cs, c)
def _upgrade_citations(self, version, doc_el): cites = doc_el.getElementsByTagName('cite') if cites: logger.info(u'Upgrading CITE Elements in %s', content_path(version)) for c in cites: author = source = '' citation = [] #html isn't currently allowed in author, source, so # we don't need to "sanity" check them! for node in c.childNodes: if not node.firstChild: continue val = node.firstChild.\ writeStream().getvalue().replace('<','<') if node.nodeType == node.ELEMENT_NODE: if node.nodeName == 'author': author = val elif node.nodeName == 'source': source = val else: citation.append(val) else: citation.append(val) citation = ''.join(citation) cs = doc_el.createElement('source') cs.setAttribute('id','cs_citation') p = doc_el.createElement('parameter') p.setAttribute('type','string') p.setAttribute('key','source') p.appendChild(doc_el.createTextNode(source)) cs.appendChild(p) p = doc_el.createElement('parameter') p.setAttribute('type','string') p.setAttribute('key','author') p.appendChild(doc_el.createTextNode(author)) cs.appendChild(p) p = doc_el.createElement('parameter') p.setAttribute('type','string') p.setAttribute('key','citation') p.appendChild(doc_el.createTextNode(citation)) cs.appendChild(p) c.parentNode.replaceChild(cs,c)
def upgrade(self, item): logger.debug('Update agenda item %s occurrences.', content_path(item)) if not item.get_occurrences(): values = {} for name in [ 'start_datetime', 'end_datetime', 'location', 'recurrence', 'all_day', 'timezone_name' ]: attr = '_' + name if attr in item.__dict__: value = item.__dict__[attr] if isinstance(value, DateTime): value = value.asdatetime() if value is not None: values[name] = value del item.__dict__[attr] item.set_occurrences([AgendaItemOccurrence(**values)]) return item
def upgrade_to_file(self, content, data): data.seek(0) full_data = data.read() data.seek(0) content_type, encoding = self.guess_buffer_type(full_data) if content_type is None or encoding is not None: raise UpgradeError(u"Impossible to detect mimetype.", content) # Fix some bug in old Images that could be BMP if content.web_format not in content.web_formats: content.web_format = 'JPEG' content._image_factory('hires_image', data, content_type) try: content._create_derived_images() except ValueError as error: logger.error(error.args[0]) data.close() ICataloging(content).reindex() logger.info(u"Image rebuilt: %s.", content_path(content)) return content
def upgrade(self, doc): # The 3.0 upgrader only upgrade the published, working and # last closed version. Only apply this upgrader on thoses. for version_id in [ doc.get_public_version(), doc.get_unapproved_version(), doc.get_last_closed_version() ]: if version_id is None: continue version = doc._getOb(version_id, None) if version is None: continue if not isinstance(version.content, ParsedXML): logger.info(u'Upgrade xmlattribute for %s.', content_path(version)) parsed_xml = version.content._content version.content = parsed_xml return doc
def upgrade(self, item): logger.debug(u'Update agenda item %s occurrences.', content_path(item)) if not item.get_occurrences(): values = {} for name in ['start_datetime', 'end_datetime', 'location', 'recurrence', 'all_day', 'timezone_name']: attr = '_' + name if attr in item.__dict__: value = item.__dict__[attr] if isinstance(value, DateTime): value = value.asdatetime() if value is not None: values[name] = value del item.__dict__[attr] item.set_occurrences([AgendaItemOccurrence(**values)]) return item
def upgrade(self, content): logger.info('Upgrading metadata for: %s.', content_path(content)) new_annotations = IAnnotations(aq_base(content)) old_annotations = getattr(aq_base(content), '_portal_annotations_') for key in old_annotations.keys(): old_data = old_annotations[key] # if it is metadata, we have to update the namespaces # as well inside the data, they will go directly in # the annotation if key == 'http://www.infrae.com/xml/metadata': for old_key in old_data.keys(): if old_key in NAMESPACES_CHANGES: new_key = NAMESPACES_CHANGES[old_key] else: new_key = old_key new_annotations[new_key] = old_data[old_key] else: new_annotations[key] = old_data # remove old annotations del aq_base(content)._portal_annotations_ return content
def _upgrade_links(self, version, context, dom): links = dom.getElementsByTagName('link') version_path = content_path(version) if links: logger.info('Upgrading links in: %s.', version_path) for link in links: if link.hasAttribute('reference'): # Already migrated continue path = link.getAttribute('url') # Look for object url, target, fragment = resolve_path(path, version_path, context.model) if fragment: link.setAttribute('anchor', fragment) link.removeAttribute('url') if target is None: if not fragment: link.setAttribute('url', url) continue build_reference(context, target, link) if not fragment: link.removeAttribute('url')
def _upgrade_links(self, version, context, dom): links = dom.getElementsByTagName('link') version_path = content_path(version) if links: logger.info(u'Upgrading links in: %s.', version_path) for link in links: if link.hasAttribute('reference'): # Already migrated continue path = link.getAttribute('url') # Look for object url, target, fragment = resolve_path( path, version_path, context.model) if fragment: link.setAttribute('anchor', fragment) link.removeAttribute('url') if target is None: if not fragment: link.setAttribute('url', url) continue build_reference(context, target, link) if not fragment: link.removeAttribute('url')
def _upgrade_images(self, version, context, dom): images = dom.getElementsByTagName('image') version_path = content_path(version) def make_link(image, target, title='', window_target='', fragment=''): """Create a link, replace the image with it and set the image as child of the link. """ link = dom.createElement('link') if not isinstance(target, str): build_reference(context, target, link) else: link.setAttribute('url', target) if fragment: link.setAttribute('anchor', fragment) if title: link.setAttribute('title', title) if window_target: link.setAttribute('target', window_target) parent = image.parentNode parent.replaceChild(link, image) link.appendChild(image) return link if images: logger.info('Upgrading images in: %s.', version_path) for image in images: if image.hasAttribute('reference'): # Already a reference continue path = image.getAttribute('path') url, target, fragment = resolve_path(path, version_path, context.model, 'image') if target is not None: # If the image target is found it is changed to a # reference. However if it is not, we still want to # process the other aspect of the image tag migration # so just don't do continue here. build_reference(context, target, image) image.removeAttribute('path') image.removeAttribute('rewritten_path') # Collect link title/target title = '' if image.hasAttribute('title'): title = image.getAttribute('title') image.removeAttribute('title') window_target = '' if image.hasAttribute('target'): window_target = image.getAttribute('target') image.removeAttribute('target') link_set = False # Check for a link if image.hasAttribute('link'): link = image.getAttribute('link') if link: link_url, link_target, fragment = resolve_path( link, version_path, context.model) if link_target is not None: make_link(image, link_target, title, window_target, fragment) elif fragment: make_link(image, '', title, window_target, fragment) else: make_link(image, link_url, title, window_target) link_set = True image.removeAttribute('link') # Check for a link to high resolution version of the image if image.hasAttribute('link_to_hires'): link = image.getAttribute('link_to_hires') if link == '1' and link_set is False: make_link(image, target, title, window_target) link_set = True image.removeAttribute('link_to_hires') # Save the image title (aka alt) to its new name if image.hasAttribute('image_title'): title = image.getAttribute('image_title') image.removeAttribute('image_title') image.setAttribute('title', title)
def _upgrade_images(self, version, context, dom): images = dom.getElementsByTagName('image') version_path = content_path(version) def make_link(image, target, title='', window_target='', fragment=''): """Create a link, replace the image with it and set the image as child of the link. """ link = dom.createElement('link') if not isinstance(target, basestring): build_reference(context, target, link) else: link.setAttribute('url', target) if fragment: link.setAttribute('anchor', fragment) if title: link.setAttribute('title', title) if window_target: link.setAttribute('target', window_target) parent = image.parentNode parent.replaceChild(link, image) link.appendChild(image) return link if images: logger.info('Upgrading images in: %s.', version_path) for image in images: if image.hasAttribute('reference'): # Already a reference continue path = image.getAttribute('path') url, target, fragment = resolve_path( path, version_path, context.model, 'image') if target is not None: # If the image target is found it is changed to a # reference. However if it is not, we still want to # process the other aspect of the image tag migration # so just don't do continue here. build_reference(context, target, image) image.removeAttribute('path') image.removeAttribute('rewritten_path') # Collect link title/target title = '' if image.hasAttribute('title'): title = image.getAttribute('title') image.removeAttribute('title') window_target = '' if image.hasAttribute('target'): window_target = image.getAttribute('target') image.removeAttribute('target') link_set = False # Check for a link if image.hasAttribute('link'): link = image.getAttribute('link') if link: link_url, link_target, fragment = resolve_path( link, version_path, context.model) if link_target is not None: make_link( image, link_target, title, window_target, fragment) elif fragment: make_link(image, '', title, window_target, fragment) else: make_link(image, link_url, title, window_target) link_set = True image.removeAttribute('link') # Check for a link to high resolution version of the image if image.hasAttribute('link_to_hires'): link = image.getAttribute('link_to_hires') if link == '1' and link_set is False: make_link(image, target, title, window_target) link_set = True image.removeAttribute('link_to_hires') # Save the image title (aka alt) to its new name if image.hasAttribute('image_title'): title = image.getAttribute('image_title') image.removeAttribute('image_title') image.setAttribute('title', title)
def upgrade(self, doc): logger.info('Upgrading HTML in: %s.', content_path(doc)) # ID + Title identifier = doc.id title = doc.get_title() parent = aq_parent(doc) # Create a new doccopy the annotation try: new_doc = self.create_document(parent, identifier + 'conv__silva30', title) except ValueError: logger.error('Cannot convert document: %s.', content_path(doc)) return doc new_identifier = new_doc.getId() # The id can have changed # Copy annotation copy_annotation(doc, new_doc) # Move references move_references(doc, new_doc) # Last closed version last_closed_version_id = doc.get_last_closed_version() if last_closed_version_id is not None: last_closed_version = doc._getOb(last_closed_version_id, None) if last_closed_version is not None: new_last_closed_version = new_doc.get_editable() self.copy_version(last_closed_version, new_last_closed_version, True) new_doc.approve_version() if new_doc.get_public_version(): # The version can already be expired new_doc.close_version() new_doc.create_copy() # Published version public_version = doc.get_viewable() if public_version is not None: new_public_version = new_doc.get_editable() self.copy_version(public_version, new_public_version, True) new_doc.approve_version() # Editable version editable_version = doc.get_editable() if editable_version is not None: if public_version is not None: new_doc.create_copy() new_editable_version = new_doc.get_editable() self.copy_version(editable_version, new_editable_version) # Markers new_mark_mg = IMarkManager(new_doc) for marker in IMarkManager(doc).usedMarkers: new_mark_mg.add_marker(marker) # Delete old document and rename content to final id order_mg = IOrderManager(parent) position = order_mg.get_position(doc) parent.manage_delObjects([identifier]) try: parent.manage_renameObject(new_identifier, identifier) except CopyError: try: parent._checkId(identifier) except BadRequest: logger.error( "Could not replace document with '%s' identifier, renaming it to '%s_changed'.", identifier, identifier) identifier += '_changed' parent.manage_renameObject(new_identifier, identifier) else: raise new_doc = parent[identifier] if position > -1: order_mg.move(new_doc, position) ICataloging(new_doc).reindex() return new_doc
def upgrade(self, doc): logger.info(u'Upgrading HTML in: %s.', content_path(doc)) # ID + Title identifier = doc.id title = doc.get_title() parent = aq_parent(doc) # Create a new doccopy the annotation try: new_doc = self.create_document( parent, identifier + 'conv__silva30', title) except ValueError: logger.error(u'Cannot convert document: %s.', content_path(doc)) return doc new_identifier = new_doc.getId() # The id can have changed # Copy annotation copy_annotation(doc, new_doc) # Move references move_references(doc, new_doc) # Last closed version last_closed_version_id = doc.get_last_closed_version() if last_closed_version_id is not None: last_closed_version = doc._getOb(last_closed_version_id, None) if last_closed_version is not None: new_last_closed_version = new_doc.get_editable() self.copy_version( last_closed_version, new_last_closed_version, True) new_doc.approve_version() if new_doc.get_public_version(): # The version can already be expired new_doc.close_version() new_doc.create_copy() # Published version public_version = doc.get_viewable() if public_version is not None: new_public_version = new_doc.get_editable() self.copy_version( public_version, new_public_version, True) new_doc.approve_version() # Editable version editable_version = doc.get_editable() if editable_version is not None: if public_version is not None: new_doc.create_copy() new_editable_version = new_doc.get_editable() self.copy_version( editable_version, new_editable_version) # Markers new_mark_mg = IMarkManager(new_doc) for marker in IMarkManager(doc).usedMarkers: new_mark_mg.add_marker(marker) # Delete old document and rename content to final id order_mg = IOrderManager(parent) position = order_mg.get_position(doc) parent.manage_delObjects([identifier]) try: parent.manage_renameObject(new_identifier, identifier) except CopyError: try: parent._checkId(identifier) except BadRequest: logger.error( u"Could not replace document with '%s' identifier, renaming it to '%s_changed'.", identifier, identifier) identifier += '_changed' parent.manage_renameObject(new_identifier, identifier) else: raise new_doc = parent[identifier] if position > -1: order_mg.move(new_doc, position) ICataloging(new_doc).reindex() return new_doc
def upgrade(self, indexer): indexer.update() logger.info('Refreshed indexer %s.', content_path(indexer)) return indexer