-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonscat.py
616 lines (568 loc) · 29 KB
/
commonscat.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
With this tool you can add the template {{commonscat}} to categories.
The tool works by following the interwiki links. If the template is present on
another langauge page, the bot will use it.
You could probably use it at articles as well, but this isnt tested.
This bot uses pagegenerators to get a list of pages. The following options are
supported:
¶ms;
-always Don't prompt you for each replacement. Warning message
has not to be confirmed. ATTENTION: Use this with care!
-summary:XYZ Set the action summary message for the edit to XYZ,
otherwise it uses messages from add_text.py as default.
-checkcurrent Work on all category pages that use the primary commonscat
template.
For example to go through all categories:
commonscat.py -start:Category:!
"""
# TODO:
"""
Commonscat bot:
Take a page. Follow the interwiki's and look for the commonscat template
*Found zero templates. Done.
*Found one template. Add this template
*Found more templates. Ask the user <- still have to implement this
TODO:
*Update interwiki's at commons
*Collect all possibilities also if local wiki already has link.
*Better support for other templates (translations) / redundant templates.
*Check mode, only check pages which already have the template
*More efficient like interwiki.py
*Possibility to update other languages in the same run
"""
#
# (C) Multichill, 2008-2009
# (C) Xqt, 2009-2012
# (C) Pywikipedia bot team, 2008-2012
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#
import wikipedia as pywikibot
import config, pagegenerators, add_text, re
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp
}
# Primary template, list of alternatives
# No entry needed if it is like _default
commonscatTemplates = {
'_default': (u'Commonscat', []),
'af' : (u'CommonsKategorie', [u'commonscat']),
'an' : (u'Commonscat', [u'Commons cat']),
'ar' : (u'تصنيف كومنز', [u'Commonscat', u'تصنيف كومونز',
u'Commons cat', u'CommonsCat']),
'arz' : (u'Commons cat', [u'Commoncat']),
'az' : (u'CommonsKat', [u'Commonscat']),
'bn' : (u'কমন্সক্যাট', [u'Commonscat']),
'ca' : (u'Commonscat', [u'Commons cat', u'Commons category']),
'crh' : (u'CommonsKat', [u'Commonscat']),
'cs' : (u'Commonscat', [u'Commons cat']),
'da' : (u'Commonscat', [u'Commons cat', u'Commons category',
u'Commonscat left', u'Commonscat2']),
'de' : (u'Commonscat', [u'Commons cat',]),
'en' : (u'Commons category', [u'Commoncat', u'Commonscat', u'Commons cat',
u'Commons+cat', u'Commonscategory',
u'Commons and category', u'Commonscat-inline',
u'Commons category-inline', u'Commons2',
u'Commons category multi',
u'Cms-catlist-up', u'Catlst commons',
u'Commonscat show2',
u'Sister project links']),
'es' : (u'Commonscat', [u'Ccat', u'Commons cat', u'Categoría Commons',
u'Commonscat-inline']),
'et' : (u'Commonsi kategooria', [u'Commonscat', u'Commonskat', u'Commons cat', u'Commons category']),
'eu' : (u'Commonskat', [u'Commonscat']),
'fa' : (u'ویکیانبار-رده', [u'Commonscat', u'Commons cat', u'انبار رده',
u'Commons category', u'انبار-رده',
u'جعبه پیوند به پروژههای خواهر',
u'در پروژههای خواهر', u'پروژههای خواهر']),
'fr' : (u'Commonscat', [u'CommonsCat', u'Commons cat',
u'Commons category']),
'frp' : (u'Commonscat', [u'CommonsCat']),
'ga' : (u'Catcómhaoin', [u'Commonscat']),
'hi' : (u'Commonscat', [u'Commons2', u'Commons cat', u'Commons category']),
'hu' : (u'Commonskat', [u'Közvagyonkat']),
'hy' : (u'Վիքիպահեստ կատեգորիա', [u'Commonscat', u'Commons cat',
u'Commons category']),
'id' : (u'Commonscat', [u'Commons cat', u'Commons2',
u'CommonsCat', u'Commons category']),
'is' : (u'CommonsCat', [u'Commonscat']),
'ja' : (u'Commonscat', [u'Commons cat', u'Commons category']),
'jv' : (u'Commonscat', [u'Commons cat']),
'kaa' : (u'Commons cat', [u'Commonscat']),
'kk' : (u'Commonscat', [u'Commons2']),
'ko' : (u'Commonscat', [u'Commons cat', u'공용분류']),
'la' : (u'CommuniaCat', []),
'mk' : (u'Ризница-врска', [u'Commonscat', u'Commons cat', u'CommonsCat',
u'Commons2', u'Commons category']),
'ml' : (u'Commonscat', [u'Commons cat', u'Commons2']),
'ms' : (u'Kategori Commons', [u'Commonscat', u'Commons category']),
'nn' : (u'Commonscat', [u'Commons cat']),
'os' : (u'Commonscat', [u'Commons cat']),
'pt' : (u'Commonscat', [u'Commons cat']),
'ro' : (u'Commonscat', [u'Commons cat']),
'ru' : (u'Commonscat', [u'Викисклад-кат', u'Commons category']),
'simple' : (u'Commonscat' ,[u'Commons cat', u'Commons cat multi',
u'Commons category', u'Commons category multi',
u'CommonsCompact', u'Commons-inline']),
'sh' : (u'Commonscat', [u'Commons cat']),
'sl' : (u'Kategorija v Zbirki', [u'Commonscat', u'Kategorija v zbirki',
u'Commons cat', u'Katzbirke']),
'sv' : (u'Commonscat', [u'Commonscat-rad', u'Commonskat', u'Commons cat',
u'Commonscatbox', u'Commonscat-box']),
'sw' : (u'Commonscat', [u'Commons2', u'Commons cat']),
'te' : (u'Commonscat', [u'Commons cat']),
'tr' : (u'Commons kategori', [u'CommonsKat', u'Commonscat',
u'Commons cat']),
'uk' : (u'Commonscat', [u'Commons cat', u'Category', u'Commonscat-inline']),
'vi' : (u'Commonscat', [u'Commons2', u'Commons cat', u'Commons category',
u'Commons+cat']),
'zh' : (u'Commonscat', [u'Commons cat', u'Commons category']),
'zh-classical' : (u'共享類', [u'Commonscat']),
'zh-yue' : (u'同享類', [u'Commonscat', u'共享類 ', u'Commons cat',
u'Commons category']),
}
ignoreTemplates = {
'af': [u'commons'],
'ar': [u'تحويلة تصنيف', u'كومنز', u'كومونز', u'Commons'],
'be-x-old' : [u'Commons', u'Commons category'],
'cs': [u'Commons', u'Sestřičky', u'Sisterlinks'],
'da': [u'Commons', u'Commons left', u'Commons2', u'Commonsbilleder',
u'Commonskat', u'Commonscat2', u'GalleriCommons', u'Søsterlinks'],
'de': [u'Commons', u'ZhSZV', u'Bauwerk-stil-kategorien',
u'Bauwerk-funktion-kategorien', u'KsPuB',
u'Kategoriesystem Augsburg-Infoleiste',
u'Kategorie Ge', u'Kategorie v. Chr. Ge',
u'Kategorie Geboren nach Jh. v. Chr.', u'Kategorie Geboren nach Jh.',
u'!Kategorie Gestorben nach Jh. v. Chr.',
u'!Kategorie Gestorben nach Jh.',
u'Kategorie Jahr', u'Kategorie Jahr v. Chr.',
u'Kategorie Jahrzehnt', u'Kategorie Jahrzehnt v. Chr.',
u'Kategorie Jahrhundert', u'Kategorie Jahrhundert v. Chr.',
u'Kategorie Jahrtausend', u'Kategorie Jahrtausend v. Chr.'],
'en': [u'Category redirect', u'Commons', u'Commonscat1A', u'Commoncats',
u'Commonscat4Ra',
u'Sisterlinks', u'Sisterlinkswp', u'Sister project links',
u'Tracking category', u'Template category', u'Wikipedia category'],
'eo': [u'Commons',
(u'Projekto/box', 'commons='),
(u'Projekto', 'commons='),
(u'Projektoj', 'commons='),
(u'Projektoj', 'commonscat=')],
'es': [u'Commons', u'IprCommonscat'],
'eu': [u'Commons'],
'fa': [u'Commons', u'ویکیانبار',
u'Category redirect',u'رده بهتر',
u'جعبه پیوند به پروژههای خواهر',u'در پروژههای خواهر',u'پروژههای خواهر'],
'fi': [u'Commonscat-rivi', u'Commons-rivi', u'Commons'],
'fr': [u'Commons', u'Commons-inline', (u'Autres projets', 'commons=')],
'fy': [u'Commons', u'CommonsLyts'],
'hr': [u'Commons', (u'WProjekti', 'commonscat=')],
'is': [u'Systurverkefni', u'Commons'],
'it': [(u'Ip', 'commons='), (u'Interprogetto', 'commons=')],
'ja': [u'CommonscatS', u'SisterlinksN', u'Interwikicat'],
'ms': [u'Commons', u'Sisterlinks', u'Commons cat show2'],
'nds-nl': [u'Commons'],
'nl': [u'Commons', u'Commonsklein', u'Commonscatklein', u'Catbeg',
u'Catsjab', u'Catwiki'],
'om': [u'Commons'],
'pt': [u'Correlatos'],
'simple': [u'Sisterlinks'],
'ru': [u'Навигация', u'Навигация для категорий', u'КПР', u'КБР', u'Годы в России', u'commonscat-inline'],
'tt': [u'Навигация'],
'zh': [u'Category redirect', u'cr', u'Commons',
u'Sisterlinks', u'Sisterlinkswp',
u'Tracking category', u'Trackingcatu',
u'Template category', u'Wikipedia category'
u'分类重定向', u'追蹤分類', u'共享資源', u'追蹤分類'],
}
msg_change = {
'be-x-old': u'Робат: зьмяніў шаблён [[:Commons:Category:%(oldcat)s|%(oldcat)s]] на [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'cs': u'Robot změnil šablonu Commonscat z [[:Commons:Category:%(oldcat)s|%(oldcat)s]] na [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'da': u'Robot: Ændrer commonscat link fra [[:Commons:Category:%(oldcat)s|%(oldcat)s]] til [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'de': u'Bot: Ändere commonscat link von [[:Commons:Category:%(oldcat)s|%(oldcat)s]] zu [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'en': u'Bot: Changing commonscat link from [[:Commons:Category:%(oldcat)s|%(oldcat)s]] to [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'fa': u'ربات: تغییر پیوند به انبار از [[:Commons:Category:%(oldcat)s|%(oldcat)s]] به [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'fr': u'Robot: Changé commonscat link de [[:Commons:Category:%(oldcat)s|%(oldcat)s]] à [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'frr':u'Bot: Feranere commonscat link faan [[:Commons:Category:%(oldcat)s|%(oldcat)s]] tu [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'is': u'Vélmenni: Breyti Commonscat tengli frá [[:Commons:Category:%(oldcat)s|%(oldcat)s]] í [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'pdc': u'Waddefresser: commonscat Gleecher vun [[:Commons:Category:%(oldcat)s|%(oldcat)s]] nooch [[:Commons:Category:%(newcat)s|%(newcat)s]] geennert',
'ru': u'Бот: Изменение commonscat-ссылки с [[:Commons:Category:%(oldcat)s|%(oldcat)s]] на [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'sk': u'Robot zmenil šablónu Commonscat z [[:Commons:Category:%(oldcat)s|%(oldcat)s]] na [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'uk': u'Бот: Зміна commonscat-посилання з [[:Commons:Category:%(oldcat)s|%(oldcat)s]] на [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'th': u'บอต: เปลี่ยนลิงก์หมวดหมู่คอมมอนส์จาก [[:Commons:Category:%(oldcat)s|%(oldcat)s]] เป็น [[:Commons:Category:%(newcat)s|%(newcat)s]]',
'zh': u'機器人:更改 commonscat 連結,從 %(oldcat)s 至 %(newcat)s',
}
class CommonscatBot:
def __init__(self, generator, always, summary=None):
self.generator = generator
self.always = always
self.summary = summary
self.site = pywikibot.getSite()
def run(self):
for page in self.generator:
self.treat(page)
def treat(self, page):
"""
Loads the given page, does some changes, and saves it.
"""
if not page.exists():
pywikibot.output(u'Page %s does not exist. Skipping.'
% page.title(asLink=True))
elif page.isRedirectPage():
pywikibot.output(u'Page %s is a redirect. Skipping.'
% page.title(asLink=True))
elif page.isCategoryRedirect():
pywikibot.output(u'Page %s is a category redirect. Skipping.'
% page.title(asLink=True))
elif page.isDisambig():
pywikibot.output(u'Page %s is a disambiguation. Skipping.'
% page.title(asLink=True))
else:
(status, always) = self.addCommonscat(page)
return
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):
# only save if something was changed
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.always:
choice = pywikibot.inputChoice(
u'Do you want to accept these changes?',
['Yes', 'No', 'Always', 'Quit'],
['y', 'N', 'a', 'q'], 'N')
if choice == 'a':
self.always = True
elif choice == 'q':
import sys
sys.exit()
if self.always or 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 %s'
% (page.title(), error.url))
else:
return True
return False
@classmethod
def getCommonscatTemplate (self, lang=None):
'''Get the template name in a language. Expects the language code.
Return as tuple containing the primary template and it's alternatives
'''
if lang in commonscatTemplates:
return commonscatTemplates[lang]
else:
return commonscatTemplates[u'_default']
def skipPage(self, page):
'''
Do we want to skip this page?
'''
if page.site().language() in ignoreTemplates:
templatesInThePage = page.templates()
templatesWithParams = page.templatesWithParams()
for template in ignoreTemplates[page.site().language()]:
if type(template) != tuple:
if template in templatesInThePage:
return True
else:
for (inPageTemplate, param) in templatesWithParams:
if inPageTemplate == template[0] \
and template[1] in param[0].replace(' ', ''):
return True
return False
def updateInterwiki (self, wikipediaPage = None, commonsPage = None):
'''Update the interwiki's at commons from a wikipedia page. The bot just
replaces the interwiki links at the commons page with the interwiki's
from the wikipedia page. This should probably be more intelligent. We
could use add all the interwiki's and remove duplicates. Or only remove
language links if multiple language links to the same language exist.
This function is disabled for the moment until i figure out what the
best way is to update the interwiki's.
'''
interwikis = {}
comment= u''
interwikilist = wikipediaPage.interwiki()
interwikilist.append(wikipediaPage)
for interwikiPage in interwikilist:
interwikis[interwikiPage.site()]=interwikiPage
oldtext = commonsPage.get()
# The commonssite object doesnt work with interwiki's
newtext = pywikibot.replaceLanguageLinks(oldtext, interwikis,
pywikibot.getSite(u'nl'))
comment = u'Updating interwiki\'s from [[' + \
wikipediaPage.site().language() + \
u':' + wikipediaPage.title() + u']]'
if newtext != oldtext:
#This doesnt seem to work. Newtext has some trailing whitespace
pywikibot.showDiff(oldtext, newtext)
commonsPage.put(newtext=newtext, comment=comment)
def addCommonscat (self, page):
'''Take a page. Go to all the interwiki page looking for a commonscat
template. When all the interwiki's links are checked and a proper
category is found add it to the page.
'''
pywikibot.output(u'Working on ' + page.title());
#Get the right templates for this page
primaryCommonscat, commonscatAlternatives = self.getCommonscatTemplate(
page.site().language())
commonscatLink = self.getCommonscatLink (page)
if commonscatLink:
pywikibot.output(u'Commonscat template is already on %s'
% page.title())
(currentCommonscatTemplate, currentCommonscatTarget, LinkText, Note) = commonscatLink
checkedCommonscatTarget = self.checkCommonscatLink(currentCommonscatTarget)
if (currentCommonscatTarget==checkedCommonscatTarget):
#The current commonscat link is good
pywikibot.output(u'Commonscat link at %s to Category:%s is ok'
% (page.title() , currentCommonscatTarget));
return (True, self.always)
elif checkedCommonscatTarget!=u'':
#We have a new Commonscat link, replace the old one
self.changeCommonscat(page, currentCommonscatTemplate,
currentCommonscatTarget, primaryCommonscat,
checkedCommonscatTarget, LinkText, Note)
return (True, self.always)
else:
#Commonscat link is wrong
commonscatLink = self.findCommonscatLink(page)
if (commonscatLink!=u''):
self.changeCommonscat (page, currentCommonscatTemplate,
currentCommonscatTarget, primaryCommonscat,
commonscatLink)
#else
#Should i remove the commonscat link?
elif self.skipPage(page):
pywikibot.output("Found a template in the skip list. Skipping %s"
% page.title());
else:
commonscatLink = self.findCommonscatLink(page)
if (commonscatLink!=u''):
if commonscatLink == page.title():
textToAdd = u'{{%s}}' % primaryCommonscat
else:
textToAdd = u'{{%s|%s}}' % (primaryCommonscat,
commonscatLink)
(success, status, self.always) = add_text.add_text(page,
textToAdd,
self.summary,
None, None,
self.always)
return (True, self.always);
return (True, self.always);
def changeCommonscat (self, page=None, oldtemplate=u'', oldcat=u'',
newtemplate=u'', newcat=u'', linktitle=u'',
description=u''):
''' Change the current commonscat template and target. '''
if oldcat == '3=S' or linktitle == '3=S':
return #additional param on de-wiki, TODO: to be handled
if not linktitle and (page.title().lower() in oldcat.lower() or
oldcat.lower() in page.title().lower()):
linktitle = oldcat
if linktitle and newcat <> page.title(withNamespace=False):
newtext = re.sub(u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}'
% oldtemplate,
u'{{%s|%s|%s}}' % (newtemplate, newcat, linktitle),
page.get())
elif newcat == page.title(withNamespace=False):
newtext = re.sub(u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}'
% oldtemplate,
u'{{%s}}' % newtemplate,
page.get())
elif oldcat.strip() != newcat: #strip trailing white space
newtext = re.sub(u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}'
%oldtemplate,
u'{{%s|%s}}' % (newtemplate, newcat),
page.get())
else: # nothing left to do
return
if self.summary:
comment = self.summary
else:
comment = pywikibot.translate(page.site(), msg_change) \
% {'oldcat':oldcat, 'newcat':newcat}
self.save(newtext, page, comment)
def findCommonscatLink (self, page=None):
for ipage in page.interwiki():
try:
if(ipage.exists() and not ipage.isRedirectPage()
and not ipage.isDisambig()):
commonscatLink = self.getCommonscatLink (ipage)
if commonscatLink:
(currentTemplate, possibleCommonscat, linkText, Note) = commonscatLink
checkedCommonscat = self.checkCommonscatLink(possibleCommonscat)
if (checkedCommonscat!= u''):
pywikibot.output(
u"Found link for %s at [[%s:%s]] to %s."
% (page.title(), ipage.site().language(),
ipage.title(), checkedCommonscat))
return checkedCommonscat
except pywikibot.BadTitle:
#The interwiki was incorrect
return u''
return u''
def getCommonscatLink (self, wikipediaPage=None):
'''
Go through the page and return a tuple of (<templatename>, <target>)
'''
primaryCommonscat, commonscatAlternatives = self.getCommonscatTemplate(
wikipediaPage.site().language())
commonscatTemplate = u''
commonscatTarget = u''
commonscatLinktext = u''
commonscatNote = u''
#See if commonscat is present
for template in wikipediaPage.templatesWithParams():
if template[0]==primaryCommonscat \
or template[0] in commonscatAlternatives:
commonscatTemplate = template[0]
if (len(template[1]) > 0):
commonscatTarget = template[1][0]
if len(template[1]) > 1:
commonscatLinktext = template[1][1]
if len(template[1]) > 2:
commonscatNote = template[1][2]
else:
commonscatTarget = wikipediaPage.title(withNamespace=False)
return (commonscatTemplate, commonscatTarget,
commonscatLinktext, commonscatNote)
return None
def checkCommonscatLink (self, name = ""):
'''
This function will retun the name of a valid commons category
If the page is a redirect this function tries to follow it.
If the page doesnt exists the function will return an empty string
'''
if pywikibot.verbose:
pywikibot.output("getCommonscat: " + name )
try:
commonsSite = self.site.image_repository()
#This can throw a pywikibot.BadTitle
commonsPage = pywikibot.Page(commonsSite, "Category:" + name)
if not commonsPage.exists():
logpages = commonsSite.logpages(mode='delete',
title=commonsPage.title())
try:
logitem = logpages.next()
(logpage, loguser, logtimestamp, logcomment) = logitem
# Some logic to extract the target page.
regex = u'moved to \[\[\:?Category:(?P<newcat1>[^\|\}]+)(\|[^\}]+)?\]\]|Robot: Changing Category:(.+) to Category:(?P<newcat2>.+)'
m = re.search(regex, logcomment, flags=re.I)
if m:
if m.group('newcat1'):
return self.checkCommonscatLink(m.group('newcat1'))
elif m.group('newcat2'):
return self.checkCommonscatLink(m.group('newcat2'))
else:
pywikibot.output(
u'getCommonscat: Deleted by %s. Couldn\'t find move target in \" %s \"'
% (loguser, logcomment))
return u''
except StopIteration:
if pywikibot.verbose:
pywikibot.output(
u"getCommonscat: The category doesnt exist and nothing found in the deletion log.")
return u''
elif commonsPage.isRedirectPage():
if pywikibot.verbose:
pywikibot.output(
u"getCommonscat: The category is a redirect")
return self.checkCommonscatLink(
commonsPage.getRedirectTarget().title(withNamespace=False))
elif "Category redirect" in commonsPage.templates():
if pywikibot.verbose:
pywikibot.output(
u"getCommonscat: The category is a category redirect")
for template in commonsPage.templatesWithParams():
if ((template[0]=="Category redirect")
and (len(template[1]) > 0)):
return self.checkCommonscatLink(template[1][0])
elif commonsPage.isDisambig():
if pywikibot.verbose:
pywikibot.output(
u"getCommonscat: The category is disambiguation")
return u''
else:
return commonsPage.title(withNamespace=False)
except pywikibot.BadTitle:
#Funky title so not correct
return u''
except pywikibot.PageNotFound:
return u''
def main():
'''
Parse the command line arguments and get a pagegenerator to work on.
Iterate through all the pages.
'''
summary = None; generator = None; checkcurrent = False; always = False
ns = []
ns.append(14)
# Load a lot of default generators
genFactory = pagegenerators.GeneratorFactory()
for arg in pywikibot.handleArgs():
if arg.startswith('-summary'):
if len(arg) == 8:
summary = pywikibot.input(u'What summary do you want to use?')
else:
summary = arg[9:]
elif arg.startswith('-checkcurrent'):
checkcurrent = True
primaryCommonscat, commonscatAlternatives = \
CommonscatBot.getCommonscatTemplate(
pywikibot.getSite().language())
generator = pagegenerators.NamespaceFilterPageGenerator(
pagegenerators.ReferringPageGenerator(
pywikibot.Page(pywikibot.getSite(),
u'Template:' + primaryCommonscat),
onlyTemplateInclusion=True), ns)
elif arg == '-always':
always = True
else:
genFactory.handleArg(arg)
if not generator:
generator = genFactory.getCombinedGenerator()
if not generator:
raise add_text.NoEnoughData(
u'You have to specify the generator you want to use for the script!')
pregenerator = pagegenerators.PreloadingGenerator(generator)
bot = CommonscatBot(pregenerator, always, summary)
bot.run()
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()