jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/815234 )
Change subject: [bugfix] Do not rely on self.current_page.site
......................................................................
[bugfix] Do not rely on self.current_page.site
It is set to the item whenever a suitable value
to import is found. This causes page existence
checks againts Wikidata, not the original site,
and imports of wrong references.
Change-Id: I99e6914d996d9a7ae540d01cde073e52a4afd286
---
M pywikibot/bot.py
M scripts/harvest_template.py
M tests/harvest_templates_tests.py
3 files changed, 17 insertions(+), 10 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 6ca5c61..3fd0700 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -2218,6 +2218,9 @@
page save will be reported and ignored (default: False)
:return: whether the item was saved successfully
+ .. note:: calling this method sets the current_page property
+ to the item which changes the site property
+
.. note:: calling this method with the 'source' argument modifies
the provided claim object in place
"""
@@ -2266,6 +2269,9 @@
:param logger_callback: function logging the output of the method
:return: whether the claim could be added
+ .. note:: calling this method may change the current_page property
+ to the item which will also change the site property
+
.. note:: calling this method with the 'source' argument modifies
the provided claim object in place
"""
diff --git a/scripts/harvest_template.py b/scripts/harvest_template.py
index 2f28dd5..6dd4677 100755
--- a/scripts/harvest_template.py
+++ b/scripts/harvest_template.py
@@ -218,6 +218,7 @@
def template_link_target(self,
item: pywikibot.ItemPage,
+ site: pywikibot.site.BaseSite,
link_text: str) -> Optional[pywikibot.ItemPage]:
"""Find the ItemPage target for a given link text.
@@ -225,7 +226,7 @@
Only follow the redirect target if redirect page has no
wikibase item.
"""
- linked_page = pywikibot.Page(self.current_page.site, link_text)
+ linked_page = pywikibot.Page(site, link_text)
try:
exists = linked_page.exists()
except (InvalidTitleError, InvalidPageError):
@@ -296,10 +297,11 @@
# We found the template we were looking for
for field_item in fielddict.items():
- self.treat_field(item, field_item)
+ self.treat_field(item, page.site, field_item)
def treat_field(self,
item: pywikibot.page.ItemPage,
+ site: pywikibot.site.BaseSite,
field_item: Tuple[str, str]) -> None:
"""Process a single field of template fielddict.
@@ -310,8 +312,6 @@
if not field or field not in self.fields:
return
- site = self.current_page.site
-
# todo: extend the list of tags to ignore
value = textlib.removeDisabledParts(
# todo: eventually we may want to import the references
@@ -334,7 +334,7 @@
do_multi = self._get_option_with_fallback(options, 'multi')
inverse_prop = self._get_option_with_fallback(options, 'inverse')
- for target in handler(value, item, field):
+ for target in handler(value, site, item, field):
claim = ppage.newClaim()
claim.setTarget(target)
# A generator might yield pages from multiple sites
@@ -362,6 +362,7 @@
exists_arg.add('p')
def handle_wikibase_item(self, value: str,
+ site: pywikibot.site.BaseSite,
item: pywikibot.page.ItemPage,
field: str) -> Iterator[pywikibot.ItemPage]:
"""Handle 'wikibase-item' claim type.
@@ -375,7 +376,7 @@
for match in pywikibot.link_regex.finditer(value):
matched = True
link_text = match.group(1)
- linked_item = self.template_link_target(item, link_text)
+ linked_item = self.template_link_target(item, site, link_text)
if linked_item:
yield linked_item
@@ -388,7 +389,7 @@
.format(prop, field, value))
return
- linked_item = self.template_link_target(item, value)
+ linked_item = self.template_link_target(item, site, value)
if linked_item:
yield linked_item
@@ -410,13 +411,13 @@
for match in self.linkR.finditer(value):
yield match.group('url')
- def handle_commonsmedia(self, value, *args
+ def handle_commonsmedia(self, value, site, *args
) -> Iterator[pywikibot.FilePage]:
"""Handle 'commonsMedia' claim type.
.. versionadded:: 7.5
"""
- repo = self.current_page.site.image_repository()
+ repo = site.image_repository()
image = pywikibot.FilePage(repo, value)
if image.isRedirectPage():
image = pywikibot.FilePage(image.getRedirectTarget())
diff --git a/tests/harvest_templates_tests.py b/tests/harvest_templates_tests.py
index b0804ee..3909727 100644
--- a/tests/harvest_templates_tests.py
+++ b/tests/harvest_templates_tests.py
@@ -44,7 +44,7 @@
with self.subTest(link=link, item=item):
dummy_item = ItemPage(self.site.data_repository(), 'Q1')
target = HarvestRobot.template_link_target(
- DummyBot(self.site), dummy_item, link)
+ DummyBot(self.site), dummy_item, self.site, link)
self.assertIsInstance(target, ItemPage)
self.assertEqual(target.title(), item)
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/815234
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I99e6914d996d9a7ae540d01cde073e52a4afd286
Gerrit-Change-Number: 815234
Gerrit-PatchSet: 3
Gerrit-Owner: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/814797 )
Change subject: [bugfix] Fix duplicate source detection in user_add_claim_unless_exists
......................................................................
[bugfix] Fix duplicate source detection in user_add_claim_unless_exists
The source argument is a site, but it was mistakenly treated
as a claim object.
Change-Id: I143dba7f5fe21454dc6722281b0efe8f70d5355a
---
M pywikibot/bot.py
1 file changed, 23 insertions(+), 6 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index f42f5b7..6ca5c61 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -2217,6 +2217,9 @@
:keyword ignore_save_related_errors: if True, errors related to
page save will be reported and ignored (default: False)
:return: whether the item was saved successfully
+
+ .. note:: calling this method with the 'source' argument modifies
+ the provided claim object in place
"""
self.current_page = item
@@ -2262,10 +2265,21 @@
:param exists_arg: pattern for merging existing claims with new ones
:param logger_callback: function logging the output of the method
:return: whether the claim could be added
+
+ .. note:: calling this method with the 'source' argument modifies
+ the provided claim object in place
"""
+ # This code is somewhat duplicate to user_add_claim but
+ # unfortunately we need the source claim here, too.
+ if source:
+ sourceclaim = self.getSource(source)
+ else:
+ sourceclaim = None
+
# Existing claims on page of same property
claims = item.get().get('claims')
assert claims is not None
+
for existing in claims.get(claim.getID(), []):
# If claim with same property already exists...
if 'p' not in exists_arg:
@@ -2296,17 +2310,20 @@
log("Append 'q' to -exists argument to override this behavior")
break
- if ('s' not in exists_arg or not source) and not existing.sources:
+ if ('s' not in exists_arg or not sourceclaim) \
+ and not existing.sources:
logger_callback(
'Skipping {} because claim without source already exists'
- .format(claim.getID(),))
+ .format(claim.getID()))
log("Append 's' to -exists argument to override this behavior")
break
- if ('s' not in exists_arg and source
- and any(source.getID() in ref
- and all(snak.target_equals(source.getTarget())
- for snak in ref[source.getID()])
+ # FIXME: the user may provide a better source, but we only
+ # assume it's the default one
+ if ('s' not in exists_arg and sourceclaim
+ and any(sourceclaim.getID() in ref
+ and all(snak.target_equals(sourceclaim.getTarget())
+ for snak in ref[sourceclaim.getID()])
for ref in existing.sources)):
logger_callback(
'Skipping {} because claim with the same source already '
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/814797
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I143dba7f5fe21454dc6722281b0efe8f70d5355a
Gerrit-Change-Number: 814797
Gerrit-PatchSet: 2
Gerrit-Owner: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: JAn Dudík <jan.dudik(a)gmail.com>
Gerrit-MessageType: merged