-
Notifications
You must be signed in to change notification settings - Fork 8
/
commons_defaultsort_conflicts.py
79 lines (66 loc) · 2.42 KB
/
commons_defaultsort_conflicts.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Check for consistency in commons category usage
# Mike Peel 18-Apr-2018 v1 - start
# Mike Peel 17 Oct 2020 Update to python3
import pywikibot
import numpy as np
import time
import string
from pywikibot import pagegenerators
import urllib
maxnum = 10000
nummodified = 0
commons = pywikibot.Site('commons', 'commons')
repo = commons.data_repository() # this is a DataSite object
debug = 1
manual = 0
savemessage = "Disable defaultsort from infobox to avoid conflict"
def fixcat(targetcat):
target_text = targetcat.get()
target_text = target_text.replace("{{Wikidata Infobox}}", "{{Wikidata Infobox|defaultsort=no}}")
target_text = target_text.replace("{{Wikidata infobox}}", "{{Wikidata Infobox|defaultsort=no}}")
target_text = target_text.replace("{{wikidata Infobox}}", "{{Wikidata Infobox|defaultsort=no}}")
target_text = target_text.replace("{{wikidata infobox}}", "{{Wikidata Infobox|defaultsort=no}}")
# Time to save it
if (target_text != targetcat.get()):
print(target_text)
targetcat.text = target_text.strip()
if manual == 1:
text = raw_input("Save on Commons? ")
if text == 'y':
try:
targetcat.save(savemessage)
return 1
except:
print("That didn't work!")
return 0
else:
return 0
else:
try:
targetcat.save(savemessage)
return 1
except:
print("That didn't work!")
return 0
else:
return 0
startcat = 'Category:Pages with DEFAULTSORT conflicts'
# First touch the pages to make sure it's not a caching issue
cat = pywikibot.Category(commons,startcat)
for result in pagegenerators.SubCategoriesPageGenerator(cat, recurse=False):
result.touch()
# Then disable the defaultsort if still needed.
cat = pywikibot.Category(commons,startcat)
targetcats = pagegenerators.SubCategoriesPageGenerator(cat, recurse=False);
for targetcat in targetcats:
print(targetcat)
print("\n" + targetcat.title())
# print(target.text)
nummodified += fixcat(targetcat)
if nummodified >= maxnum:
print('Reached the maximum of ' + str(maxnum) + ' entries modified, quitting!')
exit()
print('Done! Edited ' + str(nummodified) + ' entries')
# EOF