Legoktm has uploaded a new change for review.

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


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(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/79/85179/1

diff --git a/pywikibot/site.py b/pywikibot/site.py
index 13477b0..2f8e4be 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -3681,6 +3681,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: newchange
Gerrit-Change-Id: I5d497926c447d4c08e022c9b924cf028656378fd
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

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

Reply via email to