1 """TranslationIndex is an index for the catalog that does not index
2 anything.
3
4 Instead, the _apply_index performs a query to a thesaurus object for
5 translations of the keyword parameters and then queries the site
6 catalog for the result of the thesaurus query.
7 """
8
9 from logging import getLogger
10
11 from zope.interface import implements
12 from zope.component import queryUtility
13 from zope.i18n.interfaces import IUserPreferredLanguages
14
15
16 from Globals import Persistent, DTMLFile
17 from OFS.SimpleItem import SimpleItem
18 from BTrees.IIBTree import IITreeSet, IISet, intersection, union
19
20 from Products.PluginIndexes import PluggableIndex
21 from Products.PluginIndexes.common.util import parseIndexRequest
22 from Products.PluginIndexes.interfaces import IPluggableIndex
23
24 from Products.CMFCore.utils import getToolByName
25
26 from platecom.ontocatalog.indexes import utils
27
28
30 """An index for translations that does not index anything"""
31
32 __implements__ = (PluggableIndex.PluggableIndexInterface,)
33 implements(IPluggableIndex)
34
35 meta_type = "TranslationIndex"
36 manage_workspace = DTMLFile('dtml/manageFakeIndex', globals())
37
38 - def __init__(self, id, extra=None, caller=None):
39 """Creates a new index"""
40 self.id = id
41 self.catalog = caller
42
44 """Hook for (Z)Catalog. Since this is a fake index, nothing is
45 done here.
46 """
47 return 1
48
50 """Hook for (Z)Catalog. Since this is a fake index, nothing is
51 done here.
52 """
53 return
54
56 """Apply the index to query parameters given in the argument,
57 request.
58
59 The argument should be a mapping object.
60
61 If the request does not contain the needed parameters, then
62 None is returned.
63
64 Otherwise two objects are returned. The first object is a
65 ResultSet containing the record numbers of the matching
66 records. The second object is a tuple containing the names of
67 all data fields used.
68 """
69 portal = getToolByName(self, 'portal_url').getPortalObject()
70 query_options = ('query', 'language')
71 record = parseIndexRequest(request, self.id, query_options)
72 if record.keys is None:
73 return None
74
75 language = record.get('language', None)
76
77 if language is not None:
78 language = ('',) + tuple(language)
79 else:
80 language = ('',)
81
82
83 langutil = queryUtility(IUserPreferredLanguages,
84 name='platecom_preferred_languages')
85 user_languages = tuple(langutil.getPreferredLanguages(request=self.REQUEST))
86
87 thesaurus_results = []
88 catalog_results = ()
89 for k in record.keys:
90 for lang in user_languages:
91 filtered_langs = [l for l in language if
92 l not in [lang, '']]
93 key = "%s@%s" % (k, lang)
94 thesaurus_results += utils.get_equivalent(portal, key,
95 filtered_langs)
96
97
98
99 if thesaurus_results != []:
100 tuples = [tl.split('@') for tl in thesaurus_results]
101 translations = ['\"%s\"' % (t,) for (t,l) in tuples]
102 search_text = ' OR '.join(translations)
103 query = {'SearchableText': search_text,
104 'Language': language}
105 catalog_results += tuple(self.catalog(query))
106
107 result = utils.build_catalog_results(self.id, self.catalog._catalog,
108 catalog_results)
109 return result
110
111
112 manage_addTranslationIndexForm = DTMLFile('dtml/addFakeIndex', globals())
113
116 """Add a fake index"""
117 return self.manage_addIndex(id, 'TranslationIndex', extra=extra,
118 REQUEST=REQUEST, RESPONSE=RESPONSE, URL1=URL3)
119