Attachment 'farmfullsearch2.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 
  20 import re, time
  21 import xmlrpclib
  22 from MoinMoin import config, wikiutil, search
  23 
  24 Dependencies = ["pages"]
  25 
  26 def execute(macro, needle):
  27     request = macro.request
  28     _ = request.getText
  29 
  30     # If no args given, invoke "classic" behavior
  31     # Does not work yet since action=farmfullsearch is missing
  32     if needle is None:
  33         return searchbox(macro)
  34 
  35     # With empty arguments, show error message
  36     elif needle == '':
  37         err = err = _('Please use a more selective search term instead of '
  38                 '{{{"%s"}}}') %  needle
  39         return '<span class="error">%s</span>' % err
  40 
  41     # With whitespace argument, show error message like the one used in the search box
  42     elif needle.isspace():
  43         err = _('Please use a more selective search term instead of '
  44                 '{{{"%s"}}}') %  needle
  45         return '<span class="error">%s</span>' % err
  46 
  47     needle = needle.strip()
  48 
  49     # Search wiki farm and return the results
  50     return searchfarm(macro, needle)
  51 
  52 
  53 def searchfarm(macro, args):
  54     from farmconfig import wikis
  55     request = macro.request
  56     _ = request.getText
  57     output = ""
  58     searchterm = args
  59 
  60     #Code partly taken from wikilist.py by TheAnarcat!
  61     for wiki in wikis:
  62         name = wiki[0]
  63         url = wiki[1]
  64         from re import sub
  65         # XXX, hack: transform the regexp into a canonical url
  66         # we need canonical urls in the config for this to be clean
  67         # look for (foo|bar) and replace with foo
  68         url = sub('\(([^\|]*)(\|[^\)]*\))+', '\\1', url)
  69         # remove common regexp patterns and slap a protocol to make this a real url
  70         url = 'http://' + sub('[\^\$]|(\.\*)', '', url)
  71         # for farmsearch we additionally need a slash at the end
  72         url = url + '/'
  73 
  74         # Set the url for the remote farm wiki here. Note: Slash at the end is needed
  75         #url = "http://wikifarm.koumbit.net/"
  76         srcwiki = xmlrpclib.ServerProxy(url + '?action=xmlrpc2')
  77     
  78         start = time.time()
  79         searchresults = srcwiki.searchPages(searchterm)
  80         # This is a bad and time consuming solution just to get the number of pages searched
  81         allpages = srcwiki.getAllPages()
  82 
  83         output += macro.formatter.paragraph(1)
  84         #output += macro.formatter.text(_("%s (%d results)") %(url, len(searchresults)))
  85         output += macro.formatter.strong(1)
  86         output += macro.formatter.text(name)
  87         output += macro.formatter.strong(0)
  88         output += macro.formatter.linebreak(0)
  89         output += macro.formatter.text(_("%(hits)d results out of about %(pages)d pages.") %
  90                        {'hits': len(searchresults), 'pages': len(allpages)})
  91         elapsed = time.time() - start
  92         output += macro.formatter.text(u' (%s)' % macro.formatter.text(_("%.2f seconds") % elapsed))
  93     
  94         output += macro.formatter.paragraph(0)
  95         
  96         
  97         for searchresult in searchresults:
  98     
  99             output += macro.formatter.div(1, css_class='searchresults')       
 100             output += macro.formatter.definition_list(1)
 101             
 102             output += macro.formatter.definition_term(1)
 103             output += macro.formatter.url(1, url + searchresult[0] + '?highlight=' + searchterm)
 104             output += macro.formatter.text(searchresult[0])
 105             output += macro.formatter.url(0)
 106             output += macro.formatter.definition_term(0)
 107         
 108             output += macro.formatter.definition_desc(1)
 109             output += macro.formatter.small(1)
 110             output += macro.formatter.rawHTML(searchresult[1])
 111             output += macro.formatter.small(0)
 112             output += macro.formatter.definition_desc(0)
 113         
 114             output += macro.formatter.definition_list(0)
 115             output += macro.formatter.span(0)
 116     
 117         output += macro.formatter.linebreak(0)
 118         output += macro.formatter.linebreak(0)
 119         output += macro.formatter.linebreak(0)
 120         
 121     return output
 122 
 123 
 124 
 125 def searchbox(self):
 126         """ Make a search box (taken from wikimacro.py)
 127 
 128         
 129         @rtype: unicode
 130         @return: search box html fragment
 131         """
 132         _ = self._
 133         if self.form.has_key('value'):
 134             default = wikiutil.escape(self.form["value"][0], quote=1)
 135         else:
 136             default = ''
 137 
 138         button = _("Search Text")
 139         
 140         html = [
 141             u'<form method="get" action="">',
 142             u'<div>',
 143             u'<input type="hidden" name="action" value="farmfullsearch">',
 144             u'<input type="hidden" name="titlesearch" value="fullsearch">',
 145             u'<input type="text" name="value" size="30" value="%s">' % default,
 146             u'<input type="submit" value="%s">' % button,
 147             u'</div>',
 148             u'</form>',    
 149             ]
 150         html = u'\n'.join(html)
 151         return self.formatter.rawHTML(html)
 152     

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.