Dachary has uploaded a new change for review.

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

Change subject: plugin: lookup wikipedia item from dbname
......................................................................

plugin: lookup wikipedia item from dbname

Helper for adding "import from" sources.

Change-Id: Ibe282bbdd82015566bb28d945efd163e0e935ab6
Signed-off-by: Loic Dachary <l...@dachary.org>
---
M FLOSSbot/plugin.py
M tests/test_plugin.py
2 files changed, 27 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/bots/FLOSSbot 
refs/changes/52/316052/1

diff --git a/FLOSSbot/plugin.py b/FLOSSbot/plugin.py
index 0be598c..5ee7ea4 100644
--- a/FLOSSbot/plugin.py
+++ b/FLOSSbot/plugin.py
@@ -21,6 +21,7 @@
 
 import pywikibot
 import requests
+from pywikibot import pagegenerators as pg
 
 log = logging.getLogger(__name__)
 
@@ -31,6 +32,7 @@
         self.args = args
         self.bot = bot
         self.reset_cache()
+        self.dbname2item = {}
 
     @staticmethod
     def get_parser():
@@ -274,3 +276,20 @@
         except Exception as e:
             log.debug("GET failed with " + str(e))
             return None
+    def get_sitelink_item(self, dbname):
+        if dbname not in self.dbname2item:
+            query = """
+            SELECT DISTINCT ?item WHERE {{
+              ?item wdt:{Wikimedia_database_name} '{dbname}'.
+            }}
+            """.format(
+                Wikimedia_database_name=self.P_Wikimedia_database_name,
+                dbname=dbname,
+            )
+            for item in pg.WikidataSPARQLPageGenerator(
+                    query, site=self.bot.site):
+                item.get()
+                assert dbname == item.claims[
+                    self.P_Wikimedia_database_name][0].getTarget()
+                self.dbname2item[dbname] = item
+        return self.dbname2item[dbname]
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index 1a45398..f4cb5b5 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -150,3 +150,11 @@
         Plugin.authoritative['test'][name] = second.getID()
         found = plugin.search_entity(plugin.bot.site, name, type='item')
         assert found.getID() == second.getID()
+
+    def test_get_sitelink_item(self):
+        bot = Bot.factory(['--verbose'])
+        plugin = Plugin(bot, bot.args)
+        enwiki = plugin.get_sitelink_item('enwiki')
+        assert 'English Wikipedia' == enwiki.labels['en']
+        frwiki = plugin.get_sitelink_item('frwiki')
+        assert 'French Wikipedia' == frwiki.labels['en']

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibe282bbdd82015566bb28d945efd163e0e935ab6
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/bots/FLOSSbot
Gerrit-Branch: master
Gerrit-Owner: Dachary <l...@dachary.org>

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

Reply via email to