| 1 |
## Script (Python) "referencebrowser_queryCatalog"
|
| 2 |
##bind container=container
|
| 3 |
##bind context=context
|
| 4 |
##bind namespace=
|
| 5 |
##bind script=script
|
| 6 |
##bind subpath=traverse_subpath
|
| 7 |
##parameters=REQUEST=None,show_all=0,quote_logic=0,quote_logic_indexes=['SearchableText'],search_catalog=None
|
| 8 |
##title=wraps the portal_catalog with a rules qualified query
|
| 9 |
##
|
| 10 |
from ZODB.POSException import ConflictError
|
| 11 |
from Products.ZCTextIndex.ParseTree import ParseError
|
| 12 |
|
| 13 |
results=[]
|
| 14 |
if not search_catalog: search_catalog = 'portal_catalog'
|
| 15 |
catalog=getattr(context, search_catalog, context.portal_catalog)
|
| 16 |
if not hasattr(catalog, 'searchResults'):
|
| 17 |
catalog= context.portal_catalog
|
| 18 |
|
| 19 |
indexes=catalog.indexes()
|
| 20 |
query={}
|
| 21 |
show_query=show_all
|
| 22 |
second_pass = {}
|
| 23 |
|
| 24 |
if REQUEST is None:
|
| 25 |
REQUEST = context.REQUEST
|
| 26 |
|
| 27 |
def quotestring(s):
|
| 28 |
return '"%s"' % s
|
| 29 |
|
| 30 |
def quotequery(s):
|
| 31 |
if not s:
|
| 32 |
return s
|
| 33 |
try:
|
| 34 |
terms = s.split()
|
| 35 |
except ConflictError:
|
| 36 |
raise
|
| 37 |
except:
|
| 38 |
return s
|
| 39 |
tokens = ('OR', 'AND', 'NOT')
|
| 40 |
s_tokens = ('OR', 'AND')
|
| 41 |
check = (0, -1)
|
| 42 |
for idx in check:
|
| 43 |
if terms[idx].upper() in tokens:
|
| 44 |
terms[idx] = quotestring(terms[idx])
|
| 45 |
for idx in range(1, len(terms)):
|
| 46 |
if (terms[idx].upper() in s_tokens and
|
| 47 |
terms[idx-1].upper() in tokens):
|
| 48 |
terms[idx] = quotestring(terms[idx])
|
| 49 |
return ' '.join(terms)
|
| 50 |
|
| 51 |
for k, v in REQUEST.items():
|
| 52 |
if v and k in indexes:
|
| 53 |
if quote_logic and k in quote_logic_indexes:
|
| 54 |
v = quotequery(v)
|
| 55 |
query.update({k:v})
|
| 56 |
show_query=1
|
| 57 |
elif k.endswith('_usage'):
|
| 58 |
key = k[:-6]
|
| 59 |
param, value = v.split(':')
|
| 60 |
second_pass[key] = {param:value}
|
| 61 |
elif k in ('sort_on', 'sort_order', 'sort_limit'):
|
| 62 |
query.update({k:v})
|
| 63 |
|
| 64 |
for k, v in second_pass.items():
|
| 65 |
qs = query.get(k)
|
| 66 |
if qs is None:
|
| 67 |
continue
|
| 68 |
query[k] = q = {'query':qs}
|
| 69 |
q.update(v)
|
| 70 |
|
| 71 |
# doesn't normal call catalog unless some field has been queried
|
| 72 |
# against. if you want to call the catalog _regardless_ of whether
|
| 73 |
# any items were found, then you can pass show_all=1.
|
| 74 |
|
| 75 |
if show_query:
|
| 76 |
try:
|
| 77 |
results=catalog(query)
|
| 78 |
except ParseError:
|
| 79 |
pass
|
| 80 |
|
| 81 |
return results
|