Attachment 'Despam.py'

Download

   1 import time, urllib
   2 
   3 from MoinMoin.logfile import editlog
   4 from MoinMoin.util.dataset import TupleDataset, Column
   5 from MoinMoin.widget.browser import DataBrowserWidget
   6 from MoinMoin import wikiutil, Page, PageEditor
   7 from MoinMoin.macro import RecentChanges
   8 from MoinMoin.formatter.text_html import Formatter
   9 
  10 def show_editors(request, pagename, timestamp):
  11     _ =  request.getText
  12     
  13     timestamp = int(timestamp * 1000000)
  14     log = editlog.EditLog(request)
  15     editors = {}
  16     pages = {}
  17     for line in log.reverse():
  18         if line.ed_time_usecs < timestamp:
  19             break
  20         
  21         if not request.user.may.read(line.pagename):
  22             continue
  23         
  24         editor = line.getEditor(request)
  25         if not pages.has_key(line.pagename):
  26             pages[line.pagename] = 1
  27             editors[editor] = editors.get(editor, 0) + 1
  28             
  29     editors = [(nr, editor) for editor, nr in editors.iteritems()]
  30     editors.sort()
  31 
  32     pg = Page.Page(request, pagename)
  33 
  34     dataset = TupleDataset()
  35     dataset.columns = [Column('editor', label=_("Editor"), align='left'),
  36                        Column('pages', label=_("Pages"), align='right'),
  37                        Column('link', label='', align='left')]
  38     for nr, editor in editors:
  39         dataset.addRow((editor, unicode(nr), pg.link_to(request, text=_("Select Author"), querystr="action=Despam&editor=%s" % urllib.quote_plus(editor))))
  40     
  41     table = DataBrowserWidget(request)
  42     table.setData(dataset)
  43     table.render()
  44 
  45 class tmp:
  46     pass
  47 
  48 def show_pages(request, pagename, editor, timestamp):
  49     _ =  request.getText
  50     
  51     timestamp = int(timestamp * 1000000)
  52     log = editlog.EditLog(request)
  53     lines = []
  54     pages = {}
  55     #  mimic macro object for use of RecentChanges subfunctions
  56     macro = tmp()
  57     macro.request = request
  58     macro.formatter = Formatter(request)
  59 
  60     request.write("<p>")
  61     for line in log.reverse():
  62         if line.ed_time_usecs < timestamp:
  63             break
  64         
  65         if not request.user.may.read(line.pagename):
  66             continue
  67         
  68         
  69         if not pages.has_key(line.pagename):
  70             pages[line.pagename] = 1
  71             if line.getEditor(request)==editor:
  72                 request.write(RecentChanges.format_page_edits(macro, [line], timestamp))
  73 
  74     request.write(
  75 '''
  76 </p>
  77 <p>
  78 <form method="post" action="%s/%s">
  79 <input type="hidden" name="action" value="Despam">
  80 <input type="hidden" name="editor" value="%s">
  81 <input type="submit" name="ok" value="%s">
  82 </form>
  83 </p>
  84 ''' % (request.getScriptname(),
  85        wikiutil.quoteWikinameURL(pagename),
  86        urllib.quote(editor),
  87        _("Revert all!")))
  88 
  89 def revert_page(request, pagename, editor):
  90     if not request.user.may.revert(pagename):
  91         return 
  92 
  93     log = editlog.EditLog(request, rootpagename=pagename)
  94 
  95     first = True
  96     rev = u"00000000"
  97     for line in log.reverse():
  98         if first:
  99             first = False
 100             if line.getEditor(request) != editor:
 101                 return
 102         else:
 103             if line.getEditor(request) != editor:
 104                 rev = line.rev
 105                 break
 106     oldpg = Page.Page(request, pagename, rev=int(rev))
 107     pg = PageEditor.PageEditor(request, pagename)
 108     try:
 109         savemsg = pg.saveText(oldpg.get_raw_body(), 0, extra=rev,
 110                               action="SAVE/REVERT")
 111     except pg.SaveError, msg:
 112         # msg contain a unicode string
 113         savemsg = unicode(msg)
 114     return savemsg
 115     
 116 def revert_pages(request, editor, timestamp):
 117     _ =  request.getText
 118 
 119     editor = urllib.unquote(editor)
 120     timestamp = int(timestamp * 1000000)
 121     log = editlog.EditLog(request)
 122     pages = {}
 123     messages = []
 124     for line in log.reverse():
 125         if line.ed_time_usecs < timestamp:
 126             break
 127 
 128         if not request.user.may.read(line.pagename):
 129             continue
 130 
 131 
 132         if not pages.has_key(line.pagename):
 133             pages[line.pagename] = 1
 134             if line.getEditor(request) == editor:
 135                 msg = revert_page(request, line.pagename, editor)
 136                 if msg:
 137                     request.write("<p>%s: %s</p>" % (
 138                         Page.Page(request, line.pagename).link_to(request),
 139                         msg))
 140 
 141 def execute(pagename, request):
 142     editor = request.form.get('editor', [None])[0]
 143     timestamp = time.time() - 24 * 3600
 144        # request.form.get('timestamp', [None])[0]
 145     ok = request.form.get('ok', [0])[0]
 146 
 147     wikiutil.send_title(request, "Despam", pagename=pagename)    
 148     # Start content (important for RTL support)
 149     request.write(request.formatter.startContent("content"))
 150     
 151     if ok:
 152         revert_pages(request, editor, timestamp)
 153     elif editor:
 154         show_pages(request, pagename, editor, timestamp)
 155     else:
 156         show_editors(request, pagename, timestamp)
 157 
 158     # End content and send footer
 159     request.write(request.formatter.endContent())
 160     wikiutil.send_footer(request, pagename, editable=0, showactions=0,
 161                          form=request.form)

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] (2005-06-19 10:20:51, 4.9 KB) [[attachment:Despam.py]]
 All files | Selected Files: delete move to page copy to page

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