AttSearch Macro

This macro creates a search form for the Attachment Search Tool. There will be very much overhead (which is not working yet :( )- I borrowed the full-search code from wikimacro.py...

   1 """
   2     MoinMoin - Attachment Text Search
   3     Based on the Search macro parts (wikimacro.py) 
   4     by Jürgen Hermann <jh@web.de>
   5 
   6     Copyright (c) 2003 Thomas Renard <CyBaer42@web.de>
   7     All rights reserved, see COPYING for details.
   8 
   9     This action allows you to rename a page.
  10 
  11     $Id$
  12 """
  13 
  14 import re, urllib
  15 from MoinMoin import config, user, wikiutil, macro
  16 from MoinMoin.Page import Page 
  17 
  18 _args_re_pattern = r'((?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))|'
  19 
  20 def execute(macro, text, args_re=re.compile(_args_re_pattern)):
  21     _ = macro.request.getText
  22 
  23     # if no args are given - this is working yet!
  24     if text is None:
  25         return pdfsearch("attsearch", macro)
  26 
  27     # The following is draft and may not work! A call with parameter may go crazy!
  28 
  29     args = args_re.match(text)
  30 
  31     if not args:
  32         return '<p><strong class="error">Invalid AttachSearch arguments "%s"!</strong></p>' % (text,)
  33 
  34     needle = args.group('htext')
  35 
  36     if not needle:
  37         # empty args means to duplicate the "title click" search (backlinks to page),
  38         # especially useful on "Category" type pages
  39         needle = macro.formatter.page.page_name
  40 
  41         # do the search
  42         pagecount, hits = attSearcher(needle)
  43 
  44         # get the result
  45 
  46         result = pagecount, "%d Hits: <br><UL>" % pagecount
  47 
  48         for (count, page_name, attachment, fragment) in hits:
  49             result += '<LI>' + Page(page_name).link_to(querystr='action=AttachFile&do=get&target=%s' % attachment, text="%s: %s" % (page_name, attachment))
  50             result += "<br>" + "&nbsp;" * 8 + " . . . . "
  51             result += "(", count , ")" +  fragment[:40] + "..."
  52             result += "</UL>"
  53 
  54         return result
  55     
  56 def pdfsearch(type, macro):
  57     import cgi
  58 
  59     if macro.form.has_key('value'):
  60        default = macro.form["value"].value
  61     else:
  62        default = ''
  63     
  64     boxes = ''
  65     
  66     return macro.formatter.rawHTML((
  67         '<form method="GET">'
  68         '<input type="hidden" name="action" value="%s">'
  69         '<input name="value" size="30" value="%s">&nbsp;'
  70         '<input type="submit" value="%s">'
  71         '%s</form>') % (type, cgi.escape(default, quote=1),
  72                         macro._("Go"), boxes))
  73 
  74 # This is copied from attsearch.py. Hm, where is the place for a
  75 # global function?
  76 
  77 def attSearcher(needle):
  78     needle = lower(needle)              # lowercase only!
  79     hits = []
  80     fcount = 0
  81     
  82     needle_re = re.compile(needle)
  83     
  84     for page_name in wikiutil.getPageList(config.text_dir):
  85         path = os.path.join(config.data_dir, "pages")
  86         path = os.path.join(path, wikiutil.quoteFilename(page_name))
  87         path = os.path.join(path, "searchcache")
  88         
  89         if os.path.exists(path):
  90             for attachment in wikiutil.getPageList(path):
  91                 """ """
  92                 # print page_name + ": " +  attachment + "<br>"
  93                 count = 0
  94                 fragment = ""
  95                 try:
  96                     myfile=open(os.path.join(path, attachment))
  97                     
  98                     for i in myfile.readlines():
  99                         if find(i, needle) != -1:
 100                             fcount += 1
 101                             count += 1
 102                             fragment = fragment + " " + i
 103 
 104                     myfile.close()
 105                     if count > 0:
 106                         hits.append((count, page_name, attachment, fragment))
 107                 except:
 108                     """This is a dummy"""
 109                 
 110     return(fcount, hits)

MoinMoin: Thomas_Renard/AttSearchTool/AttachSearch (last edited 2007-10-29 19:21:25 by localhost)