Skip to content

Commit

Permalink
Merge pull request #379 from Wikidata/replace-stated-in
Browse files Browse the repository at this point in the history
refactor redirect and dead items handling
  • Loading branch information
marfox committed Dec 19, 2019
2 parents 232af49 + 4c423e4 commit 33781d4
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions soweego/ingester/wikidata_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,19 +511,27 @@ def _handle_addition(
_reference(claim, person_pid, person_tid)


def _essential_checks(
subject, predicate, value, person_pid=None, person_tid=None
):
item = pywikibot.ItemPage(REPO, subject)
def _handle_redirect_and_dead(qid):
item = pywikibot.ItemPage(REPO, qid)

# Handle redirects
while item.isRedirectPage():
item = item.getRedirectTarget()

try:
data = item.get()
except NoPage:
LOGGER.warning("%s doesn't exist anymore", subject)
LOGGER.warning("%s doesn't exist anymore", qid)
return None, None

return item, data


def _essential_checks(
subject, predicate, value, person_pid=None, person_tid=None
):
item, data = _handle_redirect_and_dead(subject)

if item is None and data is None:
return None, None

# No data at all
Expand Down Expand Up @@ -650,9 +658,13 @@ def _reference(claim, person_pid, person_tid):


def _delete_or_deprecate(action, qid, tid, catalog, catalog_pid) -> None:
item = pywikibot.ItemPage(REPO, qid)
item_data = item.get()
item_claims = item_data.get('claims')
item, data = _handle_redirect_and_dead(qid)

if item is None and data is None:
LOGGER.error('Cannot %s %s identifier %s', action, catalog, tid)
return

item_claims = data.get('claims')
# This should not happen:
# the input item is supposed to have at least an identifier claim.
# We never know, Wikidata is alive.
Expand All @@ -665,6 +677,7 @@ def _delete_or_deprecate(action, qid, tid, catalog, catalog_pid) -> None:
tid,
)
return

identifier_claims = item_claims.get(catalog_pid)
# Same comment as the previous one
if not identifier_claims:
Expand All @@ -677,6 +690,7 @@ def _delete_or_deprecate(action, qid, tid, catalog, catalog_pid) -> None:
tid,
)
return

for claim in identifier_claims:
if claim.getTarget() == tid:
if action == 'delete':
Expand Down

0 comments on commit 33781d4

Please sign in to comment.