User:Opencooper/wikidataDescription.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Opencooper/wikidataDescription. |
// 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("<div id='wikidataDescription' style='display: none;'></div>");
var sheet = window.document.styleSheets[0];
sheet.insertRule("#wikidataDescription { font-size: 45%; color: var(--color-base); padding-left: 1em; }");
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("<div id='wikidataLabelInfo' style='display: none;'>"
+ "<small>Wikidata Label: </small><i>"
+ text + "</i></div>");
}
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("<div id='wikidataDescriptionText' style='display: none;'><small>Wikidata Description: </small><i>"
+ displayText + "</i></div>");
getWikidataInstanceOf();
}
function displayLocalDescription() {
// If local {{short description}} exists, use it as well
if ($('.shortdescription').length) {
var shortDescriptionElement = $('.shortdescription').first().clone();
shortDescriptionElement.find("style").remove();
shortDescription = shortDescriptionElement.text().replace(/\s+/g, " ");
}
$("#wikidataDescription").append("<div id='descriptionBasic' title='{{short description}}'><i>"
+ shortDescription + "</i></div>");
$("#wikidataDescription").append("<div id='shortDescriptionInfo' style='display: none;'>"
+ "<small>Short Description: </small><i>"
+ shortDescription + "</i></div>");
}
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("<div id='wikidataInstanceOfInfo' style='display: none;'>"
+ "<small>Instance Of: </small><i>"
+ text + "</i></div>");
// 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);