http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10819
Revision: 10819 Author: drtrigon Date: 2012-12-23 19:45:38 +0000 (Sun, 23 Dec 2012) Log Message: ----------- new features; setclaimvalue, createclaim and getentities for wikidataPage
Modified Paths: -------------- trunk/pywikipedia/config.py trunk/pywikipedia/query.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/config.py =================================================================== --- trunk/pywikipedia/config.py 2012-12-23 19:22:04 UTC (rev 10818) +++ trunk/pywikipedia/config.py 2012-12-23 19:45:38 UTC (rev 10819) @@ -479,7 +479,9 @@ # servers. Allows simulation runs of bots to be carried out without changing any # page on the server side. This setting may be overridden in user_config.py. actions_to_block = ['edit', 'watch', 'move', 'delete', 'undelete', 'protect', - 'emailuser', 'submit'] # 'submit' is for non-API + 'emailuser', + 'submit', # for non-API + 'wbcreateclaim', 'wbsetclaimvalue'] # for Wikidata
# How many pages should be put to a queue in asynchroneous mode. # If maxsize is <= 0, the queue size is infinite.
Modified: trunk/pywikipedia/query.py =================================================================== --- trunk/pywikipedia/query.py 2012-12-23 19:22:04 UTC (rev 10818) +++ trunk/pywikipedia/query.py 2012-12-23 19:45:38 UTC (rev 10819) @@ -106,7 +106,7 @@ postAC = [ 'edit', 'login', 'purge', 'rollback', 'delete', 'undelete', 'protect', 'parse', 'block', 'unblock', 'move', 'emailuser','import', 'userrights', - 'upload', 'patrol' + 'upload', 'patrol', 'wbcreateclaim' ] if site.versionnumber() >= 18: postAC.append('watch')
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2012-12-23 19:22:04 UTC (rev 10818) +++ trunk/pywikipedia/wikipedia.py 2012-12-23 19:45:38 UTC (rev 10819) @@ -4043,15 +4043,18 @@ Supports the same interface as Page, with the following added methods:
setitem : Setting item(s) on a page + setclaimvalue : Set the value of a Wikibase claim + createclaim : Create Wikibase claims
getentity : Getting item(s) of a page + getentities : Get the data for multiple Wikibase entities
""" def __init__(self, site, *args, **kwargs): if isinstance(site, basestring): site = getSite(site) self._originSite = site - Page.__init__(self, site.data_repository(), *args, **kwargs) + Page.__init__(self, site, *args, **kwargs)
def setitem(self, summary=None, watchArticle=False, minorEdit=True, newPage=False, token=None, newToken=False, sysop=False, @@ -4168,6 +4171,57 @@ return 302, response.msg, data['success'] return response.code, response.msg, data
+ def setclaimvalue(self, guid, value, comment=None, token=None, sysop=False, botflag=True): + """API module for setting the value of a Wikibase claim. + """ + params = { + 'action': 'wbsetclaimvalue', + 'claim': guid, + 'snaktype': 'value', + 'value': value, + } + if token: + params['token'] = token + else: + params['token'] = self.site().getToken(sysop = sysop) + #if botflag: + # params['bot'] = 1 + # retrying is done by query.GetData + data = query.GetData(params, self.site(), sysop=sysop) + + if 'error' in data: + raise RuntimeError("API query error: %s" % data) + if u'warnings' in data: + output(str(data[u'warnings'])) + + return + + def createclaim(self, prop, value, comment=None, token=None, sysop=False, botflag=True): + """API module for creating Wikibase claims. + """ + params = { + 'action': 'wbcreateclaim', + 'entity': self.title(), + 'snaktype': 'value', + 'property': prop, + 'value': value, + } + if token: + params['token'] = token + else: + params['token'] = self.site().getToken(sysop = sysop) + #if botflag: + # params['bot'] = 1 + # retrying is done by query.GetData + data = query.GetData(params, self.site(), sysop=sysop) + + if 'error' in data: + raise RuntimeError("API query error: %s" % data) + if u'warnings' in data: + output(str(data[u'warnings'])) + + return + def getentity(self,force=False, get_redirect=False, throttle=True, sysop=False, change_edit_time=True): """Returns items of a entity in a dictionary @@ -4273,7 +4327,30 @@ raise SectionError # Page has no section by this name return pagetext
+ def getentities(self, sysop=False): + """API module to get the data for multiple Wikibase entities. + """ + params = { + 'action': 'wbgetentities', + 'ids': self.title(), + } + # retrying is done by query.GetData + data = query.GetData(params, self.site(), sysop=sysop) + entities = data['entities'][self.title().lower()] + debuginfo = data['debuginfo']
+ if 'error' in data: + raise RuntimeError("API query error: %s" % data) + pageInfo = entities + 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 entities + + class ImagePage(Page): """A subclass of Page representing an image descriptor wiki page.
pywikipedia-svn@lists.wikimedia.org