comparison MoinMoin/action/Despam.py @ 14:c8a7e012ebeb

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