Skip to content

Instantly share code, notes, and snippets.

@lucaswerkmeister
Created April 12, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucaswerkmeister/5056dda00c5bec336a064e920b88aadc to your computer and use it in GitHub Desktop.
Save lucaswerkmeister/5056dda00c5bec336a064e920b88aadc to your computer and use it in GitHub Desktop.
Client-side (in-browser) import of Wikidata entities into another Wikibase installation
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;
}
@lucaswerkmeister
Copy link
Author

For example, to import all the allowed “sex or gender” values, I used:

importEntitiesFromWikidata( [
  "Q6581097",
  "Q6581072",
  "Q1097630",
  "Q303479",
  "Q189125",
  "Q1052281",
  "Q2449503",
  "Q48270",
  "Q1399232",
  "Q3277905",
  "Q746411",
  "Q350374",
  "Q660882",
  "Q44148",
  "Q43445",
  "Q207959",
  "Q301702",
  "Q27679766",
  "Q27679684",
  "Q3177577",
  "Q28873047",
  "Q505371",
  "Q52261234",
  "Q18116794",
  "Q1289754",
  "Q179294"
] ).then( console.log, console.error )

The list of IDs was generated by the snippet

ids = []; for ( const node of temp0.querySelectorAll('a[title^=Q]') ) { ids.push( node.title ); }; console.log( ids )

on https://www.wikidata.org/wiki/Property:P21, where temp0 was the wikibase-snaklistview-listview element selected in the Inspector.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment