User:Opencooper/wikidataDescription.js

// This script shows some basic wikidata information for a page // Clicking toggles more advanced information

// Inspired by mobile: https://www.mediawiki.org/wiki/Reading/web/Projects/Wikidata_Descriptions // See also: https://www.wikidata.org/wiki/Help:Description

// Alternate endpoint?: https://www.mediawiki.org/w/api.php?action=help&modules=query%2Bpageterms // Example pages: https://en.wikipedia.org/wiki/Namaiki_Zakari //               https://en.wikipedia.org/wiki/Eastern_Standard_Time_(album)

// License: CC0

function setup { // If we're not reading an article, do nothing if (!(mw.config.get( 'wgAction' ) === 'view' && mw.config.get( 'wgIsArticle' ) && !mw.config.get("wgIsMainPage") && window.location.hostname != "www.wikidata.org")) { return; }

var header; if ($("#firstHeading").length) { // Vector header = $("#firstHeading"); } else if ($('.page-heading').length) { // Minerva header = $('.page-heading'); } else { console.error("wikidataDescription.js: Couldn't find a page heading. This skin (" + mw.config.get( 'skin' ) + ") might not be supported."); return; }

header.append(""                  + " ");

displayLocalDescription;

$("#wikidataDescription").click(function{   	$(this).children.toggle;    });

if (wikidataId === null) { displayWikidataLabel("[No Wikidata]"); displayWikidataDescription("[No Wikidata]"); return; }

getWikidataDescription; }

function getWikidataDescription { // API docs: https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities // Tutorial: https://www.mediawiki.org/wiki/API:Presenting_Wikidata_knowledge $.ajax({       url: "https://www.wikidata.org/w/api.php",        data: {            action: "wbgetentities",            ids: wikidataId,            props: "labels|descriptions",            languages: "en",            format: "json",            origin: "*"        },        success: parseWikidataDescription    }); }

function parseWikidataDescription(response) { var wikidataInfo = response.entities[wikidataId];

var wikidataLabel = "[None]"; if (!jQuery.isEmptyObject(wikidataInfo.labels)) { wikidataLabel = wikidataInfo.labels.en.value; }

if (wikidataLabel != "[None]" && wikidataLabel == mw.config.get("wgTitle")) { wikidataLabel = "[Same]"; }

displayWikidataLabel(wikidataLabel);

var wikidataDescription = "[None]"; if (!jQuery.isEmptyObject(wikidataInfo.descriptions)) { wikidataDescription = wikidataInfo.descriptions.en.value; }

displayWikidataDescription(wikidataDescription); }

function displayWikidataLabel(text) { $("#wikidataDescription").append(" "                                    + " Wikidata Label: "                                     + text + " "); }

function displayWikidataDescription(text) { var displayText = text; if (displayText == "[None]") { displayText = "[No Wikidata desc.]"; }	if (text != "[None]"       && shortDescription.toLowerCase == text.toLowerCase) { displayText = "[Same]"; }   if (shortDescription == "[None]" && text != "[None]") { $("#descriptionBasic").text(text); $("#descriptionBasic").attr("title", "Wikidata"); }

$("#wikidataDescription").append(" Wikidata Description: "                                    + displayText + " "); getWikidataInstanceOf; }

function displayLocalDescription { // If local exists, use it as well if ($('.shortdescription').length) { shortDescription = $('.shortdescription').first.text; }   $("#wikidataDescription").append(""                                     + shortDescription + " "); $("#wikidataDescription").append(" "                                    + " Short Description: "                                     + shortDescription + " "); }

function getWikidataInstanceOf { if (wikidataId === null) { displayWikidataInstance("[No Wikidata]"); return; }

// API docs: https://www.wikidata.org/w/api.php?action=help&modules=wbgetclaims $.ajax({       url: "https://www.wikidata.org/w/api.php",        data: {            action: "wbgetclaims",            entity: wikidataId,            format: "json",            origin: "*"        },        success: parseWikidataInstance    }); }

function parseWikidataInstance(response) { if (response.claims.P31) { var id = response.claims.P31[0].mainsnak.datavalue.value.id;

if (id == "Q5" && response.claims.P106) { // Human id = response.claims.P106[0].mainsnak.datavalue.value.id; // Occupation }

getInstanceOfLabel(id); } else { displayWikidataInstance("[None]"); } }

function getInstanceOfLabel(id) { // API docs: https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities $.ajax({       url: "https://www.wikidata.org/w/api.php",        data: {            action: "wbgetentities",            ids: id,            props: "labels",            languages: "en",            format: "json",            origin: "*"        },        success: function(response) {parseInstanceLabel(response, id);}    }); }

function parseInstanceLabel(response, id) { var wikidataInfo = response.entities[id];

var wikidataDescription = ""; if (!jQuery.isEmptyObject(wikidataInfo.labels)) { wikidataDescription = wikidataInfo.labels.en.value; }

displayWikidataInstance(wikidataDescription); }

function displayWikidataInstance(text) { $("#wikidataDescription").append(" "	                                + " Instance Of: "                                     + text + " ");

// Show advanced if we're not on enwiki if (mw.config.get( 'wgPageContentLanguage' ) != "en") { $("#wikidataDescription").children.toggle; }   $("#wikidataDescription").show; }

var wikidataId = mw.config.get( 'wgWikibaseItemId' ); var shortDescription = "[None]"; $(setup);