Page MenuHomePhabricator

DataSite.addClaim() throws a NoWikibaseEntity if MediaInfo is missing
Closed, ResolvedPublicBUG REPORT

Description

Similar bug than T348422. where MediaInfo.addClaim() failed if mediainfo didn't exist. However, this fails when site.addClaim() is called and mediainfo doesn't exits. In commons mediainfo is created only after the first data is added.

Steps to replicate the issue (include links if applicable):

  • add new image to commons
  • try to add new SDC data to image using site object (e.g. mime_claim = pywikibot.Claim(wikidata_site, 'P1163'))
  • pywikibot fails when there isn't yet SDC entry
page = pywikibot.FilePage(commons_site, 'File:newimage.tiff')
wditem = page.data_item()
claim = pywikibot.Claim(wikidata_site, property_id)
claim.setTarget(claim_value)
commons_site.addClaim(wditem, claim)

What happens?:
pywikibot throws exception such as: pywikibot.exceptions.NoWikibaseEntityError: Entity 'M145344608' doesn't exist on commons:commons
CRITICAL: Exiting due to uncaught exception NoWikibaseEntityError: Entity 'M145344608' doesn't exist on commons:commons

What should have happened instead?:
create SDC data record so actually useful information could be added

How to test the patch
As the writing betacluster is down, editing can be tested in real commons by using the script:

python pwb.py  wikimedia_commons_sdc_writing_test.py

Event Timeline

Ipr1 updated the task description. (Show Details)

Please add example code to ticket howto replicate the bug.

This comment was removed by Ipr1.
This comment was removed by Ipr1.
import pywikibot

wikidata_site = pywikibot.Site("wikidata", "wikidata")  
commonssite = pywikibot.Site("commons", "commons")
commonssite.login()

# note! to trigger bug, must be something that doesn't have SDC data yet
# so make fresh upload if there is already something
page = pywikibot.FilePage(commonssite, 'File:Hellaksen Suklaa - tuotekuva.tiff')

filepage =  pywikibot.FilePage(page)
file_info = filepage.latest_file_info

wditem = page.data_item()  # Get the data item associated with the page
sdcdata = wditem.get_data_for_new_entity() # get new sdc item

## add something like P1163 (mime-type) to force creation of sdc-data
print("adding mime-type: " + str(file_info.mime))

mime_claim = pywikibot.Claim(wikidata_site, 'P1163')
mime_claim.setTarget(file_info.mime)
commonssite.addClaim(wditem, mime_claim)

Confirmed that site.addClaim() doesn't work

commonssite.addClaim(wditem, mime_claim)

Confirmed that MediaInfo.addClaim() works

wditem.addClaim(mime_claim)

Basically this is because

  1. ) commonssite.addClaim() will fetch the latest_revision_id in site.addClaim() L347
  1. ) it lauches MediaInfo.get() -> WikibaseEntity.get() which will do the site.loadcontent() L184, which will get a response from server which says that entity is missing
  1. ) entity.get() will throw a NoWikibaseEntityError at WikibaseEntity.get() as it checks if the entity value is missing. L275

The reason why the MediaInfo.addClaim() works is because it already sets the revision id. With this the site.addClaim() doesn't fire the entity.get() and it will skip the missing test. L529

One way to fix this would be to check in MediaInfo.get() if the missing mediainfo is something what we can ignore. If so then generate the expected result dynamically in Pywikibot and ignore the server's result. However, currently the SDC editing tests ( T355927 ) are broken, so that will need to be fixed first before pushing patch for fixing the site.addClaim() to gerrit.

Zache renamed this task from pywikibot has - again - broken SDC-handling when SDC data does not yet exist to DataSite.addClaim() throws a NoWikibaseEntity if MediaInfo is missing.Feb 16 2024, 7:39 AM
Zache claimed this task.
Zache edited projects, added Fiwiki-Wikidata-Commons, Pywikibot; removed WMFI.
Zache updated the task description. (Show Details)

It seems that beta commons is broken T358364 so i made a separate test script which will edit to Wikimedia Commons. It edits either the file Sandbox Image.png or adds missing MIME-type SDC properties.

Running the script

git clone https://github.com/wikimedia/pywikibot.git
python3 -m venv venv
source venv/bin/activate
cd core
pip install -r requirements.txt
pip install -r dev-requirements.txt

python pwb.py  wikimedia_commons_sdc_writing_test.py

Change 1006184 had a related patch set uploaded (by Zache-tool; author: Zache-tool):

[pywikibot/core@master] Populate MediaInfo._content with expected attributes when loaded

https://gerrit.wikimedia.org/r/1006184

Change 1006184 merged by jenkins-bot:

[pywikibot/core@master] Populate MediaInfo._content with expected attributes when loaded

https://gerrit.wikimedia.org/r/1006184