Package platecom :: Package ontocatalog :: Package indexes :: Module related
[hide private]
[frames] | no frames]

Source Code for Module icsemantic.catalog.indexes.related

  1  """RelatedIndex 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  related words 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  from Globals import Persistent, DTMLFile 
 16  from OFS.SimpleItem import SimpleItem 
 17  from BTrees.IIBTree import IITreeSet, IISet, intersection, union 
 18   
 19  from Products.PluginIndexes import PluggableIndex 
 20  from Products.PluginIndexes.common.util import parseIndexRequest 
 21  from Products.PluginIndexes.interfaces import IPluggableIndex 
 22   
 23  from Products.CMFCore.utils import getToolByName 
 24   
 25  from platecom.ontocatalog.indexes import utils 
 26   
 27   
28 -class RelatedIndex(Persistent, SimpleItem):
29 """An index for related words that does not index anything""" 30 31 __implements__ = (PluggableIndex.PluggableIndexInterface,) 32 implements(IPluggableIndex) 33 34 meta_type = "RelatedIndex" 35 manage_workspace = DTMLFile('dtml/manageFakeIndex', globals()) 36
37 - def __init__(self, id, extra=None, caller=None):
38 """Creates a new index""" 39 self.id = id 40 self.catalog = caller
41
42 - def index_object(self, docid, obj ,threshold=100):
43 """Hook for (Z)Catalog. Since this is a fake index, nothing is 44 done here. 45 """ 46 return 1
47
48 - def unindex_object(self, docid):
49 """Hook for (Z)Catalog. Since this is a fake index, nothing is 50 done here. 51 """ 52 return
53
54 - def _apply_index(self, request, cid=''):
55 """Apply the index to query parameters given in the argument, 56 request. 57 58 The argument should be a mapping object. 59 60 If the request does not contain the needed parameters, then 61 None is returned. 62 63 Otherwise two objects are returned. The first object is a 64 ResultSet containing the record numbers of the matching 65 records. The second object is a tuple containing the names of 66 all data fields used. 67 """ 68 portal = getToolByName(self, 'portal_url').getPortalObject() 69 query_options = ('query', 'context') 70 record = parseIndexRequest(request, self.id, query_options) 71 if record.keys is None: 72 return None 73 74 #Languages dance 75 langutil = queryUtility(IUserPreferredLanguages, 76 name='platecom_preferred_languages') 77 user_languages = tuple(langutil.getPreferredLanguages(request=self.REQUEST)) 78 79 context = record.get('context', None) 80 81 if context is not None: 82 context = context + '@' + user_language 83 84 thesaurus_results = [] 85 catalog_results = () 86 for k in record.keys: 87 for lang in user_languages: 88 key = '%s@%s' % (k, lang) 89 thesaurus_results += utils.get_related(portal, key, 90 lang=user_languages, 91 context=context) 92 93 if thesaurus_results != []: 94 tuples = [tl.split('@') for tl in thesaurus_results] 95 related = ['\"%s\"' % (t,) for (t,l) in tuples] 96 search_text = ' OR '.join(related) 97 query = {'SearchableText': search_text, 98 'Language': user_languages} 99 catalog_results += tuple(self.catalog(query)) 100 101 result = utils.build_catalog_results(self.id, self.catalog._catalog, 102 catalog_results) 103 return result
104 105 106 manage_addRelatedIndexForm = DTMLFile('dtml/addFakeIndex', globals()) 107
108 -def manage_addRelatedIndex(self, id, extra=None, REQUEST=None, RESPONSE=None, 109 URL3=None):
110 """Add a fake index""" 111 return self.manage_addIndex(id, 'RelatedIndex', extra=extra, 112 REQUEST=REQUEST, RESPONSE=RESPONSE, URL1=URL3)
113