-
Notifications
You must be signed in to change notification settings - Fork 1
/
position_statements.py
231 lines (188 loc) · 7.51 KB
/
position_statements.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
import json
import pywikibot
import sys
import re
import time
from pathlib import Path
# Regular expressions copied from http://tinyurl.com/yb37xlu7
item_or_property_re = re.compile('^[PQ]\d+$', re.IGNORECASE)
item_re = re.compile('^Q\d+$', re.IGNORECASE)
property_re = re.compile('^-?P\d+$', re.IGNORECASE)
string_re = re.compile('^"(.*)"$', re.IGNORECASE)
time_re = re.compile(
'^([+-]{0,1})(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z\/{0,1}(\d*)$',
re.IGNORECASE
)
uuid_re = r'(?P<uuid>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\Z'
statement_re = re.compile(r'(wds:)?(?P<q>Q\d+)[-\$]' + uuid_re, re.I)
def entity_type(value):
if value.startswith('Q'):
return 'item'
elif value.startswith('P'):
return 'property'
else:
return 'unknown'
def parse_value(value):
value = value.strip()
m = item_or_property_re.match(value)
if m is not None:
return {
'type': 'wikibase-entityid',
'value': {
'entity-type': entity_type(value),
'id': value.upper()
}
}
m = string_re.match(value)
if m is not None:
return {'type': 'string', 'value': m.group(1).strip()}
m = time_re.match(value)
if m is not None:
precision = 9
if m.group(8) != '':
precision = int(m.group(8))
return {
'type': 'time',
'value': {
'time': re.sub(r'\/\d+$', '', value),
'precision': precision
}
}
m = statement_re.match(value)
if m:
return {
'type': 'x-wikidata-statementid',
'value': m.group('q') + '$' + m.group('uuid')
}
sys.exit("Unrecognised value: {}".format(value))
def expanded_datavalue(datavalue):
if datavalue['type'] == 'wikibase-entityid':
entity = pywikibot.ItemPage(repo, datavalue['value']['id'])
try:
entity.get()
except pywikibot.IsRedirectPage:
entity = entity.getRedirectTarget()
return entity
elif datavalue['type'] == 'string':
return datavalue['value']
elif datavalue['type'] == 'time':
time = pywikibot.WbTime.fromTimestr(datavalue['value']['time'])
time.precision = datavalue['value']['precision']
return time
def get_existing_claim(item, property_id, statement_uuid):
if property_id not in item.claims:
raise Exception('No property {} found in claims of {}'.format(
property_id, item
))
for claim in item.claims[property_id]:
if claim.snak == statement_uuid:
return claim
msg = 'The snak {statement_uuid} was not found for any claim on ' \
'\'{item}\' with property {property_id}'
raise Exception(msg.format(
statement_uuid=statement_uuid,
item=item,
property_id=property_id,
))
if __name__ == '__main__':
if len(sys.argv) == 1:
sys.exit("Usage: %s <filename> [<user_for_attribution>]" % sys.argv[0])
p = Path(sys.argv[1])
if not p.is_file():
sys.exit('"%s" is not a file' % sys.argv[1])
try:
user_name = sys.argv[2]
except IndexError:
user_name = None
site = pywikibot.Site()
repo = site.data_repository()
with p.open() as f:
statements_string = f.read()
statements = [s for s in statements_string.split("\n") if s.strip() != '']
commands = []
# Parse statements
for statement in statements:
command = {}
parts = [s.strip() for s in statement.split("\t")]
command['item'] = parts[0]
command['property'] = parts[1]
command['datavalue'] = parse_value(parts[2])
qualifiers = parts[3:]
qualifier_pairs = list(zip(qualifiers[::2], qualifiers[1::2]))
command['qualifiers'] = []
command['sources'] = []
for p, v in qualifier_pairs:
what = 'sources' if p.startswith('S') else 'qualifiers'
prop = re.sub(r'^S', 'P', p)
q = {'property': prop, 'datavalue': parse_value(v)}
command[what].append(q)
# Validate statements
m = item_re.match(command['item'])
if m is None:
sys.exit("Invalid item ID format: {}".format(command['item']))
if command['property'] != 'P39':
sys.exit("Only P39 statements are supported currently. (Got {})".format(command['property']))
if len(qualifiers) % 2 != 0:
sys.exit("Odd number of qualifier/source pairs detected: {}".format(qualifiers))
for qualifier in command['qualifiers']:
m = property_re.match(qualifier['property'])
if m is None:
sys.exit("Invalid qualifier property: {}".format(qualifier['property']))
for source in command['sources']:
m = property_re.match(source['property'])
if m is None:
sys.exit("Invalid source property: {}".format(source['property']))
# Validations passed, add to list of commands.
commands.append(command)
for command in commands:
print(json.dumps(command, indent=4, sort_keys=True))
summary = 'Edited with PositionStatements'
if user_name:
summary += ' on behalf of [[User:{}]]'.format(user_name)
# Get the item we want to modify
item = pywikibot.ItemPage(repo, command['item'])
try:
item.get()
except pywikibot.IsRedirectPage:
item = item.getRedirectTarget()
if command['datavalue']['type'] == 'x-wikidata-statementid':
claim = get_existing_claim(
item,
command['property'],
command['datavalue']['value'],
)
else:
# Get the claim we're dealing with
claim = pywikibot.Claim(site, command['property'])
# Create a new P39 statement pointing to Q123
claim.setTarget(expanded_datavalue(command['datavalue']))
# Add the claim to the item.
item.addClaim(claim, summary=summary)
for qualifier in command['qualifiers']:
# Check to see if this is actually a remove
if qualifier['property'][:1] == '-':
qualifier['property'] = qualifier['property'][1:]
# We don't use has_qualifier here because that only returns a
# bool, not the qualifier itself, so we'd still have to go find
# it in order to remove it.
for property_id, claim_qualifiers in claim.qualifiers.items():
if property_id == qualifier['property']:
# Now unpack the list...
for single_qualifier in claim_qualifiers:
if single_qualifier.getTarget() == expanded_datavalue(qualifier['datavalue']):
claim.removeQualifier(single_qualifier)
else:
qualifier_claim = pywikibot.Claim(
site, qualifier['property'], isQualifier=True
)
qualifier_claim.setTarget(expanded_datavalue(qualifier['datavalue']))
claim.addQualifier(qualifier_claim, summary=summary)
sources = []
for source in command['sources']:
source_claim = pywikibot.Claim(
site, source['property'], isReference=True
)
source_claim.setTarget(expanded_datavalue(source['datavalue']))
sources.append(source_claim)
if sources:
claim.addSources(sources, summary=summary)