petermr / openVirus

aggregation of scholarly publications and extracted knowledge on viruses and epidemics.
The Unlicense
67 stars 17 forks source link

Need to write from [R] to wikidata via API #15

Open TS404 opened 4 years ago

TS404 commented 4 years ago

There s a current capability gap in the WikidataR package in that it can only read from, not write to wikidata. This will be a key and valuable feature. I've also added a note at that repo.

serenasignorelli commented 4 years ago

I'm checking how this can be done. The COVID-19 Wikidata item already exists (https://www.wikidata.org/wiki/Q84263196) and inside it you have different things.

First, you have a series of 'statements' identified by a specific property numner (i.e. 'instance of' P31) and each of them lists a serires of other Wikidata items (i.e. 'disease' Q12136).

Then we have 'identifiers', where we have again properties but that only list one ID (not a Wikidata item).

And at the end we have all the pages on that item in all Wikimedia foundation projects.

My question is: what do you want to be able to edit? Everything? Or just statements and identifiers? This is crucial as we need to think about a way to possibly structure the queries.

TS404 commented 4 years ago

Hmm, ideally everything, since being able to edit aliases and descriptions would be useful. However the two highest priority capabilities would be:

Backup option: outputting Quickstatments tables. Relevant issue link.

magnusmanske commented 4 years ago

The QS API and its token system are detailed here (if you are logged into QS)

TS404 commented 4 years ago

&data=COMMANDS [commands in the above format]

@magnusmanske, could you confirm what the relevant quickstatements API format is?

TS404 commented 4 years ago

I think I've found it:

The URL cannot have any spaces. Replace TAB with "%09", double quote with "%22", space with "%20", end-of-line with "%0A", and slash "/" with "%2F".

Will do some tests and then try to implement in [R]

TS404 commented 4 years ago

Successful first test (using token code as per my QS userpage)!

https://tools.wmflabs.org/quickstatements/api.php
    ?action=import
    &submit=1
    &username=Evolution_and_evolvability
    &token=[the token code as per my QS userpage]
    &format=v1
    &data=Q89093122%09P7347%09%22https:%2F%2Fen.wikiversity.org%2Fwiki%2FTalk:WikiJournal_of_Medicine%2FEpidemiology_of_the_Hepatitis_D_virus%22
    &batchname=ts_API_test1

Resulting in this batch being correctly implemented.

TS404 commented 4 years ago

The new API almost works (now committed to wikiPackageTesting.R). There seem to currently be some problems with qualifiers submitted via the API. When the exact same submission is made via the API it returns errors, whereas submitted as a table via the website it runs fine:

I've also added a query about this at Wikidata Help_talk:QuickStatements. Next requirement will be proper handling of CREATE and LAST capabilities.