| 1 | import Globals |
|---|
| 2 | from Products.CMFCore.DirectoryView import registerDirectory |
|---|
| 3 | from AccessControl import allow_module |
|---|
| 4 | from Products.CMFCore.utils import getToolByName |
|---|
| 5 | from util import SortedDict |
|---|
| 6 | from os import path |
|---|
| 7 | |
|---|
| 8 | from Products.qSEOptimizer import config |
|---|
| 9 | |
|---|
| 10 | try: |
|---|
| 11 | from Products.qSEOptimizer.interfaces import IKeywords |
|---|
| 12 | zope_interface_available = True |
|---|
| 13 | except ImportError: |
|---|
| 14 | zope_interface_available = False |
|---|
| 15 | |
|---|
| 16 | allow_module('Products.qSEOptimizer.util') |
|---|
| 17 | qSEO_globals = globals() |
|---|
| 18 | registerDirectory('skins', qSEO_globals) |
|---|
| 19 | |
|---|
| 20 | try: |
|---|
| 21 | # for Plone-2.1 and higher |
|---|
| 22 | from Products.CMFPlone.PloneTool import PloneTool |
|---|
| 23 | _present = hasattr(PloneTool, "listMetaTags") |
|---|
| 24 | except ImportError: |
|---|
| 25 | _present = False |
|---|
| 26 | |
|---|
| 27 | def propertyItems(context): |
|---|
| 28 | """Return a list of (id,property) tuples. |
|---|
| 29 | """ |
|---|
| 30 | return map(lambda i,c=context: (i['id'],getattr(c,i['id'],None)), |
|---|
| 31 | context._properties) |
|---|
| 32 | |
|---|
| 33 | if _present: |
|---|
| 34 | old_lmt = PloneTool.listMetaTags |
|---|
| 35 | |
|---|
| 36 | def listMetaTags(self, context): |
|---|
| 37 | portal_props = getToolByName(context, 'portal_properties') |
|---|
| 38 | seo_props = getToolByName(portal_props, 'seo_properties', None) |
|---|
| 39 | if seo_props is None: |
|---|
| 40 | return old_lmt(self, context) |
|---|
| 41 | |
|---|
| 42 | site_props = getToolByName(portal_props, 'site_properties') |
|---|
| 43 | exposeDCMetaTags = site_props.exposeDCMetaTags |
|---|
| 44 | |
|---|
| 45 | metaTags = SortedDict() |
|---|
| 46 | metaTags.update(old_lmt(self, context)) |
|---|
| 47 | metadataList = [ |
|---|
| 48 | ('qSEO_Description', 'description'), |
|---|
| 49 | ('qSEO_Keywords', 'keywords'), |
|---|
| 50 | ('qSEO_Robots', 'robots'), |
|---|
| 51 | ('qSEO_Distribution','distribution')] |
|---|
| 52 | |
|---|
| 53 | if exposeDCMetaTags: |
|---|
| 54 | metadataList.append(('qSEO_Distribution', 'DC.distribution')) |
|---|
| 55 | |
|---|
| 56 | for accessor, key in metadataList: |
|---|
| 57 | method = getattr(context, accessor, None) |
|---|
| 58 | if not callable(method): |
|---|
| 59 | # ups |
|---|
| 60 | continue |
|---|
| 61 | # Catch AttributeErrors raised by some AT applications |
|---|
| 62 | try: |
|---|
| 63 | value = method() |
|---|
| 64 | except AttributeError: |
|---|
| 65 | value = None |
|---|
| 66 | |
|---|
| 67 | if not value: |
|---|
| 68 | continue |
|---|
| 69 | if isinstance(value, (tuple, list)): |
|---|
| 70 | value = ', '.join(value) |
|---|
| 71 | |
|---|
| 72 | metaTags[key] = value |
|---|
| 73 | |
|---|
| 74 | # add custom meta tags (added from qseo tab by user) for given context |
|---|
| 75 | property_prefix = 'qSEO_custom_' |
|---|
| 76 | for property, value in propertyItems(context): |
|---|
| 77 | idx = property.find(property_prefix) |
|---|
| 78 | if idx == 0 and len(property) > len(property_prefix): |
|---|
| 79 | metaTags[property[len(property_prefix):]] = value |
|---|
| 80 | |
|---|
| 81 | # Set the additional matching keywords, if any |
|---|
| 82 | if zope_interface_available: |
|---|
| 83 | adapter = IKeywords(context, None) |
|---|
| 84 | if adapter is not None: |
|---|
| 85 | keywords = adapter.listKeywords() |
|---|
| 86 | if keywords: |
|---|
| 87 | metaTags['keywords'] = keywords |
|---|
| 88 | |
|---|
| 89 | return metaTags |
|---|
| 90 | |
|---|
| 91 | PloneTool.listMetaTags = listMetaTags |
|---|