-
Notifications
You must be signed in to change notification settings - Fork 8
/
commonscat_check.py
167 lines (154 loc) · 6.83 KB
/
commonscat_check.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Check for consistency in commons category usage
# Mike Peel 08-Feb-2018 v1 - start
# Mike Peel 19-Apr-2018 Add in repeat attempts at the query if it returns nothing
# Mike Peel 17 Oct 2020 python3
import pywikibot
import numpy as np
import time
import string
from pywikibot import pagegenerators
import urllib
from pibot_functions import *
maxnum = 10000
nummodified = 0
catredirect_templates = ["category redirect", "Category redirect", "seecat", "Seecat", "see cat", "See cat", "categoryredirect", "Categoryredirect", "catredirect", "Catredirect", "cat redirect", "Cat redirect", "catredir", "Catredir", "redirect category", "Redirect category", "cat-red", "Cat-red", "redirect cat", "Redirect cat", "category Redirect", "Category Redirect", "cat-redirect", "Cat-redirect"]
wikidata_site = pywikibot.Site("wikidata", "wikidata")
repo = wikidata_site.data_repository() # this is a DataSite object
commons = pywikibot.Site('commons', 'commons')
debug = 0
count = 0
maxnum = 10000
step = 100
num = int(maxnum/step)
for i in range(0,num):
try:
query = 'SELECT ?item ?commonscat ?sitelink ?name WHERE {'\
' ?item wdt:P373 ?commonscat.'\
' ?sitelink schema:about ?item; schema:isPartOf <https://commons.wikimedia.org/>; schema:name ?name .'\
' FILTER( CONTAINS(STR(?sitelink), \'Category:\') = true ) .'\
' FILTER( ?commonscat != SUBSTR(STR(?name), 10) ) .'\
' } LIMIT ' + str(step) + ' OFFSET ' + str(i*step)
print(query)
generator = pagegenerators.WikidataSPARQLPageGenerator(query, site=wikidata_site)
bad_commonscat_count = 0
bad_sitelink_count = 0
interwiki_conflicts = []
for page in generator:
count += 1
item_dict = page.get()
qid = page.title()
print("\n" + qid)
sitelink = get_sitelink_title(item_dict['sitelinks']['commonswiki'])
sitelink_redirect = ""
commonscat_redirect = ""
p373 = item_dict['claims']['P373']
bad_sitelink = 0
bad_commonscat = 0
p373_check = 0
for clm in p373:
p373_check += 1
# Only attempt to do this if there is only one value for P373
if p373_check == 1:
for clm in p373:
val = clm.getTarget()
commonscat = u"Category:" + val
try:
sitelink_page = pywikibot.Page(commons, sitelink)
except:
bad_sitelink = 1
bad_sitelink_count += 1
print('Found a bad sitelink')
else:
for option in catredirect_templates:
if "{{" + option in sitelink_page.text:
try:
sitelink_redirect = (sitelink_page.text.split("{{" + option + "|"))[1].split("}}")[0]
except:
try:
sitelink_redirect = (sitelink_page.text.split("{{" + option + " |"))[1].split("}}")[0]
except:
print('Wikitext parsing bug!')
sitelink_redirect = sitelink_redirect.replace(u":Category:","")
sitelink_redirect = sitelink_redirect.replace(u"Category:","")
try:
commonscat_page = pywikibot.Page(commons, commonscat)
except:
bad_commonscat = 1
bad_commonscat_count += 1
print('Found a bad commonscat')
else:
for option in catredirect_templates:
if "{{" + option in commonscat_page.text:
try:
commonscat_redirect = (commonscat_page.text.split("{{" + option +"|"))[1].split("}}")[0]
except:
try:
commonscat_redirect = (commonscat_page.text.split("{{" + option +" |"))[1].split("}}")[0]
except:
print('Wikitext parsing bug!')
commonscat_redirect = commonscat_redirect.replace(u":Category:","")
commonscat_redirect = commonscat_redirect.replace(u"Category:","")
print(sitelink + " - " + commonscat)
if (sitelink_redirect or commonscat_redirect):
print(" " + sitelink_redirect + " - " + commonscat_redirect)
# Sort out the case where one is a redirect to the other
if (u"Category:" + sitelink_redirect) == commonscat:
if debug:
print('Would change commons sitelink to ' + sitelink_redirect)
data = {'sitelinks': [{'site': 'commonswiki', 'title': u"Category:" + sitelink_redirect}]}
try:
page.editEntity(data, summary=u'Update commons sitelink to avoid commons category redirect')
nummodified += 1
except:
interwiki_conflicts.append(qid)
if (u"Category:" + commonscat_redirect) == sitelink:
if debug:
print("Would change P373 to " + commonscat_redirect)
try:
clm.changeTarget(commonscat_redirect, summary=u"Update P373 to avoid commons category redirect")
nummodified += 1
except:
print('... but there was a problem doing so!')
# # Sort out the case where the commonscat has been moved, and one of the two hasn't been updated.
# if bad_sitelink and bad_commonscat == 0:
# # We have a bad sitelink
# if commonscat_redirect != "":
# # ... but the commonscat is a redirect
# clm.changeTarget(commonscat_redirect, summary=u"Update P373 to avoid commons category redirect")
# data = {'sitelinks': [{'site': 'commonswiki', 'title': u"Category:" + commonscat_redirect}]}
# page.editEntity(data, summary=u'Update commons sitelink to avoid missing category')
# nummodified += 1
# else:
# # ... and the commonscat is good
# data = {'sitelinks': [{'site': 'commonswiki', 'title': u"Category:" + commonscat}]}
# page.editEntity(data, summary=u'Update commons sitelink to avoid missing category')
# nummodified += 1
# if bad_commonscat and bad_sitelink == 0:
# # We have a bad commonscat
# if sitelink_redirect != "":
# # ... but the sitelink is a redirect
# clm.changeTarget(sitelink_redirect, summary=u"Update P373 to avoid missing commons category")
# data = {'sitelinks': [{'site': 'commonswiki', 'title': u"Category:" + sitelink_redirect}]}
# page.editEntity(data, summary=u'Update commons sitelink to avoid category redirect')
# nummodified += 1
# else:
# # ... and the sitelink is good
# data = {'sitelinks': [{'site': 'commonswiki', 'title': u"Category:" + sitelink}]}
# page.editEntity(data, summary=u'Update commons sitelink to avoid missing category')
# nummodified += 1
# if nummodified >= maxnum:
# print('Reached the maximum of ' + str(maxnum) + ' entries modified, quitting!')
# print('Bad commonscats: ' + str(bad_commonscat_count) + ", bad sitelinks:" + str(bad_sitelink_count))
# print('Interwiki conflicts in: ')
# print(interwiki_conflicts)
# exit()
print(count)
except:
pass
print('Done! Edited ' + str(nummodified) + ' entries')
print('Bad commonscats: ' + str(bad_commonscat_count) + ', bad sitelinks: ' + str(bad_sitelink_count))
print('Interwiki conflicts in: ')
print(interwiki_conflicts)
# EOF