Page MenuHomePhabricator

MediaInfo.EditEntity() doesn't work for claims/statements
Closed, ResolvedPublicBUG REPORT

Description

Hitting T149410 again. I was trying to switch my code to use mediainfo.editEntity() instead of using site, but nothing happened.

Some example code:

import pywikibot
site = pywikibot.Site('commons', 'commons')
repo = site.data_repository()

filepage = pywikibot.FilePage(site, 'File:-2011-03-05 Southern Hill Hospital, Mundesley, Norfolk (2).jpg')
mediainfo = filepage.data_item()

new_claim = pywikibot.Claim(repo, 'P7384')
new_claim.setTarget('2295284')

data = {'claims': [new_claim.toJSON(), ]}

response = mediainfo.editEntity(data=data)
print(response)

(nothing happens)

The problem is that the get API is returning 'statements' and the edit API expects 'claims'. In my dict I use claims.

This hits WikibaseEntity._normalizeData which uses these fields:

DATA_ATTRIBUTES = {
    'labels': LanguageDict,
    'statements': ClaimCollection,
}

Claims is not in there so the data dict is empty after normalization.

I can't really think of a clean way to solve this at the moment. Maybe add ImageInfo._normalizeData() that doesn't mess it up?

Details

Related Changes in Gerrit:

Event Timeline

Workaround:
Instead of this code (working for Wikidata):

item.editEntity(summary='my edit')

Use this (works for Commons):

data = item.toJSON(diffto=getattr(item, '_content', None))
data['claims'] = data['statements']
del data['statements']
commons.editEntity(item, data, summary='my edit')

Change #1350193 had a related patch set uploaded (by Zaidusyy; author: Zaidusyy):

[pywikibot/core@master] MediaInfo: map claims for wbeditentity

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

Change #1350193 merged by jenkins-bot:

[pywikibot/core@master] MediaInfo: map claims for wbeditentity

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