From: <ke...@us...> - 2006-11-26 19:50:54
|
Revision: 3712 http://svn.sourceforge.net/mailmanager/?rev=3712&view=rev Author: kevca Date: 2006-11-26 11:50:47 -0800 (Sun, 26 Nov 2006) Log Message: ----------- About to rename. Changes made to allow better zope integration. Removed back references to mmobj. Modified Paths: -------------- MailManager/branches/RELENG_2_2/support/searching/xapian.py Modified: MailManager/branches/RELENG_2_2/support/searching/xapian.py =================================================================== --- MailManager/branches/RELENG_2_2/support/searching/xapian.py 2006-11-26 19:34:29 UTC (rev 3711) +++ MailManager/branches/RELENG_2_2/support/searching/xapian.py 2006-11-26 19:50:47 UTC (rev 3712) @@ -5,111 +5,32 @@ from DateTime import DateTime from email.Utils import formataddr, parseaddr, mktime_tz, parsedate_tz -import xapian import logging -class ISearch: - """ - Interface for a MailManager search class. - - Any search technology that is to be utilised within MailManager - should implement all of the following methods. - """ - def clearIndex(self): - """ - Clear the search database completely. - """ - pass - def rebuildIndex(self): - """ - Rebuild the search index i.e. if something gets corrupted it should - be possible to recreate the index from existing messages in the system - for searching. - """ - pass +from Globals import DTMLFile, InitializeClass - def addMessageToIndex(self, message_id, ticket_id, ticket, msg, body, html_body, from_name, msg_date): - """ - Add a message to the search index. - """ - pass +from AccessControl import ClassSecurityInfo +from Products.PageTemplates.PageTemplateFile import PageTemplateFile +import DateTime - def deleteMessageFromIndex(self, message_id): - """ - Add a message to the search index. - """ - pass +import OFS.SimpleItem +import OFS.Folder - def searchIndex(self, searchParams): - """ - Search the index - """ - pass +from Globals import HTMLFile +from Globals import MessageDialog +from Globals import Persistent -class PostgresSearch(ISearch): +import OFS.PropertyManager +import OFS.SimpleItem +import Acquisition # Various acquisition types. +import AccessControl.Role # Zope Security management. - def __init__(self, mmobj): - self.mmobj = mmobj +import Globals - def clearIndex(self): - pass +import xapian - def rebuildIndex(self): - pass - - def addMessageToIndex(self, message_id, ticket_id, ticket, msg, body, html_body, from_name, msg_date): - pass - - def searchIndex(self, searchParams): - searchText = searchParams.get('searchText') - if searchText: - regex = re.compile('([() ])AND([() ])', re.IGNORECASE) - searchText = regex.sub('\g<1>&\g<2>', searchText) - regex = re.compile('([() ])OR([() ])', re.IGNORECASE) - searchText = regex.sub('\g<1>|\g<2>', searchText) - regex = re.compile('([() ]|^)NOT([() ])', re.IGNORECASE) - searchText = regex.sub('\g<1>!\g<2>', searchText) - searchi['searchText'] = searchText - - try: - self.sql.testQuery(sqv_query=searchText) - except: - REQUEST.set('error', 'The supplied query is not correct. ' - 'Words must be separated by AND or OR. Check the help ' - 'page for more information on what queries are valid') - REQUEST.set('flag_searchText', 1) - return self.Search(self, REQUEST) - - searchParams['sort_on'] = 'rank' - - if searchParams.get('from_name'): - from_name = '%' + searchParams.get('from_name') + '%' - else: - from_name = '' - -class MySQLSearch(ISearch): - - def __init__(self, mmobj): - self.mmobj = mmobj - - def clearIndex(self): - pass - - def addMessageToIndex(self, message_id, ticket_id, ticket, msg, body, html_body, from_name, msg_date): - self.sql.addToMySQLSearchIndex(message_id=message_id, - ticket_id=ticket_id, - body=body, - html_body=body) - - def searchIndex(self, searchParams): - if searchParams.get('from_name'): - from_name = '%' + searchParams.get('from_name') + '%' - else: - from_name = '' - - -class XapianSearch: +class XapianSearchBase: """ A search class that uses Xapian - the open source search engine for very fast retrievel of documents. @@ -118,7 +39,7 @@ database into a special index used by Xapian for """ - def __init__(self, databasePath, mmobj): + def __init__(self, databasePath): """ Creates a new xapian search object @@ -128,8 +49,6 @@ @param mmobj: a Mailmanager instance @type mmobj: MailManager """ - - self.mmobj = mmobj self.databasePath = databasePath def clearIndex(self): @@ -146,7 +65,7 @@ os.rmdir(os.path.join(root, name)) except Exception, e: log('%sunable to clear database at %s' \ - % (self.mmobj.getLogName(), self.databasePath),\ + % (self.getLogName(), self.databasePath),\ logging.INFO, 'search.indexing') @@ -180,7 +99,7 @@ """ log('%s updating index for ticket %s message %s'\ - % (self.mmobj.getLogName(), str(ticket_id), str(message_id)),\ + % (self.getLogName(), str(ticket_id), str(message_id)),\ logging.INFO, 'search.indexing') try: @@ -188,11 +107,11 @@ xapian.DB_CREATE_OR_OPEN) except Exception, e: log('%s failed to get lock on database for writing'\ - % (self.mmobj.getLogName()),\ + % (self.getLogName()),\ logging.DEBUG, 'search.indexing') - fname, femail = self.mmobj.getEmailAddrPair(msg, 'from') - subject = self.mmobj.getHeader(msg, 'subject') + fname, femail = self.getEmailAddrPair(msg, 'from') + subject = self.getHeader(msg, 'subject') doc = xapian.Document() @@ -201,34 +120,34 @@ doc.add_term('XACID' + ticket.account_id.lower().encode('utf-8')) log('%s==> indexing account id %s' \ - % (self.mmobj.getLogName(), ticket.account_id.lower()),\ + % (self.getLogName(), ticket.account_id.lower()),\ logging.INFO, 'search.indexing') doc.add_term('XASN' + ticket.assigned.lower().encode('utf-8')) log('%s==> indexing assigned %s'\ - % (self.mmobj.getLogName(), ticket.assigned.lower().encode('utf-8')),\ + % (self.getLogName(), ticket.assigned.lower().encode('utf-8')),\ logging.INFO, 'search.indexing') doc.add_term('XST' + ticket.state.lower().encode('utf-8')) log('%s==> indexing state %s'\ - % (self.mmobj.getLogName(), ticket.state.lower().encode('utf-8')),\ + % (self.getLogName(), ticket.state.lower().encode('utf-8')),\ logging.INFO, 'search.indexing') doc.add_term( 'XFE' + femail.lower().encode('utf-8') ) log('%s==> indexing from email %s'\ - % (self.mmobj.getLogName(), femail.lower()),\ + % (self.getLogName(), femail.lower()),\ logging.INFO, 'search.indexing') doc.add_term( 'XPRI' + str(ticket.priority)) log('%s==> indexing priority %s'\ - % (self.mmobj.getLogName(), str(ticket.priority)),\ + % (self.getLogName(), str(ticket.priority)),\ logging.INFO, 'search.indexing') i = 1 for subcat in ticket.category0.split(): doc.add_posting('XCAT0' + subcat.lower().encode('utf-8'), i) log('%s==> indexing category 0 %s'\ - % (self.mmobj.getLogName(), subcat.lower()),\ + % (self.getLogName(), subcat.lower()),\ logging.INFO, 'search.indexing') i += 1 @@ -236,7 +155,7 @@ for subcat in ticket.category1.split(): doc.add_posting('XCAT1' + subcat.lower().encode('utf-8'), i) log('%s==> indexing category 1 %s'\ - % (self.mmobj.getLogName(), subcat.lower()),\ + % (self.getLogName(), subcat.lower()),\ logging.INFO, 'search.indexing') i += 1 @@ -244,7 +163,7 @@ for subcat in ticket.category2.split(): doc.add_posting('XCAT2' + subcat.lower().encode('utf-8'), i) log('%s==> indexing category 2 %s'\ - % (self.mmobj.getLogName(), subcat.lower()),\ + % (self.getLogName(), subcat.lower()),\ logging.INFO, 'search.indexing') i += 1 @@ -252,7 +171,7 @@ for subname in fname.split(): doc.add_posting('XFN' + subname.lower().encode('utf-8'), i) log('%s==> indexing from name %s'\ - % (self.mmobj.getLogName(), subname.lower()),\ + % (self.getLogName(), subname.lower()),\ logging.INFO, 'search.indexing') i += 1 @@ -260,7 +179,7 @@ for term in subject.split(): doc.add_posting('XSB' + term.lower().encode('utf-8'), i) log('%s==> indexing subject %s'\ - % (self.mmobj.getLogName(), term.lower()),\ + % (self.getLogName(), term.lower()),\ logging.INFO, 'search.indexing') i += 1 @@ -268,7 +187,7 @@ for term in body.split(): doc.add_posting('XBY' + term.lower().encode('utf-8'), i) log('%s==> indexing body %s'\ - % (self.mmobj.getLogName(), term.lower()),\ + % (self.getLogName(), term.lower()),\ logging.INFO, 'search.indexing') i += 1 @@ -278,24 +197,24 @@ doc.add_term('XFD' + year + month + day) log('%s==> indexing from date %s'\ - % (self.mmobj.getLogName(), year + month + day),\ + % (self.getLogName(), year + month + day),\ logging.INFO, 'search.indexing') doc.add_term('XDYDM' + year + month) log('%s==> indexing date year and month %s'\ - % (self.mmobj.getLogName(), year + month),\ + % (self.getLogName(), year + month),\ logging.INFO, 'search.indexing') doc.add_term('XDY' + year) log('%s==> indexing date year %s'\ - % (self.mmobj.getLogName(), year),\ + % (self.getLogName(), year),\ logging.INFO, 'search.indexing') try: database.add_document(doc) except Exception,e: log('%s unable to add document to database'\ - % (self.mmobj.getLogName()),\ + % (self.getLogName()),\ logging.DEBUG, 'searching.indexing') def searchIndex(self, searchParams): @@ -313,7 +232,7 @@ sb = xapian.Database(self.databasePath) except Exception, e: log('%s unable to access to xapian database'\ - % (self.mmobj.getLogName()),\ + % (self.getLogName()),\ logging.DEBUG, 'searching.indexing') @@ -397,7 +316,7 @@ + " " log("%s raw query is %s"\ - % (self.mmobj.getLogName(), query),\ + % (self.getLogName(), query),\ logging.INFO, 'searching.searching') query = query.strip() @@ -419,14 +338,14 @@ log("%s performing query '%s'"\ - % (self.mmobj.getLogName(), xapianquery.get_description()),\ + % (self.getLogName(), xapianquery.get_description()),\ logging.INFO, 'searching.searching') e.set_query(xapianquery) matches = e.get_mset(0,100) log("%s number of matches '%s'"\ - % (self.mmobj.getLogName(), str(matches.get_matches_estimated())),\ + % (self.getLogName(), str(matches.get_matches_estimated())),\ logging.INFO, 'searching.searching') results = [] @@ -437,7 +356,7 @@ score = match[xapian.MSET_PERCENT] log("%s message: %s ticket: %s score: %f"\ - % (self.mmobj.getLogName(), message_id, ticket_id, score),\ + % (self.getLogName(), message_id, ticket_id, score),\ logging.INFO, 'searching.searching') if not ticket_id in tickets: @@ -455,18 +374,18 @@ from current messages in the relational database. """ log("%s Rebuilding search index %s"\ - % (self.mmobj.getLogName(), self.databasePath),\ + % (self.getLogName(), self.databasePath),\ logging.INFO, 'searching.searching') print "k" - messages = self.mmobj.sql.listMessages() + messages = self.sql.listMessages() self.clearIndex() print "And..." - messages = self.mmobj.sql.listMessages() + messages = self.sql.listMessages() print "ik" @@ -477,7 +396,7 @@ fname = message.from_name log("%s adding message"\ - % (self.mmobj.getLogName()), + % (self.getLogName()), logging.INFO, 'searching.searching') self.addMessageToIndex(message_id, @@ -488,3 +407,39 @@ html_body=message.html_body, from_name=fname, msg_date=message.msg_date) + + + + +class XapianSearch(OFS.SimpleItem.Item, Persistent, + OFS.PropertyManager.PropertyManager, + Acquisition.Implicit, + AccessControl.Role.RoleManager, + XapianSearchBase): + """ Zope Integration + + """ + meta_type = 'XapianSearch' + + _properties = ({'id': 'title', 'type': 'string', 'mode': 'w'}) + + # Security. + __ac_roles__ = ('Tickets', 'Reports', 'Settings', 'Customer') + security = ClassSecurityInfo() + security.setPermissionDefault('MailManager View Tickets', + ['Tickets', 'Customer', 'Settings']) + security.setPermissionDefault('MailManager Settings', ['Settings']) + + security.declareProtected('MailManager View Tickets', '') + + manage_options = ( + {'label': 'Contents', 'action': 'manage_main'}, + {'label': 'Security', 'action': 'manage_access'}, + {'label': 'Properties', 'action': 'manage_propertiesForm'}, + ) + + def __init__(self, id, title, searchindexroot): + self.id = id + self.title = title + XapianSearchBase.__init__(self, searchindexroot) + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |