-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwikiFunctions.py
381 lines (322 loc) · 10.7 KB
/
wikiFunctions.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
import pywikibot
from pywikibot.data import api
import wikiSQL as SQL
import re
'''
Items
'''
def getItem(site, wdItem, token):
request = api.Request(site=site,
action='wbgetentities',
format='json',
ids=wdItem)
return request.submit()
def getItems(site, itemtitle, language):
'''
Query Params for Items
site --
itemtitle --
language -- en, de, fr
'''
params = { 'action' :'wbsearchentities' , 'format' : 'json' , 'language' : language, 'type' : 'item', 'search': itemtitle}
request = api.Request(site=site,**params)
return request.submit()
def CreateItem(repo, ENDPOINT, row):
'''
creates a new item
row (list) - list with title, description,
'''
#TODO: quatsch? anders
new_item = pywikibot.ItemPage(repo)
if 'FR MiMoText' in row and row['FR MiMoText'] != '':
fr = row['FR MiMoText']
elif 'FR' in row and row['FR'] != '':
fr = row['FR']
else:
fr = ''
if 'EN MiMoText' in row and row['EN MiMoText'] != '':
en = row['EN MiMoText']
elif 'EN' in row and row['EN'] != '':
en = row['EN']
else:
en = ''
if 'DE MiMoText' in row and row['DE MiMoText'] != '':
de = row['DE MiMoText']
elif 'DE' in row and row['DE'] != '':
de = row['DE']
else:
de = ''
label_dict = {'fr': fr, 'de': de, 'en': en}
new_item.editLabels(labels=label_dict, summary="Setting labels")
if 'description-fr' in row or 'description-de' in row or 'description-en' in row:
desc_dict = {'fr': row['description-fr'], 'de': row['description-de'], 'en': row['description-en']}
new_item.editDescriptions(descriptions=desc_dict, summary="Setting descriptions")
return new_item
def CreateAuthor(name, repo):
'''
Create author with name as Label for en,de,fr
name (string) -- author name
'''
#Setting Labels
new_item = pywikibot.ItemPage(repo)
label_dict = {'fr': name, 'de': name, 'en':name}
new_item.editLabels(labels=label_dict, summary="Setting labels")
#Add name as monolingual Statement
new_claim = pywikibot.Claim(repo, 'p9')
target = pywikibot.WbMonolingualText(name, "fr")
new_claim.setTarget(target)
new_item.addClaim(new_claim, summary="Adding claim P9")
#Set (P2) instance of (Q10)) human
new_claim = pywikibot.Claim(repo, 'P2')
target = pywikibot.ItemPage(repo, 'Q10')
new_claim.setTarget(target)
new_item.addClaim(new_claim, summary="Adding claim P2")
#Set (P30) occupation (Q15) author
new_claim = pywikibot.Claim(repo, 'P30')
target = pywikibot.ItemPage(repo, 'Q15')
new_claim.setTarget(target)
new_item.addClaim(new_claim, summary="Adding claim P9")
return new_item.getID()
def CreateWerk(title, repo):
'''
Create novel with title as Label for en,de,fr
title (string) -- title of novel
'''
#Setting Labels
new_item = pywikibot.ItemPage(repo)
label_dict = {'fr': title, 'de': title, 'en':title}
new_item.editLabels(labels=label_dict, summary="Setting labels")
#Set (P2) instance of (Q2) literary work (P14) stated in (Q1)
new_ref = pywikibot.Claim(repo, 'P14', True)
ref_target = pywikibot.ItemPage(repo, 'Q1')
new_ref.setTarget(ref_target)
sources = []
sources.append(new_ref)
new_claim = pywikibot.Claim(repo, 'P2')
target = pywikibot.ItemPage(repo, 'Q2')
new_claim.setTarget(target)
new_claim.addSources(sources, summary="Adding reference Q1")
new_item.addClaim(new_claim, summary="Adding claim P2")
#Set (P39) genre (Q12) fictional prose (P14) stated in (Q1)
new_ref = pywikibot.Claim(repo, 'P14', True)
ref_target = pywikibot.ItemPage(repo, 'Q1')
new_ref.setTarget(ref_target)
sources = []
sources.append(new_ref)
new_claim = pywikibot.Claim(repo, 'P39')
target = pywikibot.ItemPage(repo, 'Q12')
new_claim.setTarget(target)
new_claim.addSources(sources, summary="Adding reference Q1")
new_item.addClaim(new_claim, summary="Adding claim P39")
return new_item
'''
Properties
'''
def getProperties(site, itemtitle):
'''
Query Params for Property
'''
params = { 'action' :'wbsearchentities' , 'format' : 'json' , 'language' : 'en', 'type' : 'property', 'search': itemtitle}
request = api.Request(site=site,**params)
return request.submit()
def GetProperty(site, item):
'''
return PID of Property
'''
wikiEntries = getProperties(site, item)
if wikiEntries['search']:
QID = 0
for elem in wikiEntries['search']:
if elem['label'] == item:
QID = elem['id']
return QID
return
def CreateProperty(repo, title, datatypeProp):
'''
creates a new property
title (string) - property name
datatypeProp (string) - type of property, need to be wikibase compliant
'''
# TODO: Exchange CheckForProperty with SparQL search?
if CheckForProperty(repo, title):
new_item = pywikibot.PropertyPage(repo,datatype=datatypeProp)
label_dict = {'fr': title, 'de': title, 'en':title}
new_item.editLabels(labels=label_dict, summary='Setting labels')
return new_item.getID()
return
def CreateReferenz(repo, ENDPOINT, claim, ref_prop, ref_target):
'''
add referenz to given claim
claim (claim) -- claim to add the reference
ref_prop (string) -- PID
ref_Target (string) -- QID or PID
'''
if ref_target[:1] != 'Q':
ref_target = SQL.GetEntryOverSPARQL(ENDPOINT,ref_target, SQL.SparQL_Mode.QID)
targetFound = False
for claimSources in claim.getSources():
for referenceProperty, referenceClaims in claimSources.items():
if referenceProperty == ref_prop:
for referenceClaim in referenceClaims:
if ref_target == referenceClaim.getTarget().id:
targetFound = True
if not targetFound:
target = pywikibot.ItemPage(repo, ref_target)
ref = pywikibot.Claim(repo, ref_prop, True)
ref.setTarget(target)
sources = []
sources.append(ref)
claim.addSources(sources, summary="Adding reference " + ref_target)
def CreateClaim(repo, ENDPOINT, item, prop, target, references = None):
#TODO change description after
#TODO check if claim is already set
'''
Add a new Claim to given Item
Keyword arguments:
item (ItemPage) -- given item
prop (str) -- PID of statement property
target (str) -- QID of linked target
references (list of tuples) -- (reference_property, reference_target) default None
'''
#print(item, prop, target, references)
targetFound = False
itemClaims = item.get()['claims']
for claimProperty, claims in itemClaims.items():
if claimProperty == prop:
for claim in claims:
if target == "99.11": #TODO: different werk
claimTargetId = None
else:
claimTargetId = claim.getTarget().id
if claimTargetId == target:
targetFound = True
if references:
for ref in references:
CreateReferenz(repo, ENDPOINT, claim, ref[0], ref[1])
if not targetFound:
print(repo)
print(prop)
propPage= pywikibot.PropertyPage(repo, prop)
propPage.get()
print(propPage._type)
claim = ''
claim = pywikibot.Claim(repo, prop)
print(prop)
sTarget = SetTarget(repo,ENDPOINT, prop, target)
if sTarget:
claim.setTarget(sTarget)
if references:
for ref in references:
if target and ref[0] and ref[1]:
CreateReferenz(repo,ENDPOINT,claim, ref[0], ref[1])
item.addClaim(claim, summary='Adding claim ' + prop)
def getClaim(item, pid, ENDPOINT=None, REPO=None, target=None):
#print(item)
#TODO: items with more then one matching property
itemClaims = item.get()['claims']
for claimProperty, claims in itemClaims.items():
if claimProperty == pid:
for claim in claims:
if not REPO or not ENDPOINT:
return claim
if not target:
return None
if re.match(r"^Q\d+$", target):
qid = target
else:
propertyPage = pywikibot.PropertyPage(REPO, pid)
propertyPage.get(True)
if propertyPage._type == "wikibase-item":
qid = SQL.GetEntryOverSPARQL(ENDPOINT, target)
if not qid:
qid = SQL.GetEntryOverSPARQL(ENDPOINT, target, lang="fr")
if not qid:
qid = SQL.GetEntryOverSPARQL(ENDPOINT, target, lang="de")
if claim.target_equals(qid):
return claim
return None
def SetTarget(repo,ENDPOINT, prop, target):
'''
Set Target based on property Type
prop (string) -- property to be linked
target (string) -- target of the link
'''
propertyPage = pywikibot.PropertyPage(repo, prop)
propertyPage.get()
if propertyPage._type == 'wikibase-item':
print(target, "TARGET")
if not re.match(r"Q\d+$", target):
t1 = SQL.GetEntryOverSPARQL(ENDPOINT, target, lang='fr')
target = SQL.GetEntryOverSPARQL(ENDPOINT,target)
if not target:
target = t1
if not target:
return
print(target)
return pywikibot.ItemPage(repo, target)
elif propertyPage._type == 'wikibase-property':
return pywikibot.PropertyPage(repo, target)
elif propertyPage._type == 'monolingualtext':
return pywikibot.WbMonolingualText(target, "fr")
elif propertyPage._type == 'time':
return pywikibot.WbTime(int(target), calendarmodel = 'http://www.wikidata.org/entity/Q1985727')
elif propertyPage._type == 'external_id':
return target
elif propertyPage._type == 'url':
return target
elif propertyPage._type == 'commonsMedia':
return target
elif propertyPage._type == 'quantity':
return pywikibot.WbQuantity(target)
elif propertyPage._type == 'globe-coordinate':
print("Coordinates")
coordinates = target.split(',')
lat = coordinates[0]
lon = coordinates[1]
precision = "0.000001"
print(lat, lon, precision)
coord = pywikibot.Coordinate(lat, lon, precision=precision)
print(str(coord))
return coord
else:
return target
def CheckForEntry(site, item):
'''
check if item is in wikibase
'''
wikiEntries = getItems(site, item, 'en')
if not wikiEntries['search']:
return True
else:
itemFound = False
for elem in wikiEntries['search']:
if elem['label'] == item:
itemFound = True
return not itemFound
def CheckForProperty(site, elem):
'''
check if property is in wikibse
'''
wikiEntries = getProperties(site, elem)
if not wikiEntries['search']:
return True
else:
itemFound = False
for e in wikiEntries['search']:
if e['label'] == elem:
itemFound = True
return not itemFound
def GetEntry(site, item, lang):
'''
return QID for item
lang -- en, de, fr
TODO: change to SparQL?
'''
wikiEntries = getItems(site, item, lang)
if wikiEntries['search']:
QID = 0
for elem in wikiEntries['search']:
if elem['match']['text'] == item:
QID = elem['id']
return QID
return