jenkins-bot has submitted this change and it was merged.
Change subject: Detect attempt to fetch wikibase item of an entity ......................................................................
Detect attempt to fetch wikibase item of an entity
While the wikidata and wikispecies families dont allow transcluded data, the current code tries to find a wikibase item for wikidata pages, which causes a warning like the following and raises an APIError exception. WARNING: API warning (wbgetentities): Unrecognized value for parameter 'sites': testwikidatawiki
This change detects whn ItemPage.fromPage is being asked to load an item for a page on a site that cant have an item, and raises WikiBaseError instead of APIError.
Change-Id: Ie89289536a777b8f6bb92b09d544c13cf32f5df4 --- M pywikibot/page.py M tests/wikibase_tests.py 2 files changed, 33 insertions(+), 3 deletions(-)
Approvals: John Vandenberg: Looks good to me, but someone else must approve Legoktm: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 266adb5..4675a72 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -2836,6 +2836,9 @@ @param page: Page @return: ItemPage """ + if not page.site.has_transcluded_data: + raise pywikibot.WikiBaseError(u'%s has no transcluded data' + % page.site) repo = page.site.data_repository() if hasattr(page, '_pageprops') and page.properties().get('wikibase_item'): diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index 6487b55..4059c4a 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -114,6 +114,33 @@ pass self.assertIsInstance(MyItemPage.fromPage(mainpage), MyItemPage)
+ def test_not_supported_family(self): + """Test that family without a data repository causes error.""" + # Wikispecies is not supported by Wikidata yet. + species_site = pywikibot.Site('species', 'species') + self.wdp = pywikibot.Page(species_site, 'Main Page') + self.assertRaises(pywikibot.WikiBaseError, + pywikibot.ItemPage.fromPage, self.wdp) + self.assertRaisesRegexp(pywikibot.WikiBaseError, + 'species.*no transcluded data', + self.wdp.data_item) + + # test.wikidata does not have a data repository. + self.wdp = pywikibot.ItemPage(wikidatatest, 'Q6') + self.assertRaises(pywikibot.WikiBaseError, + pywikibot.ItemPage.fromPage, self.wdp) + + # The main Wikidata also does not have a data repository. + # It is a data repository, but no pages on Wikidata have + # a linked page. + self.wdp = pywikibot.ItemPage(wikidata, 'Q60') + self.assertRaises(pywikibot.WikiBaseError, + pywikibot.ItemPage.fromPage, self.wdp) + + self.wdp = pywikibot.Page(wikidata, 'Main Page', ns=4) + self.assertRaises(pywikibot.WikiBaseError, + pywikibot.ItemPage.fromPage, self.wdp) +
class TestItemLoad(PywikibotTestCase): """Test each of the three code paths for item creation: @@ -342,10 +369,10 @@ class TestPageMethods(PywikibotTestCase): """Test cases to test methods of Page() behave correctly with Wikibase"""
- def test_item_save(self): + def test_page_methods(self): + """Test ItemPage methods inherited from superclass Page.""" self.wdp = pywikibot.ItemPage(wikidatatest, 'Q6') - item = self.wdp.data_item() - self.assertRaises(pywikibot.NoPage, item.title) + self.assertRaises(pywikibot.WikiBaseError, self.wdp.data_item) self.assertRaises(pywikibot.PageNotSaved, self.wdp.save) self.wdp.previousRevision() self.assertEquals(self.wdp.langlinks(), [])
pywikibot-commits@lists.wikimedia.org