User:Data-Complete-Gadget/WikidataComplete.js
Appearance
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/********************************************************************************************************
* Google Summer of Code 2021 Project for Wikimedia Foundation *
* Project Name : WikidataComplete *
* Project Documentation : https://www.wikidata.org/wiki/Wikidata:WikidataComplete *
* Mentors: Dennis Diefenbach,Andreas Both, Aleksandr Perevalov,Kunpeng Guo *
* Participant: Dhairya Khanna *
* *
********************************************************************************************************/
importScript("User:Gabinguo/celebration.js");
(function (mw, $, wb) {
"use strict";
if (
mw.config.get("wgNamespaceNumber") !== 0 ||
!mw.config.exists("wbEntityId")
) {
return;
}
var lang = mw.config.get("wgUserLanguage");
var messages,
entityid = mw.config.get("wbEntityId"),
api = new mw.Api();
var username = mw.config.get("wgUserName");
messages = (function () {
var translations = {
de: {
title: "Anweisungen aktualisieren",
more: "mehr",
inverse: "invers",
"show-inverse": "geholte Anweisungen anzeigen",
"no-result": "kein Ergebnis",
loading: "laden...",
},
en: {
title1: "You have these number of statements to approve",
title2: "Show next entity which can be approved",
more: "more",
inverse: "inverse",
"show-inverse": "show fetched statements",
"no-result": "no result",
loading: "loading...",
},
es: {
title: "Actualizar declaraciones",
more: "más",
inverse: "inverso",
"show-inverse": "mostrar declaraciones recuperadas",
"no-result": "sin resultados",
loading: "cargando...",
},
fr: {
title: "Mettre à jour les relevés",
more: "plus",
inverse: "inverse",
"show-inverse": "afficher les déclarations récupérées",
"no-result": "pas de résultats",
loading: "chargement...",
},
sv: {
title1: "Du har så här många uttalanden att förbättra",
title2: "Visa nästa entitet som kan förbättras",
more: "mer",
inverse: "invertera",
"show-inverse": "visa hämtade uttalanden",
"no-result": "inga resultat",
loading: "laddar...",
},
"zh-hans": {
title: "更新声明",
more: "更多",
inverse: "反向",
"show-inverse": "显示获取的语句",
"no-result": "无结果",
loading: "加载中...",
},
},
chain = mw.language.getFallbackLanguageChain(),
len = chain.length,
ret = {},
i = len - 1;
while (i >= 0) {
if (translations.hasOwnProperty(chain[i])) {
$.extend(ret, translations[chain[i]]);
}
i = i - 1;
}
return ret;
})();
//The length of the facts generated from the dataset.
var facts_length = {};
var newfacts;
$.ajax({
url:
"https://datacompletewiki.toolforge.org/api/v1/facts/" + entityid + "/",
async: false,
dataType: "json",
success: function (data) {
facts_length = data.length;
newfacts = data;
},
});
var propertyList2 = [];
$(".wikibase-statementgroupview-property-label a").each(function () {
try {
propertyList2.push(
$(this).prop("href").split("/wiki/Property:")[1].toString()
);
} catch (e) {
mw.log.warn(
"Error getting property value in User:Data-Complete-Gadget/WikidataComplete.js"
);
}
});
// the list to display
var filteredFacts = newfacts.filter(
(newfact) => !propertyList2.includes(newfact.property)
);
var filteredFactslen = filteredFacts["length"];
var highlightlink = [];
var boldtext = [];
$.ajax({
url:
"https://datacompletewiki.toolforge.org/api/v1/facts/" + entityid + "/",
async: false,
dataType: "json",
success: function (data) {
for (var i = 0; i < data.length; i++) {
var url = data[i].wikipediaLink;
var ans = data[i].text;
// => For chrome highlight, make the code fit your needs.
var evidence = data[i].evidence;
var startindex = data[i].startIdx;
var endindex = data[i].endIdx;
var res = encodeURIComponent(evidence.slice(0, startindex));
highlightlink[i] = url + "#:~:text=" + res + "-," + ans;
boldtext[i] =
evidence.substring(0, startindex) +
"" +
evidence.substring(startindex, endindex) +
"" +
evidence.substring(endindex);
}
},
});
//Generating a new random item to approve
var newitem = {};
$.ajax({
url: "https://datacompletewiki.toolforge.org/api/v1/facts/random/",
async: false,
dataType: "json",
success: function (data) {
newitem = data.wikidataLink;
},
});
function start_menu_null(filteredFactslen) {
var newitemtoappend;
if (filteredFactslen == 0) newitemtoappend = "";
else newitemtoappend = "Next Entity";
return newitemtoappend;
}
var newitemtoappend = start_menu_null(filteredFactslen);
//Function for generating message for the available facts
var flag = 0;
function start_menu(filteredFactslen) {
var final_message = "";
if (filteredFactslen == 1) {
final_message =
"There is " + String(filteredFactslen) + " statement to approve";
} else if (filteredFactslen > 1) {
final_message =
"There are " + String(filteredFactslen) + " statements to approve";
} else if (filteredFactslen == 0) {
flag = 1;
final_message = "Go to entity with statements to approve".link(newitem);
}
return final_message;
}
// prettier-ignore
var html =
'\
';
/* The following function is used to create claim using Wikidata APIs.
Want know about them? Check out the documentation: https://www.wikidata.org/w/api.php */
$(document).ready(function () {
$("#inversesection:last").remove();
});
function createclaim(qid, pid, snak, sourceSnaks, snaksorder, username, acc) {
var api = new mw.Api();
api.get({ action: "query", meta: "tokens" }).then(function (aw) {
var token = aw.query.tokens.csrftoken;
return api
.post({
action: "wbcreateclaim", //Calling API to craete the claim
entity: qid,
property: pid,
snaktype: "value",
value: snak,
summary: "Edited with Wikidatacomplete",
token: token,
})
.then(function (data) {
var comment = `Statement Suggested by Wikidatacomplete and approved by the user: ${username}`;
var api = new mw.Api();
var token = mw.user.tokens.values.csrfToken;
return api.post({
action: "wbsetreference", //Calling API to craete the reference
statement: data.claim.id,
snaks: JSON.stringify(sourceSnaks),
snaksorder: JSON.stringify(snaksorder),
token: token,
summary: comment,
});
})
.then(function (data2) {
if (data2.success == 1) {
console.log("Claim Added Successfully");
var acceptance = {
url: "https://datacompletewiki.toolforge.org/api/v1/facts/accept/",
method: "POST",
data: { fact_id: acc },
timeout: 0,
};
$.ajax(acceptance).done(function (response) {
console.log(response);
});
location.reload();
}
});
});
}
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
/*
The main function for loading the data to the gadget.
*/
//https://en.wikipedia.org/wiki/Batgirl_and_the_Birds_of_Prey#:~:text=Batgirl%20and%20the%20Birds%20of%20Prey%20was%20a%20monthly%20ongoing%20American-,comic%20book
var newList = [];
function Properties() {
$(document).ready(function () {
var propertyList = [];
$(".wikibase-statementgroupview-property-label a").each(function () {
var href = $(this).prop("href");
var p = href && href.split("/wiki/Property:")[1];
if (p) {
propertyList.push(p.toString());
}
});
// this gives you the list of existing property ids
for (var i = 0; i < propertyList.length; i++) {
newList[i] = propertyList[i];
}
// check the property id proposed by Wikidatacomplte api and you can control the show-no-show case.
});
}
Properties();
function loaditems() {
var fetchurl =
"https://datacompletewiki.toolforge.org/api/v1/facts/" + entityid + "/";
$.getJSON(fetchurl, function (result1) {
for (var i = 0; i < result1.length; i++) {
var retrieved_date = new Date(result1[i].retrieved);
if (!newList.includes(String(result1[i].property))) {
var statementgroup =
'\
\