[1850] | 1 | import logging |
---|
| 2 | from zope.component import queryMultiAdapter |
---|
| 3 | |
---|
[3223] | 4 | from Products.CMFCore.utils import getToolByName |
---|
| 5 | from Products.CMFPlone.utils import getSiteEncoding, safe_unicode |
---|
| 6 | |
---|
| 7 | from quintagroup.seoptimizer.browser.seo_configlet import ISEOConfigletSchema |
---|
| 8 | from quintagroup.seoptimizer.util import unescape |
---|
[3547] | 9 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
[1930] | 10 | from quintagroup.canonicalpath.adapters import PROPERTY_LINK |
---|
| 11 | |
---|
[1850] | 12 | logger = logging.getLogger('quintagroup.seoptimizer') |
---|
| 13 | FIX_PTYPES_DOMAIN = ['Document', 'File', 'News Item'] |
---|
[3134] | 14 | REMOVE_SEOPROPERTIES = ['additional_keywords', |
---|
| 15 | 'settings_use_keywords_sg', |
---|
| 16 | 'settings_use_keywords_lg', |
---|
| 17 | 'filter_keywords_by_content', |
---|
[3547] | 18 | ] |
---|
[1850] | 19 | |
---|
[3134] | 20 | |
---|
[1850] | 21 | def changeDomain(plone_tools): |
---|
[1910] | 22 | """ Fix i18n_domain bug for some portal_types, |
---|
[1958] | 23 | which present in earlier versions of the package. |
---|
[1850] | 24 | """ |
---|
| 25 | types_tool = plone_tools.types() |
---|
[1910] | 26 | for ptype in [ptypes for ptypes in types_tool.objectValues() |
---|
[3547] | 27 | if ptypes.id in FIX_PTYPES_DOMAIN]: |
---|
[1850] | 28 | if ptype.i18n_domain == 'quintagroup.seoptimizer': |
---|
| 29 | ptype.i18n_domain = 'plone' |
---|
[1910] | 30 | logger.log(logging.INFO, "I18n Domain of the type \'%s\' " |
---|
| 31 | "changed to \'plone\'." % ptype.id) |
---|
[1850] | 32 | |
---|
[3134] | 33 | |
---|
[1850] | 34 | def changeMetatagsOrderList(plone_tools): |
---|
[3134] | 35 | """ Change format metatags order list |
---|
| 36 | from "metaname accessor" to "metaname". |
---|
[1850] | 37 | """ |
---|
[3141] | 38 | plone_tools.types() |
---|
[1850] | 39 | setup_tool = plone_tools.url().getPortalObject().portal_setup |
---|
| 40 | seoprops_tool = plone_tools.properties().seo_properties |
---|
| 41 | if seoprops_tool.hasProperty('metatags_order'): |
---|
| 42 | mto = seoprops_tool.getProperty('metatags_order', []) |
---|
| 43 | mto_new = [line.split(' ')[0].strip() for line in mto] |
---|
| 44 | if not list(mto) == mto_new: |
---|
[1910] | 45 | logger.log(logging.INFO, "Changed format metatags order list in " |
---|
| 46 | "configlet from \"metaname accessor\" to \"metaname\".") |
---|
[1850] | 47 | seoprops_tool.manage_changeProperties(metatags_order=mto_new) |
---|
| 48 | else: |
---|
[3134] | 49 | name_profile = 'profile-quintagroup.seoptimizer:default' |
---|
| 50 | setup_tool.runImportStepFromProfile(name_profile, 'propertiestool') |
---|
[1850] | 51 | |
---|
| 52 | |
---|
| 53 | def migrationActions(plone_tools): |
---|
[1910] | 54 | """ Migration actions from portal_types action to seoproperties tool |
---|
[1958] | 55 | (for seoaction in portal_actions). |
---|
[1850] | 56 | """ |
---|
| 57 | types_tool = plone_tools.types() |
---|
| 58 | seoprops_tool = plone_tools.properties().seo_properties |
---|
[3134] | 59 | property_name = 'content_types_with_seoproperties' |
---|
| 60 | ctws = list(seoprops_tool.getProperty(property_name, [])) |
---|
[1850] | 61 | flag = False |
---|
| 62 | for ptype in types_tool.objectValues(): |
---|
[1910] | 63 | idxs = [idx_act[0] for idx_act in enumerate(ptype.listActions()) |
---|
[3547] | 64 | if idx_act[1].id == 'seo_properties'] |
---|
[1850] | 65 | if idxs: |
---|
| 66 | if ptype.id not in ctws: |
---|
| 67 | ctws.append(ptype.id) |
---|
| 68 | flag = True |
---|
| 69 | ptype.deleteActions(idxs) |
---|
[1910] | 70 | logger.log(logging.INFO, "Moved \"SEO Properties\" action from %s " |
---|
| 71 | "type in portal actions." % ptype.id) |
---|
[1850] | 72 | if flag: |
---|
[3134] | 73 | seo_change_properties = seoprops_tool.manage_changeProperties |
---|
| 74 | seo_change_properties(content_types_with_seoproperties=ctws) |
---|
[1850] | 75 | |
---|
[3134] | 76 | |
---|
[1850] | 77 | def removeNonUseSeoProperties(plone_tools): |
---|
| 78 | """ Revome properties used in earlier versions of the package |
---|
| 79 | """ |
---|
| 80 | seoprops_tool = plone_tools.properties().seo_properties |
---|
| 81 | remove_properties = [] |
---|
| 82 | for pr in REMOVE_SEOPROPERTIES: |
---|
[3134] | 83 | if seoprops_tool.hasProperty(pr): |
---|
| 84 | remove_properties.append(pr) |
---|
| 85 | logger.log(logging.INFO, "Removed %s property in " |
---|
| 86 | "seoproperties tool." % pr) |
---|
[1850] | 87 | if not remove_properties: |
---|
| 88 | seoprops_tool.manage_delProperties(remove_properties) |
---|
| 89 | |
---|
[3134] | 90 | |
---|
[1925] | 91 | def removeSkin(plone_tools): |
---|
[1958] | 92 | """ Remove skin layers. |
---|
[1925] | 93 | """ |
---|
| 94 | layer = 'quintagroup.seoptimizer' |
---|
| 95 | skins_tool = plone_tools.url().getPortalObject().portal_skins |
---|
| 96 | for skinName in skins_tool.getSkinSelections(): |
---|
[3134] | 97 | skin_paths = skins_tool.getSkinPath(skinName).split(',') |
---|
[3547] | 98 | paths = [l.strip() for l in skin_paths |
---|
| 99 | if not (l == layer or l.startswith(layer + '/'))] |
---|
[1925] | 100 | logger.log(logging.INFO, "Removed layers from %s skin." % skinName) |
---|
| 101 | skins_tool.addSkinSelection(skinName, ','.join(paths)) |
---|
| 102 | |
---|
[3134] | 103 | |
---|
[1930] | 104 | def migrateCanonical(plone_tools): |
---|
| 105 | """ Rename qSEO_canonical property into PROPERTY_LINK |
---|
| 106 | for all portal objects, which use SEO |
---|
| 107 | """ |
---|
| 108 | types = plone_tools.types() |
---|
| 109 | portal = plone_tools.url().getPortalObject() |
---|
| 110 | allCTTypes = types.listContentTypes() |
---|
[3547] | 111 | obj_metatypes = [m.content_meta_type for m in types.objectValues() |
---|
[3134] | 112 | if m.getId() in allCTTypes] |
---|
[3547] | 113 | portal.ZopeFindAndApply(portal, |
---|
[1930] | 114 | obj_metatypes=','.join(obj_metatypes), |
---|
| 115 | apply_func=renameProperty |
---|
| 116 | ) |
---|
| 117 | |
---|
[3134] | 118 | |
---|
[1930] | 119 | def renameProperty(obj, path): |
---|
| 120 | """ Rename qSEO_canonical property into PROPERTY_LINK |
---|
| 121 | for obj, which use SEO |
---|
| 122 | """ |
---|
| 123 | if obj.hasProperty('qSEO_canonical'): |
---|
| 124 | value = obj.getProperty('qSEO_canonical') |
---|
| 125 | |
---|
| 126 | level, msg = logging.INFO, "For %(url)s object 'qSEO_canonical' "\ |
---|
[3547] | 127 | "property renamed to '%(name)s'." |
---|
[1930] | 128 | try: |
---|
| 129 | ICanonicalLink(obj).canonical_link = value |
---|
[2238] | 130 | except Exception, e: |
---|
[1930] | 131 | level, msg = logging.ERROR, "%s on renaming 'qSEO_canonical' " \ |
---|
[3547] | 132 | "property for %%(url)s object" % str(e) |
---|
[1930] | 133 | |
---|
[3134] | 134 | logger.log(level, msg % {'url': obj.absolute_url(), |
---|
| 135 | 'name': PROPERTY_LINK}) |
---|
[1930] | 136 | obj.manage_delProperties(['qSEO_canonical']) |
---|
| 137 | |
---|
[3134] | 138 | |
---|
[1850] | 139 | def upgrade_2_to_3(setuptool): |
---|
| 140 | """ Upgrade quintagroup.seoptimizer from version 2.x.x to 3.0.0. |
---|
| 141 | """ |
---|
[3134] | 142 | plone_tools = queryMultiAdapter((setuptool, setuptool.REQUEST), |
---|
| 143 | name="plone_tools") |
---|
| 144 | profile_name = 'profile-quintagroup.seoptimizer:upgrade_2_to_3' |
---|
| 145 | setuptool.runAllImportStepsFromProfile(profile_name) |
---|
[1850] | 146 | migrationActions(plone_tools) |
---|
| 147 | changeDomain(plone_tools) |
---|
| 148 | changeMetatagsOrderList(plone_tools) |
---|
| 149 | removeNonUseSeoProperties(plone_tools) |
---|
[1925] | 150 | removeSkin(plone_tools) |
---|
[1930] | 151 | migrateCanonical(plone_tools) |
---|
[3223] | 152 | |
---|
| 153 | |
---|
| 154 | def unescapeOldTitle(setuptool): |
---|
| 155 | """ Upgrade quintagroup.seoptimizer title and comments properties. |
---|
| 156 | """ |
---|
| 157 | portal = getToolByName(setuptool, "portal_url").getPortalObject() |
---|
| 158 | types = ISEOConfigletSchema(portal).types_seo_enabled |
---|
| 159 | |
---|
| 160 | catalog = getToolByName(portal, "portal_catalog") |
---|
| 161 | brains = catalog(portal_type=types) |
---|
| 162 | |
---|
| 163 | for b in brains: |
---|
| 164 | obj = b.getObject() |
---|
| 165 | obj_enc = getSiteEncoding(obj) |
---|
| 166 | |
---|
| 167 | if obj.hasProperty("qSEO_title"): |
---|
| 168 | uni_qSEO_title = safe_unicode(obj.qSEO_title, encoding=obj_enc) |
---|
| 169 | fixed_title = unescape(uni_qSEO_title).encode(obj_enc) |
---|
| 170 | obj._updateProperty("qSEO_title", fixed_title) |
---|
| 171 | |
---|
| 172 | if obj.hasProperty("qSEO_html_comment"): |
---|
| 173 | uni_qSEO_html_comment = safe_unicode(obj.qSEO_html_comment, |
---|
| 174 | encoding=obj_enc) |
---|
| 175 | fixed_comment = unescape(uni_qSEO_html_comment).encode(obj_enc) |
---|
| 176 | obj._updateProperty("qSEO_html_comment", fixed_comment) |
---|