http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11240
Revision: 11240
Author: legoktm
Date: 2013-03-23 08:09:19 +0000 (Sat, 23 Mar 2013)
Log Message:
-----------
Add support for adding sources to claims
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-23 06:22:29 UTC (rev 11239)
+++ branches/rewrite/pywikibot/page.py 2013-03-23 08:09:19 UTC (rev 11240)
@@ -2613,12 +2613,14 @@
"""
return self.sources
- def addSource(self, source):
+ def addSource(self, source, **kwargs):
"""
source is a Claim.
adds it as a reference.
"""
- raise NotImplementedError
+ data = self.repo.editSource(self, source, new=True, **kwargs)
+ source.hash = data['reference']['hash']
+ self.sources.append(source)
class Revision(object):
Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py 2013-03-23 06:22:29 UTC (rev 11239)
+++ branches/rewrite/pywikibot/site.py 2013-03-23 08:09:19 UTC (rev 11240)
@@ -3409,9 +3409,55 @@
data = req.submit()
return data
+ def editSource(self, claim, source, new=False, **kwargs):
+ """
+ Create/Edit a source.
+ @param claim A Claim object to add the source to
+ @type claim pywikibot.Claim
+ @param source A Claim object to be used as a source
+ @type source pywikibot.Claim
+ @param new Whether to create a new one if the "source" already exists
+ @type new bool
+ """
+ if claim.isReference:
+ raise ValueError("The claim cannot be a reference.")
+ params = dict(action='wbsetreference',
+ statement=claim.snak,
+ )
+ params['token'] = self.token(claim, 'edit')
+ if not new and hasattr(source, 'hash'):
+ params['reference'] = source.hash
+ #build up the snak
+ if source.getType() == 'wikibase-item':
+ datavalue = {'type': 'wikibase-entityid',
+ 'value': {'entity-type': 'item',
+ 'numeric-id':
source.getTarget().getID(numeric=True),
+ },
+ }
+ elif source.getType() == 'string':
+ datavalue = {'type': 'string',
+ 'value': source.getTarget(),
+ }
+ else:
+ raise NotImplementedError('%s datatype is not supported yet.' %
claim.getType())
+ snak = {source.getID(): [{'snaktype': 'value',
+ 'property': source.getID(),
+ 'datavalue': datavalue,
+ },
+ ],
+ }
+ params['snak'] = json.dumps(snak)
+ for arg in kwargs:
+ 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 mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikipedia-svn