FelixReimann has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/81219


Change subject: Support to add a reference with multiple claims.
......................................................................

Support to add a reference with multiple claims.

site.editSource() accepts now either a single claim (=backward-compatible) for 
a single claim reference
or a list of claims for a multi claim reference

page.addSources() is new and takes a list of claims in contrast to 
page.addSource()

Change-Id: Iac583043531ba3f30985244ffad2aef2ad549f51
---
M pywikibot/page.py
M pywikibot/site.py
2 files changed, 43 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/19/81219/1

diff --git a/pywikibot/page.py b/pywikibot/page.py
index 8816da2..16f32a0 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -2815,7 +2815,7 @@
 
     def getSources(self):
         """
-        Returns a list of Claims
+        Returns a list of sources. Each source is a list of Claims.
         """
         return self.sources
 
@@ -2827,7 +2827,18 @@
         data = self.repo.editSource(self, source, new=True, **kwargs)
         source.hash = data['reference']['hash']
         self.on_item.lastrevid = data['pageinfo']['lastrevid']
-        self.sources.append(source)
+        self.sources.append([source])
+    
+    def addSources(self, sources, **kwargs):
+        """
+        sources are a list of Claims.
+        adds them as one reference.
+        """
+        data = self.repo.editSource(self, sources, new=True, **kwargs)
+        for source in sources:
+          source.hash = data['reference']['hash']
+          self.on_item.lastrevid = data['pageinfo']['lastrevid']
+        self.sources.append(sources)
 
     def _formatDataValue(self):
         """
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 4e5dfaf..2e86a9f 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -3538,22 +3538,37 @@
         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': source._formatDataValue(),
-                         }
-        elif source.getType() == 'string':
-            datavalue = {'type': 'string',
-                         'value': source._formatDataValue(),
-                         }
+        if isinstance(source, list):
+          sources = source
         else:
-            raise NotImplementedError('%s datatype is not supported yet.' % 
claim.getType())
-        snak = {source.getID(): [{'snaktype': 'value',
-                                  'property': source.getID(),
-                                  'datavalue': datavalue,
-                                  },
-                                 ],
-                }
+          sources = [ source]
+
+        snak = {}
+        for src in sources:
+          if src.getType() == 'wikibase-item':
+              datavalue = {'type': 'wikibase-entityid',
+                           'value': src._formatDataValue(),
+                           }
+          elif src.getType() == 'string':
+              datavalue = {'type': 'string',
+                           'value': src._formatDataValue(),
+                           }
+          elif src.getType() == 'time':
+              datavalue = {'type': 'time',
+                           'value': src._formatDataValue(),
+                           }
+          else:
+              raise NotImplementedError('%s datatype is not supported yet.' % 
src.getType())
+          valuesnaks = []
+          if src.getID() in snak:
+            valuesnaks = snak[src.getID()]
+          valuesnaks.append({'snaktype': 'value',
+                             'property': src.getID(),
+                             'datavalue': datavalue,
+                             },
+                            )
+
+          snak[src.getID()] = valuesnaks
         params['snaks'] = json.dumps(snak)
         for arg in kwargs:
             if arg in ['bot', 'lastrevid']:

-- 
To view, visit https://gerrit.wikimedia.org/r/81219
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iac583043531ba3f30985244ffad2aef2ad549f51
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: FelixReimann <fe...@fex-it.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to