jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/266702 )
Change subject: [L10N] Introduce a new site method page_from_repository() ......................................................................
[L10N] Introduce a new site method page_from_repository()
This method returns a Page or Category object for a given site and item number. This could easily be used to localize scripts for several sites:
A sample for de-wiki looks like:
import pwb, pywikibot as py s = py.Site() p = s.page_from_repository('Q131303') p
Page(Hallo-Welt-Programm)
- some tests added
Change-Id: If2c4b0e8d0b8499c4df1879a63d0dbad7643ff82 --- M pywikibot/site.py M tests/site_tests.py M tests/wikibase_tests.py 3 files changed, 126 insertions(+), 0 deletions(-)
Approvals: Dalba: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 4758cec..abd7e6f 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -2790,6 +2790,37 @@ """Return True if its data repository is itself.""" return self is self.data_repository()
+ def page_from_repository(self, item): + """ + Return a Page for this site object specified by wikibase item. + + @param item: id number of item, "Q###", + @type item: str + @return: Page, or Category object given by wikibase item number + for this site object. + @rtype: pywikibot.Page or None + + @raises UnknownExtension: site has no wikibase extension + @raises NotimplementedError: method not implemented for a wikibase site + """ + if not self.has_data_repository: + raise UnknownExtension( + 'Wikibase is not implemented for {0}.'.format(self)) + if self.is_data_repository(): + raise NotImplementedError( + 'page_from_repository method is not implemented for ' + 'Wikibase {0}.'.format(self)) + repo = self.data_repository() + dp = pywikibot.ItemPage(repo, item) + try: + page_title = dp.getSitelink(self) + except pywikibot.NoPage: + return None + page = pywikibot.Page(self, page_title) + if page.namespace() == Namespace.CATEGORY: + page = pywikibot.Category(page) + return page + def nice_get_address(self, title): """Return shorter URL path to retrieve page titled 'title'.""" # 'title' is expected to be URL-encoded already diff --git a/tests/site_tests.py b/tests/site_tests.py index 4b44516..3d7801e 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -3342,6 +3342,82 @@ self.assertIn(item, pnames)
+class TestPageFromWikibase(DefaultSiteTestCase): + + """Test page_from_repository method.""" + + sites = { + 'it.wb': { + 'family': 'wikibooks', + 'code': 'it', + 'result': 'Hello world', + }, + 'de.wp': { + 'family': 'wikipedia', + 'code': 'de', + 'result': 'Hallo-Welt-Programm', + }, + 'en.wp': { + 'family': 'wikipedia', + 'code': 'en', + 'result': '"Hello, World!" program', + }, + } + + ITEM = 'Q131303' + + def test_page_from_repository(self, key): + """Validate page_from_repository.""" + site = self.get_site(key) + page = site.page_from_repository(self.ITEM) + self.assertIsInstance(page, pywikibot.Page) + self.assertEqual(page.title(), self.sites[key]['result']) + + def test_page_from_repository_none(self): + """Validate page_from_repository return NoneType.""" + site = pywikibot.Site('pdc', 'wikipedia') + page = site.page_from_repository(self.ITEM) + self.assertIsNone(page) + + +class TestCategoryFromWikibase(DefaultSiteTestCase): + + """Test page_from_repository method.""" + + sites = { + 'it.wb': { + 'family': 'wikinews', + 'code': 'it', + 'result': 'Categoria:2016', + }, + 'de.wp': { + 'family': 'wikipedia', + 'code': 'de', + 'result': 'Kategorie:2016', + }, + 'en.wp': { + 'family': 'wikipedia', + 'code': 'en', + 'result': 'Category:2016', + }, + } + + ITEM = 'Q6939656' + + def test_page_from_repository(self, key): + """Validate page_from_repository.""" + site = self.get_site(key) + page = site.page_from_repository(self.ITEM) + self.assertIsInstance(page, pywikibot.Category) + self.assertEqual(page.title(), self.sites[key]['result']) + + def test_page_from_repository_none(self): + """Validate page_from_repository return NoneType.""" + site = pywikibot.Site('pdc', 'wikipedia') + page = site.page_from_repository(self.ITEM) + self.assertIsNone(page) + + if __name__ == '__main__': # pragma: no cover try: unittest.main() diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index 79a70d8..0006172 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -1425,6 +1425,13 @@ item = ItemPage.fromPage(page) self.assertEqual(item.site, site)
+ def test_page_from_repository_fails(self, key): + """Test that page_from_repository method fails.""" + site = self.get_site(key) + dummy_item = 'Q1' + self.assertRaises(NotImplementedError, + site.page_from_repository, dummy_item) +
class TestUnconnectedClient(TestCase):
@@ -1458,6 +1465,18 @@ 'no data repository', self.wdp.data_item)
+ def test_has_data_repository(self, key): + """Test that site has no data repository.""" + site = self.get_site(key) + self.assertFalse(site.has_data_repository) + + def test_page_from_repository_fails(self, key): + """Test that page_from_repository method fails.""" + site = self.get_site(key) + dummy_item = 'Q1' + self.assertRaises(pywikibot.UnknownExtension, + site.page_from_repository, dummy_item) +
class TestJSON(WikidataTestCase):
pywikibot-commits@lists.wikimedia.org