http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11235
Revision: 11235 Author: legoktm Date: 2013-03-22 07:12:16 +0000 (Fri, 22 Mar 2013) Log Message: ----------- Add support for changing the target of a claim. Some minor PEP8 fixes.
Modified Paths: -------------- branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2013-03-22 05:35:23 UTC (rev 11234) +++ branches/rewrite/pywikibot/page.py 2013-03-22 07:12:16 UTC (rev 11235) @@ -2231,7 +2231,6 @@ return False return 'lastrevid' in self._content
- def get(self, force=False, *args): """ Fetches all page data, and caches it @@ -2422,7 +2421,6 @@ 'claims': self.claims }
- def getSitelink(self, site, force=False): """ Returns a page object for the specific site @@ -2451,6 +2449,7 @@ or it can be a dbName. """ self.removeSitelinks([site], **kwargs) + def removeSitelinks(self, sites, **kwargs): """ Sites should be a list, with values either @@ -2589,6 +2588,18 @@ """ self.target = value
+ def changeTarget(self, value=None, snaktype='value', **kwargs): + """ + This actually saves the new target. + """ + if value: + self.target = value + + data = self.repo.changeClaimTarget(self, snaktype=snaktype, + **kwargs) + #TODO: Re-create the entire item from JSON, not just id + self.snak = data['claim']['id'] + def getTarget(self): """ Returns object that the property is associated with. @@ -2610,7 +2621,6 @@ raise NotImplementedError
- class Revision(object): """A structure holding information about a single revision of a Page.""" def __init__(self, revid, timestamp, user, anon=False, comment=u"",
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2013-03-22 05:35:23 UTC (rev 11234) +++ branches/rewrite/pywikibot/site.py 2013-03-22 07:12:16 UTC (rev 11235) @@ -3376,8 +3376,42 @@ item.claims[claim.getID()] = [claim] item.lastrevid = data['pageinfo']['lastrevid']
+ def changeClaimTarget(self, claim, snaktype='value', **kwargs): + """ + Sets the claim target to whatever claim.target is + An optional snaktype lets you set a novalue or somevalue. + """ + if claim.isReference: + raise NotImplementedError + if not claim.snak: + #We need to already have the snak value + raise pywikibot.NoPage(claim) + params = dict(action='wbsetclaimvalue', + claim=claim.snak, + snaktype=snaktype, + ) + params['token'] = self.token(claim, 'edit') + if snaktype == 'value': + #This code is repeated from above, maybe it should be it's own function? + if claim.getType() == 'wikibase-item': + params['value'] = json.dumps({'entity-type': 'item', + 'numeric-id': claim.getTarget().getID(numeric=True)}) + elif claim.getType() == 'string': + params['value'] = '"' + claim.getTarget() + '"' + else: + raise NotImplementedError('%s datatype is not supported yet.' % claim.getType())
+ for arg in kwargs: + #TODO: Get the lastrevid from the item + if arg in ['bot','lastrevid']: + params[arg] = kwargs[arg] + req = api.Request(site=self, **params) + data = req.submit() + return data
+ + + # deprecated BaseSite methods def fam(self): raise NotImplementedError
pywikipedia-svn@lists.wikimedia.org