Back to index

moin  1.9.0~rc2
AdvancedSearch.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - AdvancedSearch Macro
00004 
00005     <<AdvancedSearch>>
00006         displays advanced search dialog.
00007 
00008     @license: GNU GPL, see COPYING for details.
00009 """
00010 
00011 from MoinMoin import wikiutil
00012 from MoinMoin.i18n import languages
00013 from MoinMoin.widget import html
00014 from MoinMoin.util.web import makeSelection
00015 from MoinMoin.support.python_compatibility import sorted
00016 import mimetypes
00017 
00018 Dependencies = ['pages']
00019 
00020 def getMimetypes():
00021     # The types will be listed here, instead of parsing mimetypes.types_map
00022     types = [
00023         'application',
00024         'audio',
00025         'image',
00026         'message',
00027         'text',
00028         'video',
00029         ]
00030     return types
00031 
00032 
00033 def getCategories(request):
00034     # This will return all pages with "Category" in the title
00035     cat_filter = request.cfg.cache.page_category_regexact.search
00036     pages = request.rootpage.getPageList(filter=cat_filter)
00037     pages.sort()
00038     return pages
00039 
00040 
00041 def form_get(request, name, default='', escaped=False):
00042     """ Fetches a form field
00043 
00044     @param request: current request
00045     @param name: name of the field
00046     @param default: value if not present (default: '')
00047     @param escaped: if True, escape value so it can be used for html generation (default: False)
00048     """
00049     value = request.values.get(name, default)
00050     if escaped:
00051         value = wikiutil.escape(value, quote=True)
00052     return value
00053 
00054 
00055 def advanced_ui(macro):
00056     """ Returns the code for the advanced search user interface
00057 
00058     @param macro: current macro instance
00059     """
00060     _ = macro._
00061     f = macro.formatter
00062     request = macro.request
00063 
00064     disabledIfMoinSearch = not request.cfg.xapian_search and \
00065             ' disabled="disabled"' or ''
00066 
00067     search_boxes = ''.join([
00068         f.table_row(1),
00069         f.table_cell(1, attrs={'rowspan': '5', 'class': 'searchfor'}),
00070         f.text(_('Search for items')),
00071         f.table_cell(0),
00072         ''.join([''.join([
00073             f.table_row(1),
00074             f.table_cell(1),
00075             f.text(txt),
00076             f.table_cell(0),
00077             f.table_cell(1),
00078             f.rawHTML(input_field),
00079             f.table_cell(0),
00080             f.table_row(0),
00081         ]) for txt, input_field in (
00082             (_('containing all the following terms'),
00083                 '<input type="text" name="and_terms" size="30" value="%s">'
00084                 % (form_get(request, 'and_terms', escaped=True) or form_get(request, 'value', escaped=True))),
00085             (_('containing one or more of the following terms'),
00086                 '<input type="text" name="or_terms" size="30" value="%s">'
00087                 % form_get(request, 'or_terms', escaped=True)),
00088             (_('not containing the following terms'),
00089                 '<input type="text" name="not_terms" size="30" value="%s">'
00090                 % form_get(request, 'not_terms', escaped=True)),
00091             #('containing only one of the following terms',
00092             #    '<input type="text" name="xor_terms" size="30" value="%s">'
00093             #    % form_get(request, 'xor_terms', escaped=True)),
00094             # TODO: dropdown-box?
00095             (_('last modified since (e.g. 2 weeks before)'),
00096                 '<input type="text" name="mtime" size="30" value="%s">'
00097                 % form_get(request, 'mtime', escaped=True)),
00098         )])
00099     ])
00100 
00101     # category selection
00102     categories = form_get(request, 'categories')
00103     c_select = makeSelection('categories',
00104             [('', _('any category'))] +
00105             [(cat, '%s' % cat) for cat in getCategories(request)],
00106             categories, 3, True)
00107 
00108     # language selection
00109     searchedlang = form_get(request, 'language')
00110     langs = dict([(lang, lmeta['x-language-in-english'])
00111         for lang, lmeta in languages.items()])
00112     userlang = macro.request.lang
00113     lang_select = makeSelection('language',
00114             [('', _('any language')),
00115             (userlang, langs[userlang])] + sorted(langs.items(), key=lambda i: i[1]),
00116             searchedlang, 3, True)
00117 
00118     # mimetype selection
00119     mimetype = form_get(request, 'mimetype')
00120     mt_select = makeSelection('mimetype',
00121             [('', _('any mimetype'))] +
00122             [(type, 'all %s files' % type) for type in getMimetypes()] +
00123             [(m[1], '*%s - %s' % m) for m in sorted(mimetypes.types_map.items())],
00124             mimetype, 3, True)
00125 
00126     # misc search options (checkboxes)
00127     search_options = ''.join([
00128         ''.join([
00129             f.table_row(1),
00130             f.table_cell(1, attrs={'class': 'searchfor'}),
00131             txt[0],
00132             f.table_cell(0),
00133             f.table_cell(1, colspan=2),
00134             unicode(txt[1]),
00135             txt[2],
00136             f.table_cell(0),
00137             f.table_row(0),
00138             ]) for txt in (
00139                 (_('Categories'), unicode(c_select), ''),
00140                 (_('Language'), unicode(lang_select), ''),
00141                 (_('File Type'), unicode(mt_select), ''),
00142                 ('', html.INPUT(type='checkbox', name='titlesearch',
00143                     value='1', checked=form_get(request, 'titlesearch', escaped=True),
00144                     id='titlesearch'),
00145                     '<label for="titlesearch">%s</label>' % _('Search only in titles')),
00146                 ('', html.INPUT(type='checkbox', name='case', value='1',
00147                     checked=form_get(request, 'case', escaped=True),
00148                     id='case'),
00149                     '<label for="case">%s</label>' % _('Case-sensitive search')),
00150                 ('', html.INPUT(type='checkbox', name='excludeunderlay',
00151                     value='1', checked=form_get(request, 'excludeunderlay', escaped=True),
00152                     id='excludeunderlay'),
00153                     '<label for="excludeunderlay">%s</label>' % _('Exclude underlay')),
00154                 ('', html.INPUT(type='checkbox', name='nosystemitems',
00155                     value='1', checked=form_get(request, 'nosystemitems', escaped=True),
00156                     id='nosystempages'),
00157                     '<label for="nosystempages">%s</label>' % _('No system items')),
00158                 ('', html.INPUT(type='checkbox', name='historysearch',
00159                     value='1', checked=form_get(request, 'historysearch', escaped=True),
00160                     disabled=(not request.cfg.xapian_search or
00161                         not request.cfg.xapian_index_history),
00162                     id='historysearch'),
00163                     '<label for="historysearch">%s</label>' % _('Search in all page revisions'))
00164             )
00165     ])
00166 
00167     # the dialogue
00168     return f.rawHTML('\n'.join([
00169         u'<form method="get" action="%s">' % macro.request.href(macro.request.formatter.page.page_name),
00170         u'<div>',
00171         u'<input type="hidden" name="action" value="fullsearch">',
00172         u'<input type="hidden" name="advancedsearch" value="1">',
00173         f.table(1, attrs={'tableclass': 'advancedsearch'}),
00174         search_boxes,
00175         search_options,
00176         f.table_row(1),
00177         f.table_cell(1, attrs={'class': 'submit', 'colspan': '3'}),
00178         u'<input type="submit" value="%s">' % _('Go get it!'),
00179         f.table_cell(0),
00180         f.table_row(0),
00181         f.table(0),
00182         u'</div>',
00183         u'</form>',
00184     ]))
00185 
00186 
00187 def execute(macro, needle):
00188     # for now, just show the advanced ui
00189     return advanced_ui(macro)
00190