-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdar.py
293 lines (252 loc) · 9.97 KB
/
dar.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
dar.py - a script to move categories by request
useage:
python pwb.py dar [OPTIONS]
"""
#
# (C) Reza (w:fa:User:Reza1615), 2015
# (C) Amir (w:fa:User:Ladsgroup), 2015
# (C) Huji (w:fa:User:Huji), 2016
#
# Distributed under the terms of MIT License (MIT)
#
from __future__ import absolute_import, unicode_literals
#
import pywikibot
from pywikibot import pagegenerators
from pywikibot import config
import sys
import json
import codecs
import re
from scripts import category
class CatMoveBot:
def __init__(self, logPage=None, project='wikipedia'):
if logPage is None:
raise ValueError('Log page must be specified')
self.logPage = logPage
self.site = pywikibot.Site('fa', project)
self.redirTemplate = u'رده بهتر'
self.summary = u'[[وپ:دار|ربات: انتقال رده]] به درخواست [[User:' + \
'%s|%s]] از [[:%s]] به [[:%s]]'
"""
Actually moves pages from one category to the other.
@param origin: name of the origin category
@param destination: name of the destination category
@param user: Name of the user on whose behalf the category move is done
"""
def move(self, origin, destination, user):
comment = self.summary % (user, user, origin, destination)
cat = category.CategoryMoveRobot(
origin, destination, batch=True,
comment=comment, inplace=False, move_oldcat=True,
delete_oldcat=True, title_regex=None, history=False)
cat.run()
"""
Plans moving pages from one category to the other, and updates the category
pages to reflect this move.
@param task: A list with two elements; the first element is the name of the
origin category, and the second is the name of, family=project the destination category
@param user: Name of the user on whose behalf the category move is done
"""
def run(self, task, user):
origin = task[0]
destination = task[1]
# Title of the destination page, without prefix
destTitle = re.sub('^(رده|[Cc]ategory)\:', '', destination)
originPage = pywikibot.Page(self.site, origin)
destinationPage = pywikibot.Page(self.site, destination)
originPageText = ''
destinationPageText = ''
if originPage:
try:
originPageText = originPage.get()
# Replace contents with the {{Category redirect}} template
originPage.put(
'{{' + self.redirTemplate + '|' + destTitle + '}}',
self.summary % (user, user, origin, destinatino))
except:
# Failed to fetch page contents. Gracefully ignore!
pass
if destinationPage:
try:
originPageText = originPage.get()
# TODO: Remove old {{Category redirect}}
except:
# Failed to fetch page contents. Gracefully ignore!
pass
self.move(origin, destination, user)
originPage = pywikibot.Page(self.site, origin)
if originPage:
try:
originPageText = originPage.get()
# Replace contents with the {{Category redirect}} template
originPage.put(
'{{' + self.redirTemplate + '|' + destTitle + '}}',
self.summary % (user, user, origin, destinatino))
except:
# Failed to fetch page contents. Gracefully ignore!
pass
class CatMoveInput:
"""
@param cacheFile: path to the local cache of previously validated users
"""
def __init__(self, cacheFile=None, project='wikipedia', threshold=3000):
if cacheFile is None:
raise ValueError('Cache file location must be specified')
else:
self.cacheFile = cacheFile
self.cache = self.loadCache()
self.site = pywikibot.Site('fa', project)
self.tasksPageDefault = u'{{/بالا}}'
self.moverBots = [u'Dexbot', u'HujiBot', u'rezabot']
self.threshold = threshold
self.successSummary = u'ربات: انتقال رده انجام شد!'
def loadCache(self):
f = codecs.open(self.cacheFile, 'r', 'utf-8')
txt = f.read().strip()
f.close()
if txt == '':
# Brand new cache file, will fail json.loads()
# Return an empty dictionary instead
cache = {}
else:
cache = json.loads(txt)
return cache
"""
@param cache: Validated users cache in JSON format
"""
def updateCache(self, cache):
fh = codecs.open(self.cacheFile, 'w', 'utf-8')
fh.write(json.dumps(cache))
fh.close()
"""
Verifies that the user's edit count is greater than self.threshold
@param username
"""
def verifyUser(self, username):
username = username.replace(u' ', u'_')
# If we have already established that this user qualifies
# then don't verify the user again
if self.cache.get(username):
return True
# Only users whose edit count is larger than self.threshold
# can request category moves
params = {
'action': 'query',
'list': 'users',
'ususers': username,
'usprop': 'editcount'
}
try:
req = pywikibot.data.api.Request(site=self.site, **params)
query = req.submit()
if query[u'query'][u'users'][0][u'editcount'] > self.threshold:
self.cache[username] = True
self.updateCache(self.cache)
return True
else:
return False
except:
return False
"""
Looks for a list of category move requests on the given page.
Each task must be defined in a separate line using either of the following
three formats:
* Category:Origin > Category:Destination
* [[:Category:Origin]] > [[:Category:Destination]]
* Origin @ Destination
Spaces are optional around `>` and `@` characters, and after the `*`.
If such a list is found, it will return a dictionary object containing a
list of category move tasks and name of the user on whose behalf categories
are moved.
@param tasksPageName: Wiki page on which category move requests are listed
"""
def processInput(self, tasksPageName):
tasksPage = pywikibot.Page(self.site, tasksPageName)
try:
pageText = tasksPage.get()
pageHistory = tasksPage.getVersionHistory()
lastUser = pageHistory[0][2]
except pywikibot.IsRedirectPage:
tasksPage = tasksPage.getRedirectTarget()
try:
pageText = tasksPage.get()
pageHistory = tasksPage.getVersionHistory()
lastUser = pageHistory[0][2]
except:
raise ValueError('Task list page not found!')
except:
raise ValueError('Task list page not found!')
if lastUser in self.moverBots:
print json.dumps({
'result': 'Last edit was by a mover bot. Request ignored.'
})
return False
elif self.verifyUser(lastUser):
print json.dumps({
'result': 'User verified. Processing task list.'
})
tasks = self.getTaskList(pageText)
tasksPage.put(self.tasksPageDefault, self.successSummary)
return {'tasks': tasks, 'user': lastUser}
else:
print json.dumps({
'result': 'Last editor was not qualified. Request ignored.'
})
return False
"""
Returns a list of lists, where each inner lists describe one category move
request (i.e. [origin, destination]).
@param taskText: wikicode of the page containing category move requests
"""
def getTaskList(self, taskText):
taskText=taskText.replace(u'{{/بالا}}',u'').replace(u'\r',u'').replace(u'\n\n',u'\n').strip()
taskList = []
for line in taskText.strip('\r').split('\n'):
if line[0] == '*':
# Remove the * and any immediately following spaces
line = re.sub('^\* *', '', line)
# Unlink category links
if '[[' in line:
line = re.sub('\[\[\:(رده|[Cc]ategory)\:([^\]]+)\]\]',
'\\1:\\2', line)
# Split by '>' or '@' (optionally surrounded by spaces)
if '>' in line or '@' in line:
pieces = re.split(' *[>@] *', line)
# Clean up category mentions
for i in range(0, len(pieces)):
# Make edit summaries more beautiful!
pieces[i] = pieces[i].replace(u'_', u' ')
# Add missing `Category` prefix
if (
re.search('^[Cc]ategory\:', pieces[i]) is None and
re.search('^رده\:', pieces[i]) is None
):
pieces[i] = u'رده:' + pieces[i]
# Add the pair to our task list
taskList.append(pieces)
else:
# Skip the line
pass
return taskList
def main():
cacheFile = '/data/project/dexbot/cache_dar.txt'
if 'wikiquote' in sys.argv:
project = 'wikiquote'
page_name = u'ویکیگفتاورد:درخواست انتقال رده'
limit = 750
config.family = project
else:
project = 'wikipedia'
page_name = u'ویکیپدیا:درخواست انتقال رده'
limit = 3000
vBot = CatMoveInput(cacheFile, project, limit)
req = vBot.processInput(page_name)
for task in req['tasks']:
mBot = CatMoveBot(page_name, project)
mBot.run(task, req['user'])
if __name__ == '__main__':
main()