ProfessionalWiki / ProfessionalWiki/WikibaseLocalMedia

Creating claims of local media to items through api interface

Open
#27 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
PHP
Stars
13
Forks
11
PR merge metrics
No merged PRs in 30d

Description

Is there an appropriate syntax to claim an image programmatically to an item ?

When i try doing the following,

site = pwb.Site('localwiki:en')
#site.login()

token = pwb.data.api.Request(site=site, parameters={'action': 'query', 'meta': 'tokens'}).submit()
csrf_token = token['query']['tokens']['csrftoken']


result = pwb.data.api.Request(site=site, parameters={'action': 'wbcreateclaim', 'token': csrf_token,
                              'entity': 'P1','property': 'P50', 'snaktype': 'value', 
                              'datatype': 'localMedia', 'type': 'statement',
                             'value': {'value': 'Neo.jpg', 'type': 'localMedia'}}).submit()

I get the following error

APIError: invalid-snak: Invalid snak data.
[param: action=wbcreateclaim&entity=P1&property=P50&snaktype=value&datatype=localMedia&type=statement&value=value%7Ctype&assert=user&maxlag=1&format=json&token=6caed2ab16ab837896c5a17b01d6fee7652fb29e%2B%5C;
 messages: [{'name': 'wikibase-api-invalid-snak', 'parameters': [], 'html': {'*': 'Invalid snak data.'}}];

When i create the claim over the web interface, the json data of the item contains the claim as the following

'claims': <class 'pywikibot.page._collections.ClaimCollection'>(
    {
        'P50': [Claim.fromJSON(DataSite("en", "localwiki"), 
                               {'mainsnak': {'snaktype': 'value', 
                                             'property': 'P50', 
                                             'datatype': 'localMedia', 
                                             'datavalue': {'value': 'Jpg3.jpg', 'type': 'localMedia'}
                                            }, 
                                'type': 'statement', 
                                'id': 'P1$ceabb6c6-4ba1-f31b-d2e1-b18cdb78050e', 
                                'rank': 'normal'}), 
                Claim(DataSite("en", "localwiki"), 'P50'), 
                Claim(DataSite("en", "localwiki"), 'P50')]    
    }
)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the wbcreateclaim API request using the localMedia datatype and compare its value format with the claim JSON produced by the web interface. Done means a programmatic request creates the P50 claim without the invalid-snak error and produces the same local-media value structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.