-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockreview.py
316 lines (288 loc) · 13.5 KB
/
blockreview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This bot implements a blocking review process for de-wiki first.
For other sites this bot script must be changed.
This script is run by [[de:User:xqt]]. It should
not be run by other users without prior contact.
The following parameters are supported:
-
All other parameters will be regarded as part of the title of a single page,
and the bot will only work on that single page.
"""
#
# (C) xqt, 2010-2013
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#
import wikipedia as pywikibot
import userlib
import query
class BlockreviewBot:
# notes
note_admin = {
'de': u"\n\n== Sperrprüfungswunsch ==\nHallo %(admin)s, \n\n[[%(user)s]] wünscht diePrüfung seiner/ihrer Sperre vom %(time)s über die Dauer von %(duration)s. Kommentar war ''%(comment)s''. Bitte äußere Dich dazu auf der [[%(usertalk)s#%(section)s|Diskussionsseite]]. -~~~~"
}
note_project = {
'de': u"\n\n== [[%(user)s]] ==\n* gesperrt am %(time)s durch {{Benutzer|%(admin)s}} für eine Dauer von %(duration)s.\n* Kommentar war ''%(comment)s''.\n* [[Benutzer:%(admin)s]] wurde [[Benutzer Diskussion:%(admin)s#Sperrprüfungswunsch|benachrichtigt]].\n* [[%(usertalk)s#%(section)s|Link zur Diskussion]]\n:<small>-~~~~</small>\n;Antrag entgegengenommen"
}
# edit summaries
msg_admin = {
'de': u'Bot-Benachrichtigung: Sperrprüfungswunsch von [[%(user)s]]',
}
msg_user = {
'de': u'Bot: Administrator [[Benutzer:%(admin)s|%(admin)s]] für Sperrprüfung benachrichtigt',
}
msg_done = {
'de': u'Bot: Sperrprüfung abgeschlossen. Benutzer ist entsperrt.',
}
unblock_tpl = {
'de': u'Benutzer:TAXman/Sperrprüfungsverfahren',
'pt': u'Predefinição:Discussão de bloqueio',
}
review_cat = {
'de': u'Wikipedia:Sperrprüfung',
}
project_name = {
'de': u'Benutzer:TAXman/Sperrprüfung Neu',
'pt': u'Wikipedia:Pedidos a administradores/Discussão de bloqueio',
}
def __init__(self, dry):
"""
Constructor. Parameters:
* generator - The page generator that determines on which pages
to work on.
* dry - If True, doesn't do any real changes, but only shows
what would have been changed.
"""
self.site = pywikibot.getSite()
self.dry = dry
self.info = None
self.parts = None
def run(self):
# TODO: change the generator for template to the included category
try:
genPage = pywikibot.Page(self.site,
self.unblock_tpl[self.site.lang],
defaultNamespace=10)
except KeyError:
pywikibot.error(u'Language "%s" not supported by this bot.'
% self.site.lang)
else:
for page in genPage.getReferences(follow_redirects=False,
withTemplateInclusion=True,
onlyTemplateInclusion=True):
if page.namespace() == 3:
self.treat(page)
else:
pywikibot.output(u'Ignoring %s, user namespace required'
% page.title(asLink=True))
def treat(self, userPage):
"""
Loads the given page, does some changes, and saves it.
"""
talkText = self.load(userPage)
if not talkText:
# sanity check. No talk page found.
return
unblock_tpl = self.unblock_tpl[self.site.lang]
project_name = self.project_name[self.site.lang]
user = userlib.User(self.site, userPage.title(withNamespace=False))
saveAdmin = saveProject = False
talkComment = None
for templates in userPage.templatesWithParams():
if templates[0] == unblock_tpl:
self.getInfo(user)
# Step 1
# a new template is set on blocked users talk page.
# Notify the blocking admin
if templates[1] == [] or templates[1][0] == u'1':
if self.info['action'] == 'block' or user.isBlocked():
if self.site.sitename() == 'wikipedia:de':
admin = userlib.User(self.site, self.info['user'])
adminPage = admin.getUserTalkPage()
adminText = adminPage.get()
note = pywikibot.translate(self.site.lang,
self.note_admin,
self.parts)
comment = pywikibot.translate(self.site.lang,
self.msg_admin,
self.parts)
adminText += note
self.save(adminText, adminPage, comment, False)
### test for pt-wiki
### just print all sysops talk pages
elif self.site.sitename() == 'wikipedia:pt':
import pagegenerators as pg
gen = pg.PreloadingGenerator(self.SysopGenerator())
for sysop in gen:
print sysop.title()
talkText = talkText.replace(u'{{%s}}' % unblock_tpl,
u'{{%s|2}}' % unblock_tpl)
talkText = talkText.replace(u'{{%s|1}}' % unblock_tpl,
u'{{%s|2}}' % unblock_tpl)
talkComment = pywikibot.translate(self.site.lang,
self.msg_user
% self.parts)
# some test stuff
if pywikibot.logger.isEnabledFor(pywikibot.DEBUG) \
and self.site().loggedInAs() == u'Xqbot:':
testPage = pywikibot.Page(self.site,
'Benutzer:Xqt/Test')
test = testPage.get()
test += note
self.save(test, testPage,
'[[WP:BA#SPP-Bot|SPPB-Test]]')
else:
# nicht blockiert. Fall auf DS abschließen
talkText = talkText.replace(u'{{%s}}' % unblock_tpl,
u'{{%s|4}}' % unblock_tpl)
talkText = talkText.replace(u'{{%s|1}}' % unblock_tpl,
u'{{%s|4}}' % unblock_tpl)
talkComment = pywikibot.translate(self.site.lang,
self.msg_done)
# Step 2
# Admin has been notified.
# Wait for 2 hours, than put a message to the project page
elif templates[1][0] == u'2':
if self.info['action'] == 'block' or user.isBlocked():
# TODO: check whether wait time is gone
# check whether this entry already esists
project = pywikibot.Page(self.site, project_name)
projText = project.get()
note = pywikibot.translate(self.site.lang,
self.note_project,
self.parts)
comment = pywikibot.translate(self.site.lang,
self.msg_admin,
self.parts)
projText += note
self.save(projText, project, comment, botflag=False)
talkText = talkText.replace(u'{{%s|2}}' % unblock_tpl,
u'{{%s|3}}' % unblock_tpl)
talkComment = u'Bot: [[%s|Wikipedia:Sperrprüfung]] eingetragen' \
% project_name
else:
# User is unblocked. Review can be closed
talkText = talkText.replace(u'{{%s|2}}' % unblock_tpl,
u'{{%s|4}}' % unblock_tpl)
talkComment = pywikibot.translate(self.site.lang,
self.msg_done)
# Step 3
# Admin is notified, central project page has a message
# Discussion is going on
# Check whether it can be closed
elif templates[1][0] == u'3':
if self.info['action'] == 'block' or user.isBlocked():
pass
else:
# User is unblocked. Review can be closed
talkText = talkText.replace(u'{{%s|3}}' % unblock_tpl,
u'{{%s|4}}' % unblock_tpl)
talkComment = pywikibot.translate(self.site.lang,
self.msg_done)
# Step 4
# Review is closed
elif templates[1][0] == u'4':
# nothing left to do
pass
else:
# wrong template found
pass
# at last if there is a talk comment, users talk page must be changed
if talkComment:
self.save(talkText, userPage, talkComment)
def getInfo(self, user):
if not self.info:
self.info = self.site.logpages(1, mode='block',
title=user.getUserPage().title(),
dump=True).next()
self.parts = {
'admin': self.info['user'],
'user': self.info['title'],
'usertalk': user.getUserTalkPage().title(),
'section': u'Sperrprüfung',
'time': self.info['timestamp'],
'duration': self.info['block']['duration'],
'comment': self.info['comment'],
}
def SysopGenerator(self):
params = param = {
'action': 'query',
'list': 'allusers',
'augroup': 'sysop',
'auprop': 'groups',
'aulimit': 500,
}
data = query.GetData(params, self.site)
for user in data['query']['allusers']:
# exclude sysop bots
if 'bot' not in user['groups']:
# yield the sysop talkpage
yield pywikibot.Page(self.site, user['name'],
defaultNamespace=3)
def load(self, page):
"""
Loads the given page, does some changes, and saves it.
"""
try:
# Load the page
text = page.get()
except pywikibot.NoPage:
pywikibot.output(u"Page %s does not exist; skipping."
% page.title(asLink=True))
except pywikibot.IsRedirectPage:
pywikibot.output(u"Page %s is a redirect; skipping."
% page.title(asLink=True))
else:
return text
return None
def save(self, text, page, comment, minorEdit=True, botflag=True):
if text != page.get():
# Show the title of the page we're working on.
# Highlight the title in purple.
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
% page.title())
# show what was changed
pywikibot.showDiff(page.get(), text)
pywikibot.output(u'Comment: %s' % comment)
if not self.dry:
choice = pywikibot.inputChoice(
u'Do you want to accept these changes?',
['Yes', 'No'], ['y', 'N'], 'N')
if choice == 'y':
try:
# Save the page
page.put(text, comment=comment, minorEdit=minorEdit,
botflag=botflag)
except pywikibot.LockedPage:
pywikibot.output(u"Page %s is locked; skipping."
% page.title(asLink=True))
except pywikibot.EditConflict:
pywikibot.output(
u'Skipping %s because of edit conflict'
% (page.title()))
except pywikibot.SpamfilterError, error:
pywikibot.output(
u'Cannot change %s because of spam blacklist entry '
u'%s' % (page.title(), error.url))
else:
return True
return
def main():
show = False
# Parse command line arguments
for arg in pywikibot.handleArgs():
show = True
if not show:
bot = BlockreviewBot(pywikibot.simulate)
bot.run()
else:
pywikibot.showHelp()
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()