jenkins-bot has submitted this change and it was merged.
Change subject: [FEAT] WikidataBot: Allow direct usage on Wikidata ......................................................................
[FEAT] WikidataBot: Allow direct usage on Wikidata
The current WikidataBot requires that the generator works on pages and that the ItemPages are created from them. But if the generator is iterating over ItemPages from Wikidata it should use those directly.
There are three possible modes: It either always uses the ItemPage.fromPage method or ItemPage constructor or it uses ItemPage.fromPage only when the page is not in the item namespace.
Change-Id: I8b3f27c9e559dfbeb4a557a0b209b2e978ada5aa --- M pywikibot/bot.py M scripts/claimit.py 2 files changed, 32 insertions(+), 6 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved M4tx: Looks good to me, but someone else must approve jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 21eb4a4..d56b526 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -1133,7 +1133,17 @@ """
def __init__(self, **kwargs): - """Constructor.""" + """ + Constructor of the WikidataBot. + + @kwarg use_from_page: If True (default) it will apply ItemPage.fromPage + for every item. If False it assumes that the pages are actually + already ItemPage (page in treat will be None). If None it'll use + ItemPage.fromPage when the page is not in the site's item + namespace. + @kwtype use_from_page: bool, None + """ + self.use_from_page = kwargs.pop('use_from_page', True) super(WikidataBot, self).__init__(**kwargs) self.site = pywikibot.Site() self.repo = self.site.data_repository() @@ -1180,10 +1190,26 @@ for page in self.generator: if not page.exists(): pywikibot.output('%s doesn't exist.' % page) - try: - item = pywikibot.ItemPage.fromPage(page) - except pywikibot.NoPage: - item = None + # FIXME: Hack because 'is_data_repository' doesn't work if + # site is the APISite. See T85483 + data_site = page.site.data_repository() + if (data_site.family == page.site.family and + data_site.code == page.site.code): + is_item = page.namespace() == data_site.item_namespace.id + else: + is_item = False + if self.use_from_page is not True and is_item: + item = pywikibot.ItemPage(data_site, page.title()) + item.get() + elif self.use_from_page is False: + pywikibot.error('{0} is not in the item namespace but ' + 'must be an item.'.format(page)) + continue + else: + try: + item = pywikibot.ItemPage.fromPage(page) + except pywikibot.NoPage: + item = None if not item: if not treat_missing_item: pywikibot.output( diff --git a/scripts/claimit.py b/scripts/claimit.py index 96471c2..b1a968d 100755 --- a/scripts/claimit.py +++ b/scripts/claimit.py @@ -81,7 +81,7 @@ * exists_arg - String specifying how to handle duplicate claims
""" - super(ClaimRobot, self).__init__() + super(ClaimRobot, self).__init__(use_from_page=None) self.generator = generator self.claims = claims self.exists_arg = exists_arg