1 | import re, commands |
---|
2 | from zope.interface import implements |
---|
3 | from zope.component import queryMultiAdapter |
---|
4 | from Products.CMFCore.utils import getToolByName |
---|
5 | |
---|
6 | from quintagroup.seoptimizer.interfaces import IMetaKeywords, IMappingMetaTags |
---|
7 | from quintagroup.seoptimizer.util import SortedDict |
---|
8 | |
---|
9 | |
---|
10 | class MetaKeywordsAdapter(object): |
---|
11 | implements(IMetaKeywords) |
---|
12 | |
---|
13 | def __init__(self, context): |
---|
14 | self.context = context |
---|
15 | |
---|
16 | def getMetaKeywords(self): |
---|
17 | """ See interface. |
---|
18 | """ |
---|
19 | request = self.context.REQUEST |
---|
20 | meta_keywords = '' |
---|
21 | filtered_keywords = [] |
---|
22 | portal_props = getToolByName(self.context, 'portal_properties') |
---|
23 | seo_props = getToolByName(portal_props, 'seo_properties', None) |
---|
24 | seo_context = queryMultiAdapter((self.context, request), name='seo_context') |
---|
25 | if seo_context: |
---|
26 | meta_keywords = list(seo_context.meta_keywords()) |
---|
27 | is_test = self.context.REQUEST.get('without_metatag_keywords', None) |
---|
28 | if meta_keywords and is_test is None: |
---|
29 | # extract words from url page using lynx browser (test page randered without metatag keywords) |
---|
30 | text = commands.getoutput('lynx --dump --nolist %s?without_metatag_keywords=1' % self.context.absolute_url()).lower() |
---|
31 | |
---|
32 | # for tests package |
---|
33 | if text and 'lynx: can\'t access startfile http://nohost/plone/my_doc?without_metatag_keywords=1' in text: |
---|
34 | text = self.context.getText() |
---|
35 | |
---|
36 | if text and text != 'sh: lynx: command not found': |
---|
37 | text = text.decode('utf8') |
---|
38 | for meta_keyword in meta_keywords: |
---|
39 | if re.compile(u'\\b%s\\b' % meta_keyword.decode('utf8').lower(), re.I|re.U).search(text): |
---|
40 | filtered_keywords.append(meta_keyword) |
---|
41 | meta_keywords = filtered_keywords |
---|
42 | return ', '.join(meta_keywords) |
---|
43 | |
---|
44 | |
---|
45 | class MappingMetaTags(object): |
---|
46 | implements(IMappingMetaTags) |
---|
47 | |
---|
48 | def __init__(self, context): |
---|
49 | self.context = context |
---|
50 | self.portal_props = getToolByName(self.context, 'portal_properties') |
---|
51 | self.seo_props = getToolByName(self.portal_props, 'seo_properties', None) |
---|
52 | |
---|
53 | def getMappingMetaTags(self): |
---|
54 | """ See interface. |
---|
55 | """ |
---|
56 | metadata_name = SortedDict() |
---|
57 | if self.seo_props: |
---|
58 | pmn = self.seo_props.getProperty('metatags_order') |
---|
59 | pmn = pmn and pmn or '' |
---|
60 | for mt in [mt.split(' ') for mt in pmn if len(mt.split(' '))==2]: |
---|
61 | metadata_name[mt[0]] = mt[1] |
---|
62 | return metadata_name |
---|