Created
April 12, 2019 14:38
-
-
Save lucaswerkmeister/5056dda00c5bec336a064e920b88aadc to your computer and use it in GitHub Desktop.
Client-side (in-browser) import of Wikidata entities into another Wikibase installation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function importEntitiesFromWikidata( wikidataEntityIds ) { | |
const params = new URLSearchParams(); | |
params.set( 'action', 'wbgetentities' ); | |
params.set( 'ids', wikidataEntityIds.join( '|' ) ); | |
params.set( 'props', [ 'labels', 'descriptions', 'aliases', 'datatype' ].join( '|' ) ); | |
params.set( 'format', 'json' ); | |
params.set( 'origin', '*' ); | |
const response = await fetch( `https://www.wikidata.org/w/api.php?${ params.toString() }` ), | |
json = await response.json(); | |
for ( const wikidataEntityData of Object.values( json.entities ) ) { | |
await importEntityFromJson( wikidataEntityData ); | |
} | |
} | |
async function importEntityFromJson( wikidataEntityData ) { | |
const wikidataEntityId = wikidataEntityData.id, | |
api = new mw.Api(); | |
delete wikidataEntityData.id; | |
for ( const key of [ 'labels', 'descriptions', 'aliases' ] ) { | |
if ( key in wikidataEntityData ) { | |
filterUnknownLanguages( wikidataEntityData[ key ] ); | |
} | |
} | |
try { | |
await api.postWithEditToken( { | |
action: 'wbeditentity', | |
new: wikidataEntityData.type, | |
data: JSON.stringify( wikidataEntityData ), | |
summary: `Imported from ${ wikidataEntityId } on Wikidata`, | |
bot: true, | |
} ); | |
} catch ( e ) { | |
if ( e === 'modification-failed' ) { | |
return; // ignore; entity exists already | |
} else { | |
throw e; | |
} | |
} | |
} | |
function filterUnknownLanguages( terms ) { | |
// InitialiseSettings.php, wmgExtraLanguageNames | |
delete terms.kea; | |
delete terms.nod; | |
delete terms.nys; | |
delete terms.ota; | |
delete terms.rwr; | |
delete terms.sje; | |
delete terms.smj; | |
delete terms.smn; | |
delete terms.sms; | |
delete terms.srq; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example, to import all the allowed “sex or gender” values, I used:
The list of IDs was generated by the snippet
on https://www.wikidata.org/wiki/Property:P21, where
temp0
was thewikibase-snaklistview-listview
element selected in the Inspector.