jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/348479 )
Change subject: [IMPR] Introduce user_add_claim and allow asynchronous ItemPage.addClaim ......................................................................
[IMPR] Introduce user_add_claim and allow asynchronous ItemPage.addClaim
Bug: T87493 Change-Id: I05f02939f8797dff1914fc141bd2b4ca289a60c1 --- M pywikibot/bot.py M pywikibot/page.py M scripts/claimit.py M scripts/harvest_template.py M scripts/illustrate_wikidata.py 5 files changed, 63 insertions(+), 22 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 4cef28a..eea0052 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -1916,6 +1916,54 @@ # (bug T86083) return self._save_page(item, item.editEntity, data, **kwargs)
+ def _add_source_callback(self, claim, source, **kwargs): + """ + Make a callback for user_add_claim. + + @return: callback to be executed after saving the claim + @rtype: callable or None + """ + callback = None + sourceclaim = self.getSource(source) + if sourceclaim: + def callback(item, err): + if err is None and claim.on_item is not None: + claim.addSource(sourceclaim, **kwargs) + + return callback + + def user_add_claim(self, item, claim, source=None, bot=True, **kwargs): + """ + Add a claim to an item, with user confirmation as required. + + @param item: page to be edited + @type item: pywikibot.ItemPage + @param claim: claim to be saved + @type claim: pywikibot.Claim + @param source: site where the claim comes from + @type source: pywikibot.site.APISite + @param bot: whether to flag as bot (if possible) + @type bot: bool + @kwarg ignore_server_errors: if True, server errors will be reported + and ignored (default: False) + @type ignore_server_errors: bool + @kwarg ignore_save_related_errors: if True, errors related to + page save will be reported and ignored (default: False) + @type ignore_save_related_errors: bool + @return: whether the item was saved successfully + @rtype: bool + """ + self.current_page = item + + callback = None + if source: + callback = self._add_source_callback(claim, source, bot=bot) + + pywikibot.output('Adding %s --> %s' % (claim.getID(), + claim.getTarget())) + return self._save_page(item, item.addClaim, claim, bot=bot, + callback=callback, **kwargs) + def getSource(self, site): """ Create a Claim usable as a source for Wikibase statements. diff --git a/pywikibot/page.py b/pywikibot/page.py index b57317e..0a5ecd3 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -4263,6 +4263,7 @@ data = {'sitelinks': data} self.editEntity(data, **kwargs)
+ @allow_asynchronous def addClaim(self, claim, bot=True, **kwargs): """ Add a claim to the item. @@ -4271,6 +4272,15 @@ @type claim: Claim @param bot: Whether to flag as bot (if possible) @type bot: bool + @keyword asynchronous: if True, launch a separate thread to add claim + asynchronously + @type asynchronous: bool + @keyword callback: a callable object that will be called after the + claim has been added. It must take two arguments: (1) an ItemPage + object, and (2) an exception instance, which will be None if the + item was saved successfully. This is intended for use by bots that + need to keep track of which saves were successful. + @type callback: callable """ self.repo.addClaim(self, claim, bot=bot, **kwargs) claim.on_item = self diff --git a/scripts/claimit.py b/scripts/claimit.py index cd2993c..19142df 100755 --- a/scripts/claimit.py +++ b/scripts/claimit.py @@ -83,6 +83,7 @@ * exists_arg - String specifying how to handle duplicate claims
""" + self.availableOptions['always'] = True super(ClaimRobot, self).__init__(use_from_page=None) self.generator = generator self.claims = claims @@ -142,15 +143,8 @@ break skip = False if not skip: - pywikibot.output('Adding %s --> %s' - % (claim.getID(), claim.getTarget())) - item.addClaim(claim) # A generator might yield pages from multiple languages - source = self.getSource(page.site) - if source: - claim.addSource(source, bot=True) - # TODO FIXME: We need to check that we aren't adding a - # duplicate + self.user_add_claim(item, claim, page.site)
def listsEqual(list1, list2): diff --git a/scripts/harvest_template.py b/scripts/harvest_template.py index 62d24c6..309705e 100755 --- a/scripts/harvest_template.py +++ b/scripts/harvest_template.py @@ -71,6 +71,7 @@ * fields - A dictionary of fields that are of use to us
""" + self.availableOptions['always'] = True super(HarvestRobot, self).__init__() self.generator = pg.PreloadingGenerator(generator) self.templateTitle = templateTitle.replace(u'_', u' ') @@ -211,14 +212,8 @@ % claim.type) continue
- pywikibot.output( - 'Adding %s --> %s' - % (claim.getID(), claim.getTarget())) - item.addClaim(claim) # A generator might yield pages from multiple sites - source = self.getSource(page.site) - if source: - claim.addSource(source, bot=True) + self.user_add_claim(item, claim, page.site)
def main(*args): diff --git a/scripts/illustrate_wikidata.py b/scripts/illustrate_wikidata.py index 6e0f91a..243be1c 100755 --- a/scripts/illustrate_wikidata.py +++ b/scripts/illustrate_wikidata.py @@ -84,14 +84,8 @@ return
newclaim.setTarget(image) - pywikibot.output('Adding %s --> %s' - % (newclaim.getID(), newclaim.getTarget())) - item.addClaim(newclaim) - # A generator might yield pages from multiple sites - source = self.getSource(page.site) - if source: - newclaim.addSource(source, bot=True) + self.user_add_claim(item, newclaim, page.site)
def main(*args):