jenkins-bot has submitted this change and it was merged.

Change subject: [WIP] Function to create a new item
......................................................................


[WIP] Function to create a new item

Based on the provided page, it would add each of the langlinks
as connected pages.

Some TODOs:

* Option to add aliases based on redirects
* Check against already existing links upon errors
* Use linktitles if one of the links already exists

Change-Id: I5d497926c447d4c08e022c9b924cf028656378fd
---
M pywikibot/site.py
1 file changed, 36 insertions(+), 0 deletions(-)

Approvals:
  Merlijn van Deen: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/site.py b/pywikibot/site.py
index 56492c5..8bf77bb 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -4017,6 +4017,42 @@
         data = req.submit()
         return data
 
+    def createNewItemFromPage(self, page, bot=True, **kwargs):
+        """
+        Create a new Wikibase item for a provided page
+        @param page: page to fetch links from
+        @type page: pywikibot.Page
+        @param bot: whether to mark the edit as bot
+        @return: pywikibot.ItemPage of newly created item
+        """
+        sitelinks = {
+            page.site.dbName(): {
+                'site': page.site.dbName(),
+                'title': page.title(),
+            }
+        }
+        labels = {
+            page.site.language(): {
+                'language': page.site.language(),
+                'value': page.title(),
+            }
+        }
+        for link in page.iterlanglinks():
+            sitelinks[link.site.dbName()] = {
+                'site': link.site.dbName(),
+                'title': link.title,
+            }
+            labels[link.site.language()] = {
+                'language': link.site.language(),
+                'value': link.title,
+            }
+        data = {
+            'sitelinks': sitelinks,
+            'labels': labels,
+        }
+        result = self.editEntity({}, data, bot=bot, **kwargs)
+        return pywikibot.ItemPage(self, result['entity']['id'])
+
     # deprecated BaseSite methods
     def fam(self):
         raise NotImplementedError

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I5d497926c447d4c08e022c9b924cf028656378fd
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to