| 1 | ################################################################################
|
|---|
| 2 | # _zcatalogmanager.py
|
|---|
| 3 | #
|
|---|
| 4 | # This program is free software; you can redistribute it and/or
|
|---|
| 5 | # modify it under the terms of the GNU General Public License
|
|---|
| 6 | # as published by the Free Software Foundation; either version 2
|
|---|
| 7 | # of the License, or (at your option) any later version.
|
|---|
| 8 | #
|
|---|
| 9 | # This program is distributed in the hope that it will be useful,
|
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | # GNU General Public License for more details.
|
|---|
| 13 | #
|
|---|
| 14 | # You should have received a copy of the GNU General Public License
|
|---|
| 15 | # along with this program; if not, write to the Free Software
|
|---|
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 17 | ################################################################################
|
|---|
| 18 |
|
|---|
| 19 | # Imports.
|
|---|
| 20 | from Products.ZCatalog import ZCatalog, CatalogAwareness
|
|---|
| 21 | from zope.interface import implements
|
|---|
| 22 | import re
|
|---|
| 23 | import sys
|
|---|
| 24 | # Product Imports.
|
|---|
| 25 | import _globals
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | # ------------------------------------------------------------------------------
|
|---|
| 29 | # _zcatalogmanager.Empty
|
|---|
| 30 | # ------------------------------------------------------------------------------
|
|---|
| 31 | class Empty: pass
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | # ------------------------------------------------------------------------------
|
|---|
| 35 | # _zcatalogmanager.zcat_meta_types
|
|---|
| 36 | # ------------------------------------------------------------------------------
|
|---|
| 37 | zcat_meta_types = ['ZMS','ZMSCustom']
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | # ------------------------------------------------------------------------------
|
|---|
| 41 | # _zcatalogmanager.intValue:
|
|---|
| 42 | # ------------------------------------------------------------------------------
|
|---|
| 43 | def intValue(v):
|
|---|
| 44 | try:
|
|---|
| 45 | i = int(v)
|
|---|
| 46 | except:
|
|---|
| 47 | i = 0
|
|---|
| 48 | return i
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | # ------------------------------------------------------------------------------
|
|---|
| 52 | # _zcatalogmanager.search_string:
|
|---|
| 53 | #
|
|---|
| 54 | # Search string of given value.
|
|---|
| 55 | # ------------------------------------------------------------------------------
|
|---|
| 56 | def search_string(v):
|
|---|
| 57 | s = ''
|
|---|
| 58 | if v is not None:
|
|---|
| 59 | if type(v) is str and len(v) > 0:
|
|---|
| 60 | s += v + ' '
|
|---|
| 61 | elif type(v) is list:
|
|---|
| 62 | for i in v:
|
|---|
| 63 | s += search_string(i) + ' '
|
|---|
| 64 | elif type(v) is dict:
|
|---|
| 65 | for k in v.keys():
|
|---|
| 66 | i = v[k]
|
|---|
| 67 | s += search_string(i) + ' '
|
|---|
| 68 | return s
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | # ------------------------------------------------------------------------------
|
|---|
| 72 | # _zcatalogmanager.search_quote:
|
|---|
| 73 | #
|
|---|
| 74 | # Remove HTML-Tags from given string.
|
|---|
| 75 | # ------------------------------------------------------------------------------
|
|---|
| 76 | def search_quote(s, maxlen=255, tag='·'):
|
|---|
| 77 | # remove all tags.
|
|---|
| 78 | s = re.sub( '<script((.|\n|\r|\t)*?)>((.|\n|\r|\t)*?)</script>', '', s)
|
|---|
| 79 | s = re.sub( '<style((.|\n|\r|\t)*?)>((.|\n|\r|\t)*?)</style>', '', s)
|
|---|
| 80 | s = re.sub( '<((.|\n|\r|\t)*?)>', '', s)
|
|---|
| 81 | # limit characters.
|
|---|
| 82 | if len(s) > maxlen:
|
|---|
| 83 | if s[:maxlen].rfind('&') >= 0 and not s[:maxlen].rfind('&') < s[:maxlen].rfind(';') and \
|
|---|
| 84 | s[maxlen:].find(';') >= 0 and not s[maxlen:].find(';') > s[maxlen:].find('&'):
|
|---|
| 85 | maxlen = maxlen + s[maxlen:].find(';')
|
|---|
| 86 | if s[:maxlen].endswith(chr(195)) and maxlen < len(s):
|
|---|
| 87 | maxlen += 1
|
|---|
| 88 | s = s[:maxlen] + tag * 3
|
|---|
| 89 | # return quoted search string.
|
|---|
| 90 | return s
|
|---|
| 91 |
|
|---|
| 92 | # ------------------------------------------------------------------------------
|
|---|
| 93 | # _zcatalogmanager.addLexicon:
|
|---|
| 94 | # ------------------------------------------------------------------------------
|
|---|
| 95 | def addLexicon( self, cat):
|
|---|
| 96 |
|
|---|
| 97 | #-- Remove Lexicon
|
|---|
| 98 | ids = cat.objectIds( ['ZCTextIndex Lexicon','ZCTextIndex Unicode Lexicon'])
|
|---|
| 99 | if len( ids) > 0:
|
|---|
| 100 | cat.manage_delObjects( ids)
|
|---|
| 101 |
|
|---|
| 102 | #-- Add Lexicon
|
|---|
| 103 | index_type = self.getConfProperty('ZCatalog.TextIndexType','ZCTextIndex')
|
|---|
| 104 | if index_type == 'ZCTextIndex':
|
|---|
| 105 | elements = []
|
|---|
| 106 | wordSplitter = Empty()
|
|---|
| 107 | wordSplitter.group = 'Word Splitter'
|
|---|
| 108 | wordSplitter.name = 'HTML aware splitter'
|
|---|
| 109 | elements.append(wordSplitter)
|
|---|
| 110 | caseNormalizer = Empty()
|
|---|
| 111 | caseNormalizer.group = 'Case Normalizer'
|
|---|
| 112 | caseNormalizer.name = 'Case Normalizer'
|
|---|
| 113 | elements.append(caseNormalizer)
|
|---|
| 114 | stopWords = Empty()
|
|---|
| 115 | stopWords.group = 'Stop Words'
|
|---|
| 116 | stopWords.name = 'Remove listed and single char words'
|
|---|
| 117 | elements.append(stopWords)
|
|---|
| 118 | try:
|
|---|
| 119 | cat.manage_addProduct['ZCTextIndex'].manage_addLexicon('Lexicon', 'Default lexicon', elements)
|
|---|
| 120 | except:
|
|---|
| 121 | pass
|
|---|
| 122 |
|
|---|
| 123 | # --------------------------------------------------------------------------
|
|---|
| 124 | # _zcatalogmanager.getCatalog:
|
|---|
| 125 | # --------------------------------------------------------------------------
|
|---|
| 126 | def getCatalog(self, lang):
|
|---|
| 127 | context = self.getDocumentElement()
|
|---|
| 128 | cat_id = 'catalog_%s'%lang
|
|---|
| 129 | obs = filter( lambda x: x.id==cat_id, context.objectValues( [ 'ZCatalog']))
|
|---|
| 130 | if obs:
|
|---|
| 131 | return obs[0]
|
|---|
| 132 | else:
|
|---|
| 133 | return getattr( self, cat_id, None)
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | ################################################################################
|
|---|
| 137 | ################################################################################
|
|---|
| 138 | ###
|
|---|
| 139 | ### class ZCatalogItem
|
|---|
| 140 | ###
|
|---|
| 141 | ################################################################################
|
|---|
| 142 | ################################################################################
|
|---|
| 143 | class ZCatalogItem(CatalogAwareness.CatalogAware):
|
|---|
| 144 |
|
|---|
| 145 | """
|
|---|
| 146 | TextIndexNG3
|
|---|
| 147 | """
|
|---|
| 148 | try:
|
|---|
| 149 |
|
|---|
| 150 | from textindexng.interfaces import IIndexableContent
|
|---|
| 151 | implements(IIndexableContent)
|
|---|
| 152 |
|
|---|
| 153 | def indexableContent(self, fields):
|
|---|
| 154 | from textindexng.content import IndexContentCollector as ICC
|
|---|
| 155 | icc = ICC()
|
|---|
| 156 | default_language = self.REQUEST.get('lang',self.getPrimaryLanguage())
|
|---|
| 157 | default_encoding = 'utf-8'
|
|---|
| 158 |
|
|---|
| 159 | for f in fields:
|
|---|
| 160 | v = getattr(self, f, None)
|
|---|
| 161 | if not v: continue
|
|---|
| 162 |
|
|---|
| 163 | if f in ['zcat_data']:
|
|---|
| 164 |
|
|---|
| 165 | # unpack result triple
|
|---|
| 166 | source, mimetype, encoding = v()
|
|---|
| 167 | icc.addBinary(f, source, mimetype, encoding, default_language)
|
|---|
| 168 |
|
|---|
| 169 | elif f in ['zcat_text']:
|
|---|
| 170 | v = v()
|
|---|
| 171 |
|
|---|
| 172 | # accept only a string/unicode string
|
|---|
| 173 | if not isinstance(v, basestring):
|
|---|
| 174 | raise TypeError('Value returned for field "%s" must be string or unicode (got: %s, %s)' % (f, repr(v), type(v)))
|
|---|
| 175 |
|
|---|
| 176 | if isinstance(v, str):
|
|---|
| 177 | v = unicode(v, default_encoding, 'ignore')
|
|---|
| 178 |
|
|---|
| 179 | icc.addContent(f, v, default_language)
|
|---|
| 180 |
|
|---|
| 181 | return icc
|
|---|
| 182 |
|
|---|
| 183 | except:
|
|---|
| 184 | pass
|
|---|
| 185 |
|
|---|
| 186 | # --------------------------------------------------------------------------
|
|---|
| 187 | # ZCatalogItem.search_quote:
|
|---|
| 188 | #
|
|---|
| 189 | # Remove HTML-Tags.
|
|---|
| 190 | # --------------------------------------------------------------------------
|
|---|
| 191 | def search_quote(self, s, maxlen=255, tag='·'):
|
|---|
| 192 | return search_quote(s,maxlen,tag)
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 | # --------------------------------------------------------------------------
|
|---|
| 196 | # ZCatalogItem.search_encode:
|
|---|
| 197 | #
|
|---|
| 198 | # Encodes given string.
|
|---|
| 199 | # --------------------------------------------------------------------------
|
|---|
| 200 | def search_encode(self, s):
|
|---|
| 201 | return _globals.umlaut_quote(s)
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 | # --------------------------------------------------------------------------
|
|---|
| 205 | # ZCatalogItem.getCatalogNavUrl:
|
|---|
| 206 | #
|
|---|
| 207 | # Returns catalog-navigation url.
|
|---|
| 208 | # --------------------------------------------------------------------------
|
|---|
| 209 | def getCatalogNavUrl(self, REQUEST):
|
|---|
| 210 | return self.url_inherit_params(REQUEST['URL'],REQUEST,['qs'])
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 | # --------------------------------------------------------------------------
|
|---|
| 214 | # ZCatalogItem.catalogText:
|
|---|
| 215 | #
|
|---|
| 216 | # Catalog text.
|
|---|
| 217 | # --------------------------------------------------------------------------
|
|---|
| 218 | def catalogText(self, REQUEST):
|
|---|
| 219 | v = ''
|
|---|
| 220 |
|
|---|
| 221 | # Custom hook (overwrite).
|
|---|
| 222 | if 'catalogText' in self.getMetaobjAttrIds(self.meta_id):
|
|---|
| 223 | value = self.getObjProperty('catalogText',REQUEST)
|
|---|
| 224 | value = search_string(value)
|
|---|
| 225 | v += value
|
|---|
| 226 |
|
|---|
| 227 | else:
|
|---|
| 228 |
|
|---|
| 229 | ##### Trigger custom catalogText-Contributors (if there is one) ####
|
|---|
| 230 | l = _globals.triggerEvent( self, 'catalogTextContrib', REQUEST=REQUEST)
|
|---|
| 231 | if l:
|
|---|
| 232 | v += ' '.join(l)
|
|---|
| 233 |
|
|---|
| 234 | # Attributes.
|
|---|
| 235 | for key in filter( lambda x: x.find('_') != 0, self.getObjAttrs().keys()):
|
|---|
| 236 | obj_attr = self.getObjAttr(key)
|
|---|
| 237 | if obj_attr['xml']:
|
|---|
| 238 | datatype = obj_attr['datatype_key']
|
|---|
| 239 | if datatype in _globals.DT_STRINGS or \
|
|---|
| 240 | datatype == _globals.DT_DICT or \
|
|---|
| 241 | datatype == _globals.DT_LIST:
|
|---|
| 242 | value = self.getObjAttrValue(obj_attr,REQUEST)
|
|---|
| 243 | value = search_string(value)
|
|---|
| 244 | # Increase weight of attributes!
|
|---|
| 245 | if obj_attr['id'].find('title') == 0:
|
|---|
| 246 | value = value * 5
|
|---|
| 247 | elif obj_attr['id'].find('attr_dc_') == 0:
|
|---|
| 248 | value = value * 3
|
|---|
| 249 | v += value
|
|---|
| 250 | for ob in filter( lambda x: x.isActive(REQUEST), self.getChildNodes()):
|
|---|
| 251 | if not ob.isCatalogItem():
|
|---|
| 252 | v += ob.catalogText(REQUEST)
|
|---|
| 253 | return v
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 | # --------------------------------------------------------------------------
|
|---|
| 257 | # ZCatalogItem.catalogData:
|
|---|
| 258 | #
|
|---|
| 259 | # Catalog data.
|
|---|
| 260 | # --------------------------------------------------------------------------
|
|---|
| 261 | def catalogData(self, REQUEST):
|
|---|
| 262 | source = ''
|
|---|
| 263 | key = self.zcat_data_key()
|
|---|
| 264 | if key is not None:
|
|---|
| 265 | file = self.getObjProperty( key, REQUEST)
|
|---|
| 266 | if file is not None:
|
|---|
| 267 | source = file.getData()
|
|---|
| 268 | mimetype = file.getContentType()
|
|---|
| 269 | encoding = 'utf-8'
|
|---|
| 270 | return source, mimetype, encoding
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 | ############################################################################
|
|---|
| 274 | ###
|
|---|
| 275 | ### Metadate: Indices / Columns
|
|---|
| 276 | ###
|
|---|
| 277 | ############################################################################
|
|---|
| 278 |
|
|---|
| 279 | # --------------------------------------------------------------------------
|
|---|
| 280 | # ZCatalogItem.zcat_data_key:
|
|---|
| 281 | # --------------------------------------------------------------------------
|
|---|
| 282 | def zcat_data_key(self):
|
|---|
| 283 | ids = self.getMetaobjAttrIds(self.meta_id,types=[_globals.DT_FILE])
|
|---|
| 284 | if len(ids) == 1:
|
|---|
| 285 | return ids[ 0]
|
|---|
| 286 | return None
|
|---|
| 287 |
|
|---|
| 288 | # --------------------------------------------------------------------------
|
|---|
| 289 | # ZCatalogItem.zcat_data:
|
|---|
| 290 | # --------------------------------------------------------------------------
|
|---|
| 291 | def zcat_data( self, lang=None):
|
|---|
| 292 | if lang is None:
|
|---|
| 293 | lang = self.REQUEST.get('lang',self.getPrimaryLanguage())
|
|---|
| 294 | source, mimetype, encoding = self.catalogData( {'lang':lang})
|
|---|
| 295 | return source, mimetype, encoding
|
|---|
| 296 |
|
|---|
| 297 | # --------------------------------------------------------------------------
|
|---|
| 298 | # ZCatalogItem.zcat_text:
|
|---|
| 299 | # --------------------------------------------------------------------------
|
|---|
| 300 | def zcat_text( self, lang=None):
|
|---|
| 301 | if lang is None:
|
|---|
| 302 | lang = self.REQUEST.get('lang',self.getPrimaryLanguage())
|
|---|
| 303 | req = {'lang':lang}
|
|---|
| 304 | zcat = self.catalogText( req)
|
|---|
| 305 | zcat = self.search_encode( zcat)
|
|---|
| 306 | return zcat
|
|---|
| 307 |
|
|---|
| 308 | # --------------------------------------------------------------------------
|
|---|
| 309 | # ZCatalogItem.zcat_date:
|
|---|
| 310 | # --------------------------------------------------------------------------
|
|---|
| 311 | def zcat_date( self, lang=None):
|
|---|
| 312 | if lang is None:
|
|---|
| 313 | lang = self.REQUEST.get('lang',self.getPrimaryLanguage())
|
|---|
| 314 | req = {'lang':lang}
|
|---|
| 315 | zcat = self.getObjProperty('attr_dc_date',req)
|
|---|
| 316 | if zcat is None or zcat == '':
|
|---|
| 317 | zcat = self.getObjProperty('change_dt',req)
|
|---|
| 318 | for ob in self.filteredChildNodes( req, self.PAGEELEMENTS):
|
|---|
| 319 | ob_change_dt = ob.getObjProperty('change_dt',req)
|
|---|
| 320 | if ob_change_dt > zcat:
|
|---|
| 321 | zcat = ob_change_dt
|
|---|
| 322 | return zcat
|
|---|
| 323 |
|
|---|
| 324 | # --------------------------------------------------------------------------
|
|---|
| 325 | # ZCatalogItem.zcat_title:
|
|---|
| 326 | # --------------------------------------------------------------------------
|
|---|
| 327 | def zcat_title( self, lang=None):
|
|---|
| 328 | if lang is None:
|
|---|
| 329 | lang = self.REQUEST.get('lang',self.getPrimaryLanguage())
|
|---|
| 330 | req = {'lang':lang}
|
|---|
| 331 | zcat = self.getTitle( req)
|
|---|
| 332 | zcat = self.search_encode( zcat)
|
|---|
| 333 | zcat = self.search_quote( zcat)
|
|---|
| 334 | return zcat
|
|---|
| 335 |
|
|---|
| 336 | # --------------------------------------------------------------------------
|
|---|
| 337 | # ZCatalogItem.zcat_summary:
|
|---|
| 338 | # --------------------------------------------------------------------------
|
|---|
| 339 | def zcat_summary( self, lang=None):
|
|---|
| 340 | if lang is None:
|
|---|
| 341 | lang = self.REQUEST.get('lang',self.getPrimaryLanguage())
|
|---|
| 342 | req = {'lang':lang}
|
|---|
| 343 | zcat = self.getObjProperty( 'attr_dc_description', req)
|
|---|
| 344 | zcat = self.search_encode( zcat)
|
|---|
| 345 | zcat = self.search_quote( zcat)
|
|---|
| 346 | return zcat
|
|---|
| 347 |
|
|---|
| 348 | # --------------------------------------------------------------------------
|
|---|
| 349 | # ZCatalogItem.zcat_url:
|
|---|
| 350 | # --------------------------------------------------------------------------
|
|---|
| 351 | def zcat_url( self, lang=None):
|
|---|
| 352 | if lang is None:
|
|---|
| 353 | lang = self.REQUEST.get('lang',self.getPrimaryLanguage())
|
|---|
| 354 | req = {'lang':lang}
|
|---|
| 355 | txng_key = self.zcat_data_key()
|
|---|
| 356 | txng_value = None
|
|---|
| 357 | if txng_key is not None:
|
|---|
| 358 | txng_value = self.getObjProperty( txng_key, req)
|
|---|
| 359 | if txng_value is not None:
|
|---|
| 360 | zcat = txng_value.getHref( req)
|
|---|
| 361 | zcat = '/'.join(zcat.split('/')[-2:])
|
|---|
| 362 | else:
|
|---|
| 363 | zcat = 'index_%s.html'%lang
|
|---|
| 364 | return zcat
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 | # --------------------------------------------------------------------------
|
|---|
| 368 | # ZCatalogItem.synchronizeSearch:
|
|---|
| 369 | # --------------------------------------------------------------------------
|
|---|
| 370 | def synchronizeSearch(self, REQUEST, forced=0):
|
|---|
| 371 | if self.getConfProperty('ZMS.CatalogAwareness.active',1) or forced:
|
|---|
| 372 | _globals.writeLog( self, '[synchronizeSearch]')
|
|---|
| 373 | for ref_by in self.getRefByObjs(REQUEST):
|
|---|
| 374 | ref_ob = self.getLinkObj(ref_by,REQUEST)
|
|---|
| 375 | if ref_ob is not None and \
|
|---|
| 376 | ref_ob. meta_type == 'ZMSLinkElement' and \
|
|---|
| 377 | ref_ob.isEmbedded( REQUEST) and not \
|
|---|
| 378 | ref_ob.isEmbeddedRecursive( REQUEST):
|
|---|
| 379 | if not forced or ref_ob.getHome().id != self.getHome().id:
|
|---|
| 380 | ref_ob.synchronizeSearch( REQUEST=REQUEST, forced=forced)
|
|---|
| 381 | lang = REQUEST.get( 'lang', self.getPrimaryLanguage())
|
|---|
| 382 | # Reindex object.
|
|---|
| 383 | ob = self.getCatalogItem()
|
|---|
| 384 | if ob is not None:
|
|---|
| 385 | ob.default_catalog = 'catalog_%s'%lang
|
|---|
| 386 | ob.reindex_object()
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 | # --------------------------------------------------------------------------
|
|---|
| 390 | # ZCatalogItem.isCatalogItem:
|
|---|
| 391 | #
|
|---|
| 392 | # Returns true if this is a catalog item.
|
|---|
| 393 | # --------------------------------------------------------------------------
|
|---|
| 394 | def isCatalogItem(self):
|
|---|
| 395 | b = False
|
|---|
| 396 | b = b or (self.isPage() or self.meta_id == 'ZMSFile')
|
|---|
| 397 | b = b or (self.getConfProperty('ZCatalog.TextIndexNG',0)==1 and self.zcat_data_key() is not None)
|
|---|
| 398 | return b
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 | # --------------------------------------------------------------------------
|
|---|
| 402 | # ZCatalogItem.getCatalogItem:
|
|---|
| 403 | # --------------------------------------------------------------------------
|
|---|
| 404 | def getCatalogItem(self):
|
|---|
| 405 | ob = self
|
|---|
| 406 | while ob is not None:
|
|---|
| 407 | if ob.isCatalogItem():
|
|---|
| 408 | break
|
|---|
| 409 | ob = ob.getParentNode()
|
|---|
| 410 | return ob
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 | # --------------------------------------------------------------------------
|
|---|
| 414 | # ZCatalogItem.reindexCatalogItem:
|
|---|
| 415 | #
|
|---|
| 416 | # Reindex catalog item.
|
|---|
| 417 | # --------------------------------------------------------------------------
|
|---|
| 418 | def reindexCatalogItem(self, REQUEST):
|
|---|
| 419 | message = ''
|
|---|
| 420 | # Process catalog-item.
|
|---|
| 421 | if self.isCatalogItem():
|
|---|
| 422 | self.synchronizeSearch(REQUEST=REQUEST,forced=1)
|
|---|
| 423 | # Recurse.
|
|---|
| 424 | for ob in filter( lambda x: x.isActive(REQUEST), self.filteredChildNodes(REQUEST)):
|
|---|
| 425 | ob.reindexCatalogItem(REQUEST)
|
|---|
| 426 | # Return with message.
|
|---|
| 427 | return message
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 | ################################################################################
|
|---|
| 431 | ################################################################################
|
|---|
| 432 | ###
|
|---|
| 433 | ### class ZCatalogManager
|
|---|
| 434 | ###
|
|---|
| 435 | ################################################################################
|
|---|
| 436 | ################################################################################
|
|---|
| 437 | class ZCatalogManager:
|
|---|
| 438 |
|
|---|
| 439 | # --------------------------------------------------------------------------
|
|---|
| 440 | # ZCatalogManager.reindexCatalog:
|
|---|
| 441 | #
|
|---|
| 442 | # Reindex catalog.
|
|---|
| 443 | # --------------------------------------------------------------------------
|
|---|
| 444 | def reindexCatalog(self, REQUEST, clients=False):
|
|---|
| 445 | message = ''
|
|---|
| 446 |
|
|---|
| 447 | for lang in self.getLangIds():
|
|---|
| 448 | REQUEST.set('lang',lang)
|
|---|
| 449 |
|
|---|
| 450 | #-- Recreate catalog.
|
|---|
| 451 | message += self.recreateCatalog(lang)+'<br/>'
|
|---|
| 452 |
|
|---|
| 453 | #-- Find items to catalog.
|
|---|
| 454 | message += self.reindexCatalogItem(REQUEST)+'<br/>'
|
|---|
| 455 |
|
|---|
| 456 | message += 'Catalog %s indexed successfully.'%self.getHome().id
|
|---|
| 457 |
|
|---|
| 458 | #-- Process clients.
|
|---|
| 459 | if clients:
|
|---|
| 460 | for portalClient in self.getPortalClients():
|
|---|
| 461 | message += portalClient.reindexCatalog( REQUEST, clients)
|
|---|
| 462 |
|
|---|
| 463 | # Return with message.
|
|---|
| 464 | return message
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 | # --------------------------------------------------------------------------
|
|---|
| 468 | # ZCatalogManager.recreateCatalog:
|
|---|
| 469 | #
|
|---|
| 470 | # Recreates catalog.
|
|---|
| 471 | # --------------------------------------------------------------------------
|
|---|
| 472 | def recreateCatalog(self, lang):
|
|---|
| 473 | message = ''
|
|---|
| 474 | context = self.getDocumentElement()
|
|---|
| 475 |
|
|---|
| 476 | #-- Get catalog
|
|---|
| 477 | zcatalog = getCatalog( self, lang)
|
|---|
| 478 | if zcatalog is None:
|
|---|
| 479 | cat_id = 'catalog_%s'%lang
|
|---|
| 480 | cat_title = 'Default catalog'
|
|---|
| 481 | vocab_id = 'create_default_catalog_'
|
|---|
| 482 | zcatalog = ZCatalog.ZCatalog(cat_id, cat_title, vocab_id, context)
|
|---|
| 483 | context._setObject(zcatalog.id, zcatalog)
|
|---|
| 484 | zcatalog = getCatalog( self, lang)
|
|---|
| 485 |
|
|---|
| 486 | #-- Add lexicon
|
|---|
| 487 | addLexicon( self, zcatalog)
|
|---|
| 488 |
|
|---|
| 489 | #-- Clear catalog
|
|---|
| 490 | zcatalog.manage_catalogClear()
|
|---|
| 491 |
|
|---|
| 492 | #-- Delete indexes from catalog
|
|---|
| 493 | index_names = []
|
|---|
| 494 | l = []
|
|---|
| 495 | l.extend( zcatalog.schema())
|
|---|
| 496 | l.extend( zcatalog.indexes())
|
|---|
| 497 | for index_name in l:
|
|---|
| 498 | if index_name not in index_names and \
|
|---|
| 499 | (index_name.find( 'zcat_') == 0 or index_name == 'meta_id'):
|
|---|
| 500 | try:
|
|---|
| 501 | zcatalog.manage_delColumn( [ index_name])
|
|---|
| 502 | except:
|
|---|
| 503 | _globals.writeError(self,"[recreateCatalog]: Can't delete column '%s' from catalog"%index_name)
|
|---|
| 504 | try:
|
|---|
| 505 | zcatalog.manage_delIndex( [ index_name])
|
|---|
| 506 | except:
|
|---|
| 507 | _globals.writeError(self,"[recreateCatalog]: Can't delete index '%s' from catalog"%index_name)
|
|---|
| 508 | index_names.append( index_name)
|
|---|
| 509 |
|
|---|
| 510 | #-- Get index types.
|
|---|
| 511 | index_types = []
|
|---|
| 512 | for index in zcatalog.Indexes.filtered_meta_types():
|
|---|
| 513 | index_types.append(index['name'])
|
|---|
| 514 |
|
|---|
| 515 | #-- (Re-)create indexes on catalog
|
|---|
| 516 | message += "Create Index: "
|
|---|
| 517 | index_name = 'meta_id'
|
|---|
| 518 | zcatalog.manage_addColumn(index_name)
|
|---|
| 519 | message += index_name
|
|---|
| 520 | # Text (Index & Column)
|
|---|
| 521 | index_name = 'zcat_text'
|
|---|
| 522 | zcatalog.manage_addColumn(index_name)
|
|---|
| 523 | index_type = self.getConfProperty('ZCatalog.TextIndexType','ZCTextIndex')
|
|---|
| 524 | extra = None
|
|---|
| 525 | if index_type == 'ZCTextIndex':
|
|---|
| 526 | extra = Empty()
|
|---|
| 527 | extra.doc_attr = index_name
|
|---|
| 528 | extra.index_type = 'Okapi BM25 Rank'
|
|---|
| 529 | extra.lexicon_id = 'Lexicon'
|
|---|
| 530 | else:
|
|---|
| 531 | extra = {}
|
|---|
| 532 | extra['default_encoding'] = 'utf-8'
|
|---|
| 533 | extra['indexed_fields'] = index_name
|
|---|
| 534 | extra['fields'] = [index_name]
|
|---|
| 535 | extra['near_distance'] = 5
|
|---|
| 536 | extra['splitter_casefolding'] = 1
|
|---|
| 537 | extra['splitter_max_len'] = 64
|
|---|
| 538 | extra['splitter_separators'] = '.+-_@'
|
|---|
| 539 | extra['splitter_single_chars'] = 0
|
|---|
| 540 | if index_type == 'TextIndexNG2':
|
|---|
| 541 | extra['use_converters'] = 1
|
|---|
| 542 | extra['use_normalizer'] = ''
|
|---|
| 543 | # setattr(index_extra,'use_stemmer','')
|
|---|
| 544 | extra['use_stopwords'] = ''
|
|---|
| 545 | elif index_type == 'TextIndexNG3':
|
|---|
| 546 | extra['languages'] = (lang,)
|
|---|
| 547 | extra['query_parser'] = 'txng.parsers.en'
|
|---|
| 548 | extra['index_unknown_languages'] = True
|
|---|
| 549 | extra['dedicated_storage'] = True
|
|---|
| 550 | extra['use_stopwords'] = False
|
|---|
| 551 | extra['use_normalizer'] = False
|
|---|
| 552 | extra['use_converters'] = True
|
|---|
| 553 | extra['use_stemmer'] = False
|
|---|
| 554 | zcatalog.manage_addIndex(index_name,index_type,extra)
|
|---|
| 555 | message += ", "+index_name+"("+index_type+")"
|
|---|
| 556 | # Date (Column)
|
|---|
| 557 | index_name = 'zcat_date'
|
|---|
| 558 | zcatalog.manage_addColumn(index_name)
|
|---|
| 559 | message += ", "+index_name
|
|---|
| 560 | # Title (Column)
|
|---|
| 561 | index_name = 'zcat_title'
|
|---|
| 562 | zcatalog.manage_addColumn(index_name)
|
|---|
| 563 | message += ", "+index_name
|
|---|
| 564 | # Summary (Column)
|
|---|
| 565 | index_name = 'zcat_summary'
|
|---|
| 566 | zcatalog.manage_addColumn(index_name)
|
|---|
| 567 | message += ", "+index_name
|
|---|
| 568 | # Url (Column)
|
|---|
| 569 | index_name = 'zcat_url'
|
|---|
| 570 | zcatalog.manage_addColumn(index_name)
|
|---|
| 571 | message += ", "+index_name
|
|---|
| 572 | # Data (Index)
|
|---|
| 573 | index_type = None
|
|---|
| 574 | for k in [ 'ZCTextIndex', 'TextIndexNG2', 'TextIndexNG3']:
|
|---|
| 575 | if k in index_types:
|
|---|
| 576 | index_type = k
|
|---|
| 577 | if self.getConfProperty('ZCatalog.TextIndexNG',0)==1:
|
|---|
| 578 | index_name = 'zcat_data'
|
|---|
| 579 | extra = {}
|
|---|
| 580 | if index_type == 'ZCTextIndex':
|
|---|
| 581 | extra['doc_attr'] = index_name
|
|---|
| 582 | extra['index_type'] = 'Okapi BM25 Rank'
|
|---|
| 583 | extra['lexicon_id'] = 'Lexicon'
|
|---|
| 584 | else:
|
|---|
| 585 | extra['default_encoding'] = 'utf-8'
|
|---|
| 586 | extra['indexed_fields'] = index_name
|
|---|
| 587 | extra['fields'] = [index_name]
|
|---|
| 588 | extra['near_distance'] = 5
|
|---|
| 589 | extra['splitter_casefolding'] = 1
|
|---|
| 590 | extra['splitter_max_len'] = 64
|
|---|
| 591 | extra['splitter_separators'] = '.+-_@'
|
|---|
| 592 | extra['splitter_single_chars'] = 0
|
|---|
| 593 | if index_type == 'TextIndexNG2':
|
|---|
| 594 | extra['use_converters'] = 1
|
|---|
| 595 | extra['use_normalizer'] = ''
|
|---|
| 596 | extra['use_stopwords'] = ''
|
|---|
| 597 | elif index_type == 'TextIndexNG3':
|
|---|
| 598 | extra['languages'] = (lang,)
|
|---|
| 599 | extra['query_parser'] = 'txng.parsers.en'
|
|---|
| 600 | extra['index_unknown_languages'] = True
|
|---|
| 601 | extra['dedicated_storage'] = True
|
|---|
| 602 | extra['use_stopwords'] = False
|
|---|
| 603 | extra['use_normalizer'] = False
|
|---|
| 604 | extra['use_converters'] = True
|
|---|
| 605 | extra['use_stemmer'] = False
|
|---|
| 606 | zcatalog.manage_addIndex(index_name,index_type,extra)
|
|---|
| 607 | message += ", "+index_name+"("+index_type+")"
|
|---|
| 608 |
|
|---|
| 609 | #-- Return message.
|
|---|
| 610 | return message
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 | # --------------------------------------------------------------------------
|
|---|
| 614 | # ZCatalogManager.getCatalogQueryString:
|
|---|
| 615 | # --------------------------------------------------------------------------
|
|---|
| 616 | def getCatalogQueryString(self, raw, option='AND', only_words=False):
|
|---|
| 617 | qs = []
|
|---|
| 618 | i = 0
|
|---|
| 619 | for si in raw.split('"'):
|
|---|
| 620 | si = si.strip()
|
|---|
| 621 | if si:
|
|---|
| 622 | if i % 2 == 0:
|
|---|
| 623 | for raw_item in si.split(' '):
|
|---|
| 624 | raw_item = raw_item.strip()
|
|---|
| 625 | if len(raw_item) > 1 and not raw_item.upper() in ['AND','OR']:
|
|---|
| 626 | raw_item = raw_item.replace('-','* AND *')
|
|---|
| 627 | if not only_words and not raw_item.endswith('*'):
|
|---|
| 628 | raw_item += '*'
|
|---|
| 629 | if raw_item not in qs:
|
|---|
| 630 | qs.append( raw_item)
|
|---|
| 631 | else:
|
|---|
| 632 | raw_item = '"%s"'%si
|
|---|
| 633 | if raw_item not in qs:
|
|---|
| 634 | qs.append( raw_item)
|
|---|
| 635 | i += 1
|
|---|
| 636 | return (' %s '%option).join(filter( lambda x: len(x.strip())>0, qs))
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 | # --------------------------------------------------------------------------
|
|---|
| 640 | # ZCatalogManager.getCatalogPathObject:
|
|---|
| 641 | #
|
|---|
| 642 | # Returns object from catalog-path.
|
|---|
| 643 | # --------------------------------------------------------------------------
|
|---|
| 644 | def getCatalogPathObject(self, path):
|
|---|
| 645 | ob = self.getHome()
|
|---|
| 646 | l = path.split( '/')
|
|---|
| 647 | if ob.id not in l:
|
|---|
| 648 | docElmnt = self.getDocumentElement()
|
|---|
| 649 | if docElmnt.id not in l:
|
|---|
| 650 | ob = docElmnt
|
|---|
| 651 | else:
|
|---|
| 652 | l = l[ l.index(ob.id)+1:]
|
|---|
| 653 | for id in l:
|
|---|
| 654 | if len( id) > 0 and ob is not None:
|
|---|
| 655 | ob = getattr(ob,id,None)
|
|---|
| 656 | return ob
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 | # --------------------------------------------------------------------------
|
|---|
| 660 | # ZCatalogManager.submitCatalogQuery:
|
|---|
| 661 | #
|
|---|
| 662 | # Submits query to catalog.
|
|---|
| 663 | # --------------------------------------------------------------------------
|
|---|
| 664 | def submitCatalogQuery(self, search_query, search_order_by, search_meta_types, search_clients, REQUEST):
|
|---|
| 665 |
|
|---|
| 666 | #-- Initialize local variables.
|
|---|
| 667 | rtn = []
|
|---|
| 668 | if search_query == '':
|
|---|
| 669 | return rtn
|
|---|
| 670 |
|
|---|
| 671 | #-- Process clients.
|
|---|
| 672 | if self.getConfProperty('ZCatalog.portalClients',1) == 1 and search_clients == 1:
|
|---|
| 673 | for portalClient in self.getPortalClients():
|
|---|
| 674 | rtn.extend(portalClient.submitCatalogQuery( search_query, search_order_by, search_meta_types, search_clients, REQUEST))
|
|---|
| 675 |
|
|---|
| 676 | #-- Search catalog.
|
|---|
| 677 | lang = REQUEST['lang']
|
|---|
| 678 | zcatalog = getCatalog(self,lang)
|
|---|
| 679 | if zcatalog is None:
|
|---|
| 680 | return rtn
|
|---|
| 681 | items = []
|
|---|
| 682 | for zcindex in zcatalog.indexes():
|
|---|
| 683 | if zcindex.find('zcat_')==0:
|
|---|
| 684 | d = {}
|
|---|
| 685 | d['meta_type'] = zcat_meta_types
|
|---|
| 686 | if zcindex.find('zcat_data')==0:
|
|---|
| 687 | d[zcindex] = search_query
|
|---|
| 688 | else:
|
|---|
| 689 | d[zcindex] = self.search_encode( search_query)
|
|---|
| 690 | _globals.writeLog( self, "[searchCatalog]: %s=%s"%(zcindex,d[zcindex]))
|
|---|
| 691 | qr = zcatalog(d)
|
|---|
| 692 | _globals.writeLog( self, "[searchCatalog]: qr=%i"%len( qr))
|
|---|
| 693 | items.extend( qr)
|
|---|
| 694 |
|
|---|
| 695 | #-- Sort order.
|
|---|
| 696 | if int(search_order_by)==1:
|
|---|
| 697 | order_by = 'score'
|
|---|
| 698 | else:
|
|---|
| 699 | order_by = 'time'
|
|---|
| 700 |
|
|---|
| 701 | #-- Process results.
|
|---|
| 702 | results = []
|
|---|
| 703 | for item in items:
|
|---|
| 704 | data_record_id = item.data_record_id_
|
|---|
| 705 | path = zcatalog.getpath(data_record_id)
|
|---|
| 706 | # Append to valid results.
|
|---|
| 707 | if (len(search_meta_types) == 0 or item.meta_id in search_meta_types) and \
|
|---|
| 708 | (len(filter(lambda x: x[1]['path']==path, results)) == 0):
|
|---|
| 709 | result = {}
|
|---|
| 710 | result['score'] = intValue(item.data_record_score_)
|
|---|
| 711 | result['normscore'] = intValue(item.data_record_normalized_score_)
|
|---|
| 712 | result['time'] = getattr(item,'zcat_date',None)
|
|---|
| 713 | result['path'] = path
|
|---|
| 714 | if REQUEST.get('search_ob',True):
|
|---|
| 715 | ob = self.getCatalogPathObject( path)
|
|---|
| 716 | append = ob is not None
|
|---|
| 717 | if append:
|
|---|
| 718 | for o in ob.breadcrumbs_obj_path():
|
|---|
| 719 | append = append and o.isActive(REQUEST)
|
|---|
| 720 | if append:
|
|---|
| 721 | result['ob'] = ob
|
|---|
| 722 | result['url'] = ob.getDeclUrl(REQUEST) + '/' + ob.zcat_url(lang)
|
|---|
| 723 | results.append((result[order_by],result))
|
|---|
| 724 | else:
|
|---|
| 725 | result['title'] = getattr(item,'zcat_title','')
|
|---|
| 726 | result['summary'] = getattr(item,'zcat_summary','')
|
|---|
| 727 | result['zcat_url'] = getattr(item,'zcat_url','')
|
|---|
| 728 | result['url'] = (path + '/' + getattr(item,'zcat_url','')).replace('//','/')
|
|---|
| 729 | results.append((result[order_by],result))
|
|---|
| 730 |
|
|---|
| 731 | #-- Sort objects.
|
|---|
| 732 | results.sort()
|
|---|
| 733 | results.reverse()
|
|---|
| 734 |
|
|---|
| 735 | #-- Append objects.
|
|---|
| 736 | rtn.extend(map(lambda ob: ob[1],results))
|
|---|
| 737 |
|
|---|
| 738 | #-- Return objects in correct sort-order.
|
|---|
| 739 | return rtn
|
|---|
| 740 |
|
|---|
| 741 | ################################################################################
|
|---|