1 | import re, commands |
---|
2 | |
---|
3 | from zope.component import adapts |
---|
4 | from zope.interface import implements |
---|
5 | from zope.component import queryMultiAdapter |
---|
6 | |
---|
7 | from Acquisition import aq_inner |
---|
8 | from Products.CMFCore.utils import getToolByName |
---|
9 | |
---|
10 | from quintagroup.seoptimizer.util import SortedDict |
---|
11 | from quintagroup.seoptimizer.interfaces import IMetaKeywords, IMappingMetaTags |
---|
12 | |
---|
13 | |
---|
14 | class MetaKeywordsAdapter(object): |
---|
15 | implements(IMetaKeywords) |
---|
16 | |
---|
17 | def __init__(self, context): |
---|
18 | self.context = context |
---|
19 | |
---|
20 | def getMetaKeywords(self): |
---|
21 | """ See interface. |
---|
22 | """ |
---|
23 | request = self.context.REQUEST |
---|
24 | meta_keywords = '' |
---|
25 | filtered_keywords = [] |
---|
26 | portal_props = getToolByName(self.context, 'portal_properties') |
---|
27 | seo_props = getToolByName(portal_props, 'seo_properties', None) |
---|
28 | seo_context = queryMultiAdapter((self.context, request), name='seo_context') |
---|
29 | if seo_context: |
---|
30 | filter_keywords_by_content = seo_props.getProperty('filter_keywords_by_content', None) |
---|
31 | meta_keywords = list(seo_context.meta_keywords()) |
---|
32 | is_test = self.context.REQUEST.get('without_metatag_keywords', None) |
---|
33 | if filter_keywords_by_content and meta_keywords and is_test is None: |
---|
34 | # extract words from url page using lynx browser (test page randered without metatag keywords) |
---|
35 | text = commands.getoutput('lynx --dump --nolist %s?without_metatag_keywords=1' % self.context.absolute_url()).lower() |
---|
36 | |
---|
37 | # for tests package |
---|
38 | if text and 'lynx: can\'t access startfile http://nohost/plone/my_doc?without_metatag_keywords=1' in text: |
---|
39 | text = self.context.getText() |
---|
40 | |
---|
41 | if text and text != 'sh: lynx: command not found': |
---|
42 | text = text.decode('utf8') |
---|
43 | for meta_keyword in meta_keywords: |
---|
44 | if re.compile(u'\\b%s\\b' % meta_keyword.decode('utf8').lower(), re.I|re.U).search(text): |
---|
45 | filtered_keywords.append(meta_keyword) |
---|
46 | meta_keywords = filtered_keywords |
---|
47 | return ', '.join(meta_keywords) |
---|
48 | |
---|
49 | |
---|
50 | class MappingMetaTags(object): |
---|
51 | implements(IMappingMetaTags) |
---|
52 | |
---|
53 | def __init__(self, context): |
---|
54 | self.context = context |
---|
55 | self.portal_props = getToolByName(self.context, 'portal_properties') |
---|
56 | self.seo_props = getToolByName(self.portal_props, 'seo_properties', None) |
---|
57 | |
---|
58 | def getMappingMetaTags(self): |
---|
59 | """ See interface. |
---|
60 | """ |
---|
61 | metadata_name = SortedDict() |
---|
62 | if self.seo_props: |
---|
63 | pmn = self.seo_props.getProperty('metatags_order') |
---|
64 | pmn = pmn and pmn or '' |
---|
65 | for mt in [mt.split(' ') for mt in pmn if len(mt.split(' '))==2]: |
---|
66 | metadata_name[mt[0]] = mt[1] |
---|
67 | return metadata_name |
---|
68 | |
---|
69 | |
---|
70 | class canonicalPathAdapter(object): |
---|
71 | """Adapts base content to canonical path, with taking into consideration |
---|
72 | SEO canonical path value. |
---|
73 | """ |
---|
74 | |
---|
75 | def __init__(self, context): |
---|
76 | self.context = context |
---|
77 | |
---|
78 | def canonical_path(self): |
---|
79 | purl = getToolByName(self.context,'portal_url') |
---|
80 | |
---|
81 | prop = aq_inner(self.context).getProperty('qSEO_canonical', None) |
---|
82 | if prop is not None: |
---|
83 | return prop[len(purl()):] |
---|
84 | |
---|
85 | return '/'+'/'.join(purl.getRelativeContentPath(self.context)) |
---|