Back to index

moin  1.9.0~rc2
FullSearch.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - FullSearch Macro
00004 
00005     <<FullSearch>>
00006         displays a search dialog, as it always did.
00007 
00008     <<FullSearch()>>
00009         does the same as clicking on the page title, only that
00010         the result is embedded into the page. note the '()' after
00011         the macro name, which is an empty argument list.
00012 
00013     <<FullSearch(Help)>>
00014         embeds a search result into a page, as if you entered
00015         'Help' into the search box.
00016 
00017     The macro creates a page list without context or match info, just
00018     like PageList macro. It does not make sense to have context in non
00019     interactive search, and this kind of search is used usually for
00020     Category pages, where we don't care about the context.
00021 
00022     TODO: If we need to have context for some cases, either we add a context argument,
00023           or make another macro that uses context, which may be easier to use.
00024 
00025     @copyright: 2000-2004 Juergen Hermann <jh@web.de>,
00026                 2006 MoinMoin:FranzPletz
00027     @license: GNU GPL, see COPYING for details.
00028 """
00029 
00030 from MoinMoin import wikiutil, search
00031 
00032 Dependencies = ["pages"]
00033 
00034 
00035 def search_box(type, macro):
00036     """ Make a search box
00037 
00038     Make both Title Search and Full Search boxes, according to type.
00039 
00040     @param type: search box type: 'titlesearch' or 'fullsearch'
00041     @rtype: unicode
00042     @return: search box html fragment
00043     """
00044     _ = macro._
00045     if 'value' in macro.request.values:
00046         default = wikiutil.escape(macro.request.values["value"], quote=1)
00047     else:
00048         default = ''
00049 
00050     # Title search settings
00051     boxes = ''
00052     button = _("Search Titles")
00053 
00054     # Special code for fullsearch
00055     if type == "fullsearch":
00056         boxes = [
00057             u'<br>',
00058             u'<input type="checkbox" name="context" value="160" checked="checked">',
00059             _('Display context of search results'),
00060             u'<br>',
00061             u'<input type="checkbox" name="case" value="1">',
00062             _('Case-sensitive searching'),
00063             ]
00064         boxes = u'\n'.join(boxes)
00065         button = _("Search Text")
00066 
00067     # Format
00068     type = (type == "titlesearch")
00069     html = [
00070         u'<form method="get" action="%s">' % macro.request.href(macro.request.formatter.page.page_name),
00071         u'<div>',
00072         u'<input type="hidden" name="action" value="fullsearch">',
00073         u'<input type="hidden" name="titlesearch" value="%i">' % type,
00074         u'<input type="text" name="value" size="30" value="%s">' % default,
00075         u'<input type="submit" value="%s">' % button,
00076         boxes,
00077         u'</div>',
00078         u'</form>',
00079         ]
00080     html = u'\n'.join(html)
00081     return macro.formatter.rawHTML(html)
00082 
00083 
00084 def execute(macro, needle):
00085     request = macro.request
00086     _ = request.getText
00087 
00088     # if no args given, invoke "classic" behavior
00089     if needle is None:
00090         return search_box("fullsearch", macro)
00091 
00092     # With empty arguments, simulate title click (backlinks to page)
00093     elif needle == '':
00094         needle = '"%s"' % macro.formatter.page.page_name
00095 
00096     # With whitespace argument, show error message like the one used in the search box
00097     # TODO: search should implement those errors message for clients
00098     elif needle.isspace():
00099         err = _('Please use a more selective search term instead of '
00100                 '{{{"%s"}}}', wiki=True) % needle
00101         return '<span class="error">%s</span>' % err
00102 
00103     needle = needle.strip()
00104 
00105     # Search the pages and return the results
00106     results = search.searchPages(request, needle, sort='page_name')
00107 
00108     return results.pageList(request, macro.formatter, paging=False)
00109 
00110