http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10858
Revision: 10858 Author: drtrigon Date: 2013-01-01 22:40:36 +0000 (Tue, 01 Jan 2013) Log Message: ----------- follow-up; to r10821 more wikidata support for subster.py and new method in wikidataPage class
Modified Paths: -------------- trunk/pywikipedia/subster.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/subster.py =================================================================== --- trunk/pywikipedia/subster.py 2013-01-01 10:55:12 UTC (rev 10857) +++ trunk/pywikipedia/subster.py 2013-01-01 22:40:36 UTC (rev 10858) @@ -537,6 +537,30 @@ else: pywikibot.output(u'Creating new claim with value: %s' % item) outpage.createclaim(prop, item, comment=comment) + # search linked items and update them too + # VERY HACKY, HAS TO BE CONCEPTIONALLY IMPROVED: + # link any "key = value" pair to any other item by adding "key" + # to the items 'aliases' (could also use 'description' or even + # a redirect) + (key, value) = map(string.strip, item.split('=')) + for linked in outpage.searchentities(key): + outpage = pywikibot.wikidataPage(self.site, linked[u'id']) + #attr = outpage.getentities() + attr = linked + if (u'aliases' in attr) and (key in attr[u'aliases']): + pywikibot.output(u'Item %s linked to key %s ...' % (outpage.title(asLink=True), key)) + data = outpage.getentities() + if u'claims' in data: + if (data[u'claims'][u'p32'][0][u'mainsnak'][u'datavalue'][u'value'].strip() == value): + pywikibot.output(u'... ok') + continue + changed = True + pywikibot.output(u'... updating claim with value: %s' % value) + outpage.setclaimvalue(data[u'claims'][u'p32'][0][u'id'], value, comment=comment) + else: + changed = True + pywikibot.output(u'... creating new claim with value: %s' % value) + outpage.createclaim(prop, value, comment=comment) # speed-up by setting everything at once (in one single write attempt) #outpage.editentity(data = {u'claims': data}) #outpage.setitem()
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2013-01-01 10:55:12 UTC (rev 10857) +++ trunk/pywikipedia/wikipedia.py 2013-01-01 22:40:36 UTC (rev 10858) @@ -4048,6 +4048,7 @@
getentity : Getting item(s) of a page getentities : Get the data for multiple Wikibase entities + searchentities : Search for entities
""" def __init__(self, site, *args, **kwargs): @@ -4173,6 +4174,8 @@
def setclaimvalue(self, guid, value, comment=None, token=None, sysop=False, botflag=True): """API module for setting the value of a Wikibase claim. + + (independent of page object and could thus be extracted from this class) """ params = { 'action': 'wbsetclaimvalue', @@ -4350,7 +4353,34 @@
return entities
+ def searchentities(self, search, sysop=False): + """API module to search for entities.
+ (independent of page object and could thus be extracted from this class) + """ + params = { + 'action': 'wbsearchentities', + 'search': search, + #'language': self.site().language(), + 'language': 'en', + } + # retrying is done by query.GetData + data = query.GetData(params, self.site(), sysop=sysop) + search = data['search'] + debuginfo = data['debuginfo'] + + if 'error' in data: + raise RuntimeError("API query error: %s" % data) + pageInfo = search + if 'missing' in pageInfo: + raise NoPage(self.site(), unicode(self), +"Page does not exist. In rare cases, if you are certain the page does exist, look into overriding family.RversionTab") + elif 'invalid' in pageInfo: + raise BadTitle('BadTitle: %s' % self) + + return search + + class ImagePage(Page): """A subclass of Page representing an image descriptor wiki page.