attsearch action

This action creates a list of all attachments with our needle-value. There is some code borrowed from wikiutil.pys full-search too...

   1 """
   2     MoinMoin - Attachment Text Search
   3     Based on the Search action parts (wikiaction.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 find words in an attachment
  10 
  11     $Id$
  12 """
  13 
  14 from string import *
  15 import os
  16 import re
  17 
  18 from MoinMoin import config, wikiutil, webapi
  19 from MoinMoin.Page import Page
  20 
  21 def attSearcher(needle):
  22     needle = lower(needle)              # lowercase only!
  23     hits = []
  24     fcount = 0
  25     
  26     needle_re = re.compile(needle)
  27     
  28     for page_name in wikiutil.getPageList(config.text_dir):
  29         path = os.path.join(config.data_dir, "pages")
  30         path = os.path.join(path, wikiutil.quoteFilename(page_name))
  31         path = os.path.join(path, "searchcache")
  32         
  33         if os.path.exists(path):
  34             for attachment in wikiutil.getPageList(path):
  35                 """ """
  36                 # print page_name + ": " +  attachment + "<br>"
  37                 count = 0
  38                 fragment = ""
  39                 try:
  40                     myfile=open(os.path.join(path, attachment))
  41                     
  42                     for i in myfile.readlines():
  43                         if find(i, needle) != -1:
  44                             fcount += 1
  45                             count += 1
  46                             fragment = fragment + " " + i
  47 
  48                     myfile.close()
  49                     if count > 0:
  50                         hits.append((count, page_name, attachment, fragment))
  51                 except:
  52                     """This is a dummy"""
  53                 
  54     return(fcount, hits)
  55 
  56 def execute(pagename, request,fieldname='value'):
  57     _ = request.getText
  58     webapi.http_headers(request)
  59 
  60     if request.form.has_key(fieldname):
  61         needle = request.form[fieldname].value
  62     else:
  63         needle = ''
  64 
  65     # check for sensible search term
  66     
  67     if len(needle) < 1:
  68         Page(pagename).send_page(request,
  69                                  msg=_("<b>Please use a more selective search term instead of '%(needle)s'!</b>") % locals())
  70         return
  71 
  72     # send title
  73 
  74     wikiutil.send_title(request, _('Full text search for "%s"') % (needle,))
  75 
  76     pagecount, hits = attSearcher(needle)
  77 
  78     # print the result
  79 
  80     print pagecount, "Hits: <br>"
  81     print "<UL>"
  82     for (count, page_name, attachment, fragment) in hits:
  83         print '<LI>' + Page(page_name).link_to(querystr='action=AttachFile&do=get&target=%s' % attachment, text="%s: %s" % (page_name, attachment))
  84         print "<br>", "&nbsp;" * 8, " . . . . "
  85         print "(", count , ")", fragment[:40], "..."
  86     print "</UL>"
  87 
  88     wikiutil.send_footer(request, pagename)

MoinMoin: Thomas_Renard/AttSearchTool/attsearch (last edited 2007-10-29 19:12:52 by localhost)