Attachment 'farmfullsearch2_macro_DL.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - FarmFullSearch Macro
   4 
   5     [[FarmFullSearch]]
   6         displays a search dialog, as it always did.
   7 
   8     [[FarmFullSearch(Help)]]
   9         embeds a search result into a page, as if you entered
  10         'Help' into the search box.
  11 
  12     @copyright: 2006 by Oliver Siemoneit
  13     @license: GNU GPL, see COPYING for details.
  14 
  15     FarmFullSearch is heavily based on FullSearch
  16     @copyright: 2000-2004 by Jürgen Hermann <jh@web.de>
  17     @license: GNU GPL, see COPYING for details.
  18 
  19     Changes:
  20     * 2006-11-18 (DavidLinke) Handle failure of xmlrpc request gracefully
  21                  Failure is expected e.g. when authentication is required.
  22 """
  23 
  24 import re, time
  25 import xmlrpclib
  26 from MoinMoin import config, wikiutil, search
  27 from farmconfig import wikis
  28 
  29 Dependencies = ["pages"]
  30 
  31 def execute(macro, needle):
  32     request = macro.request
  33     _ = request.getText
  34 
  35     # If no args given, invoke "classic" behavior
  36     # Does not work yet since action=farmfullsearch is missing
  37     if needle is None:
  38         return searchbox(macro)
  39 
  40     # With empty arguments, show error message
  41     elif needle == '':
  42         err = err = _('Please use a more selective search term instead of '
  43                 '{{{"%s"}}}') %  needle
  44         return '<span class="error">%s</span>' % err
  45 
  46     # With whitespace argument, show error message like the one used in the search box
  47     elif needle.isspace():
  48         err = _('Please use a more selective search term instead of '
  49                 '{{{"%s"}}}') %  needle
  50         return '<span class="error">%s</span>' % err
  51 
  52     needle = needle.strip()
  53 
  54     # Search wiki farm and return the results
  55     return searchfarm(macro, needle)
  56 
  57 
  58 def searchfarm(macro, args):
  59     request = macro.request
  60     _ = request.getText
  61     output = ""
  62     searchterm = args
  63 
  64     for wiki in wikis:
  65         searchresults = []
  66         #Code partly taken from wikilist.py by TheAnarcat!
  67         name = wiki[0]
  68         url = wiki[1]
  69         from re import sub
  70         # XXX, hack: transform the regexp into a canonical url
  71         # we need canonical urls in the config for this to be clean
  72         # look for (foo|bar) and replace with foo
  73         url = sub('\(([^\|]*)(\|[^\)]*\))+', '\\1', url)
  74         # remove common regexp patterns and slap a protocol to make this a real url
  75         url = 'http://' + sub('[\^\$]|(\.\*)', '', url)
  76         # for farmsearch we additionally need a slash at the end
  77         url = url + '/'
  78 
  79         # Set the url for the remote farm wiki here. Note: Slash at the end is needed
  80         #url = "http://wikifarm.koumbit.net/"
  81         srcwiki = xmlrpclib.ServerProxy(url + '?action=xmlrpc2')
  82 
  83         start = time.time()
  84         try:
  85             searchresults = srcwiki.searchPages(searchterm)
  86             # This is a bad and time consuming solution just to get the number of pages searched
  87             allpages = srcwiki.getAllPages()
  88         except xmlrpclib.ProtocolError:
  89             output += macro.formatter.paragraph(1)
  90             output += macro.formatter.text('Failed to perform search in %s' %
  91                                            url)
  92             output += macro.formatter.paragraph(0)
  93             continue
  94 
  95         output += macro.formatter.paragraph(1)
  96         #output += macro.formatter.text(_("%s (%d results)") %(url, len(searchresults)))
  97         output += macro.formatter.strong(1)
  98         output += macro.formatter.text(name)
  99         output += macro.formatter.strong(0)
 100         output += macro.formatter.linebreak(0)
 101         output += macro.formatter.text(_("%(hits)d results out of about %(pages)d pages.") %
 102                        {'hits': len(searchresults), 'pages': len(allpages)})
 103         elapsed = time.time() - start
 104         output += macro.formatter.text(u' (%s)' % macro.formatter.text(_("%.2f seconds") % elapsed))
 105 
 106         output += macro.formatter.paragraph(0)
 107 
 108 
 109         for searchresult in searchresults:
 110 
 111             output += macro.formatter.div(1, css_class='searchresults')
 112             output += macro.formatter.definition_list(1)
 113 
 114             output += macro.formatter.definition_term(1)
 115             output += macro.formatter.url(1, url + searchresult[0] + '?highlight=' + searchterm)
 116             output += macro.formatter.text(searchresult[0])
 117             output += macro.formatter.url(0)
 118             output += macro.formatter.definition_term(0)
 119 
 120             output += macro.formatter.definition_desc(1)
 121             output += macro.formatter.small(1)
 122             output += macro.formatter.rawHTML(searchresult[1])
 123             output += macro.formatter.small(0)
 124             output += macro.formatter.definition_desc(0)
 125 
 126             output += macro.formatter.definition_list(0)
 127             output += macro.formatter.span(0)
 128 
 129         output += macro.formatter.linebreak(0)
 130         output += macro.formatter.linebreak(0)
 131         output += macro.formatter.linebreak(0)
 132 
 133     return output
 134 
 135 
 136 
 137 def searchbox(self):
 138         """ Make a search box (taken from wikimacro.py)
 139 
 140 
 141         @rtype: unicode
 142         @return: search box html fragment
 143         """
 144         _ = self._
 145         if self.form.has_key('value'):
 146             default = wikiutil.escape(self.form["value"][0], quote=1)
 147         else:
 148             default = ''
 149 
 150         button = _("Search Text")
 151 
 152         html = [
 153             u'<form method="get" action="">',
 154             u'<div>',
 155             u'<input type="hidden" name="action" value="farmfullsearch">',
 156             u'<input type="hidden" name="titlesearch" value="fullsearch">',
 157             u'<input type="text" name="value" size="30" value="%s">' % default,
 158             u'<input type="submit" value="%s">' % button,
 159             u'</div>',
 160             u'</form>',
 161             ]
 162         html = u'\n'.join(html)
 163         return self.formatter.rawHTML(html)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-11-17 19:56:31, 4.3 KB) [[attachment:a_farmfullsearch.py]]
  • [get | view] (2006-11-17 19:56:51, 4.4 KB) [[attachment:a_farmfullsearch2.py]]
  • [get | view] (2006-11-16 20:53:26, 5.1 KB) [[attachment:farmfullsearch.py]]
  • [get | view] (2006-11-17 13:55:51, 5.3 KB) [[attachment:farmfullsearch2.py]]
  • [get | view] (2006-11-18 16:45:04, 4.9 KB) [[attachment:farmfullsearch2_action_DL.py]]
  • [get | view] (2006-11-18 16:44:37, 5.6 KB) [[attachment:farmfullsearch2_macro_DL.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.