User:Sebotic/wef-history.js
Appearance
//
/**
* wef-history
*
* Ce script permet d'afficher dans les pages d'historique de Wikipédia les modifications de l'élément Wikidata correspondant.
*
* Auteurs : H4stings.
*/
// Documentation :
// 1. Ajouter dans votre fichier commons.js la ligne suivante : importScript('Utilisateur:H4stings/wef-history.js');
// 2. Accédez à une page d'historique : c'est bon !
//
// Pour plus d'infos, lisez [[Discussion utilisateur:H4stings/wef-history.js]].
//
//////////////////////ZONE PERSONNALISABLE//////////////////////
if (typeof cacherModifLabel == 'undefined') { //cacher les wbsetlabel
cacherModifLabel = true;
}
if (typeof cacherModifAlias == 'undefined') { //cacher les wbsetaliases
cacherModifAlias = true;
}
if (typeof cacherModifLiens == 'undefined') { //cacher les wbsetsitelink & clientsitelink
cacherModifLiens = true;
}
if (typeof afficherTout == 'undefined') {
afficherTout = false;
}
if (afficherTout === true) { //afficher tout
cacherModifLabel = false;
cacherModifAlias = false;
cacherModifLiens = false;
}
/////////////////FIN DE LA ZONE PERSONNALISABLE/////////////////
/** @const liée au script initial */
var version = 1455143959;
try {
mw.loader.addSource( "ruwiki", "//ru.wikipedia.org/w/load.php" );
mw.loader.register( 'ext.gadget.wefcore', version, [ 'jquery.ui', //
'jquery.ui', //
'jquery.ui', //
'jquery.uls.data', //
'mediawiki.ForeignApi', //
], undefined, 'ruwiki' );
mw.loader.register( 'ext.gadget.isbnjs', version, undefined, undefined, 'ruwiki' );
mw.loader.register( 'ext.gadget.wefflags', version, undefined, undefined, 'ruwiki' );
mw.loader.register( 'ext.gadget.wefcore', version, [ 'mediawiki.api' ], undefined, 'ruwiki' );
} catch ( error ) {
// already registered
}
if ( mw.config.get( 'wgAction' ) === 'history' ) {
mw.loader.using( [ 'mediawiki.api', 'mediawiki.ForeignApi', 'ext.gadget.wefcore' ], function() {
var notifyOptions = {
autoHide: true,
tag: 'WEF-history',
};
var i18n = {
actionObtain: 'Récupération des modifications de Wikidata',
actionIntegrate: 'Affichage des modifications de Wikidata',
linePartLetter: 'd',
linePartTooltip: 'Wikidata',
linePartChange: 'diff.',
linePartHistory: 'historique',
// the same as in watchlist
monthes: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ],
revMonthes: { "January" : 1, "February" : 2, "March": 3, "April" : 4, "May" : 5, "June": 6, "July" : 7, "August" : 8, "September": 9, "October" : 10, "November" : 11, "December": 12 }, // works only for french wikipedia. has to be translated.
dico : { "wbsetsitelink-add" : "Sitelink added", "wbsetsitelink-set" : "Sitelink modified", "wbsetsitelink-remove" : "Sitelink removed", "wbsetlabel-add" : "Label added", "wbsetlabel-set" : "changed label", "wbsetlabel-remove" : "removed label", "wbsetdescription-add" : "Description added", "wbsetdescription-set" : "Description modified", "wbsetdescription-remove" : "Description removed", "wbeditentity-update" : "Item updated", "wbeditentity-override" : "Overrode item'", "wbeditentity-create" : "Item created", "wbsetaliases-set" : "Changed aliase(s)", "wbsetaliases-remove" : "Removed aliase(s)", "wbsetaliases-add" : "Added aliase(s)", "wblinktitles-create" : "Entité créée pour connecter les pages", "wblinktitles-connect" : "Lien de langue ajouté pour connecter les pages", "wbsetclaimvalue" : "Created claim", "wbremoveclaims" : "Deleted claim", "wbremoveclaims-remove" : "Deleted claim", "wbcreateclaim-value" : "Created Claim", "wbcreateclaim-novalue" : "Created novalue claim", "wbcreateclaim-somevalue" : "Created somevalue claim", "wbsetclaim-update" : "Updated claim", "wbsetclaim-create" : "Created claim", "wbsetclaim-update-qualifiers" : "Updated qualifers", "wbsetclaim-update-references" : "Updated references", "wbsetclaim-update-rank" : "Updated claim rank", "wbsetreference" : "Modified reference", "wbsetreference-add" : "Added reference", "wbcreateclaim-create" : "Created claim", "wbmergeitems-from" : "Merged item from", "wbsetqualifier-add" : "Added qualifier", "wbremovereferences-remove" : "Removed reference" },
reP : /\[\[Property\:(P[0-9]*)\]\]/,
reQ : /\[\[(Q[0-9]*)\]\]/,
reD : /\/\* ([^: ]*)/,
reH : /([0-9][0-9]):([0-9][0-9]), ([0-9]+) (\w+) ([0-9][0-9][0-9][0-9])/, // /!\ works only for french wikipedia. has to be rewritted for each wikipedia
};
var entityId = null;
// [[Property:P910]]: [[Q7804038]]"
var revs = null;
function affichage() {
//"use strict";
mw.notify( i18n.actionIntegrate, notifyOptions );
//affichage des révisions
$.each(revs, function(keyr, valr){
//console.log("diff : " + valr.comment + " | " + valr.timestamp);
try {
generateWatchlistLine( valr, i18n.dico );
} catch ( error ) {
mw.log.warn( "Can't add change line", error );
}
});
}
function showChanges() {
//"use strict";
mw.notify( i18n.actionObtain, notifyOptions );
if ( mw.config.get( 'wgArticleId' ) ) {
WEF_Utils.getEntityIdDeferred().done( function( entityId ) {
if ( !WEF_Utils.isEmpty( entityId ) ) {
//api des modifs à afficher
console.log( 'https://www.wikidata.org/w/api.php?action=query&prop=revisions&rvlimit=50&titles=' + entityId );
WEF_Utils.getWikidataApi().get( {
rvlimit: 50,
titles: entityId,
prop: 'revisions',
action: 'query',
} ).done( function( result ) {
//en principe, y'a qu'une page...
var page=result.query.pages;
var listePQ = "";
$.each(page, function(key, val){
//console.log ("id page " + key);
revs=val.revisions;
//1ere boucle pour lister les P & Q qu'on va devoir traduire
$.each(revs, function(keyr, valr){
//console.log ("valr.comment " + valr.comment);
if (valr.comment){
var res1 = i18n.reP.exec(valr.comment);
if (res1 !== null){
if (listePQ.indexOf(res1[1])==-1) {
listePQ = listePQ + "|" + res1[1];
}
}
var res2 = i18n.reQ.exec(valr.comment);
if (res2 !== null){
if (listePQ.indexOf(res2[1])==-1) {
listePQ = listePQ + "|" + res2[1];
}
}
//console.log (res1 + " " + res2);
}
});
});
listePQ = listePQ.substring(1);
completedico(listePQ);
} );
}
});
}
}
function completedico(liste) {
//"use strict";
//2e api de récup pour construire le i18n.dico des labels
console.log("https://www.wikidata.org/w/api.php?action=wbgetentities&ids=" + liste + "&props=labels&languages=en");
WEF_Utils.getWikidataApi().get( {
action: 'wbgetentities',
props: 'labels',
languages: 'en',
ids: liste,
format: 'json',
} ).done( function( result ) {
//faire un i18n.dico avec P/Q => label
var entities=result.entities;
$.each(entities, function(keyr, valr){
//i18n.dico[keyr] = valr.labels.fr.value;
if(valr.labels && valr.labels.en && valr.labels.en.value){
//console.log(keyr + " " + valr + " " + valr.labels.fr.value);
i18n.dico[keyr] = valr.labels.en.value;
}else{
i18n.dico[keyr] = keyr;
}
});
//console.log (i18n.dico);
affichage();
});
}
function pad( src ) {
"use strict";
if ( src < 10 ) {
return "0" + src;
} else {
return "" + src;
}
}
function generateWatchlistLine( jEntry, dico ) {
//"use strict";
var id = jEntry.revid;
var testSummary = jEntry.comment;
//console.log( "id "+ id + " summary "+ testSummary );
//if (false) {
if (testSummary.indexOf("|fr") == -1 && ((testSummary.indexOf("wbsetlabel") > -1 && cacherModifLabel)
|| (testSummary.indexOf("wbsetaliases") > -1 && cacherModifAlias) || (testSummary.indexOf("wbsetsitelink") > -1 && cacherModifLiens)
|| (testSummary.indexOf("clientsitelink") > -1 && cacherModifLiens) || (testSummary.indexOf("wbsetdescription") > -1) ) ) {
console.log( "diff " + id + " masqué car libellé/alias/liens hors fr");
}else{
var changeUrl = "https://www.wikidata.org/w/index.php?diff=" + id;
var author = jEntry.user;
var updated = new Date(jEntry.timestamp);
var dateAff = updated.getDate() + ' ' + i18n.monthes[updated.getMonth()] + ' ' + updated.getFullYear();
var heureAff = pad( updated.getHours() ) + ':' + pad( updated.getMinutes() );
//traduction commentaire sur la base du i18n.dico
var resSum1 = i18n.reP.exec(testSummary);
if (resSum1 !== null && i18n.dico[resSum1[1]] !== null){
testSummary = testSummary.replace("[[Property:"+resSum1[1]+"]]", i18n.dico[resSum1[1]] + " <small>(<a href=\"https://www.wikidata.org/wiki/Property:"+resSum1[1]+"\">Property:"+resSum1[1]+"</a>)</small>");
}
var resSum2 = i18n.reQ.exec(testSummary);
if (resSum2 !== null && i18n.dico[resSum2[1]] !== null){
testSummary = testSummary.replace("[["+resSum2[1]+"]]", i18n.dico[resSum2[1]] + " <small>(<a href=\"https://www.wikidata.org/wiki/"+resSum2[1]+"\">"+resSum2[1]+"</a>)</small>");
}
var resSum3 = i18n.reD.exec(testSummary);
if (resSum3 !== null && typeof i18n.dico[resSum3[1]] != 'undefined'){
testSummary = testSummary.replace(resSum3[1], i18n.dico[resSum3[1]]+" ");
}
testSummary = testSummary.replace("/*", "<span class=\"autocomment\">");
testSummary = testSummary.replace("*/", "</span>");
var txt = "<li id=\"wef-history\"><span>"
+ "<abbr style=\"font-size:bold; margin-left:0.5em;\" title=\"" + i18n.linePartTooltip + "\">" + i18n.linePartLetter + "</abbr> <span class=\"mw-changeslist-separator\">. .</span> "
+ "(<a href=\"https://www.wikidata.org/w/index.php?diff=" + id + "\">diff</a>)</span> <span class=\"mw-changeslist-separator\">. . . . .</span> "
+ "<span class=\"mw-changeslist-date\">" + dateAff + " à " + heureAff + "</span> "
+ "<span class=\"history-user\">" + author + "</span><span class=\"mw-changeslist-separator\">. .</span> "
+ "<span class=\"comment\">" + testSummary + "</span></li>";
insertChang (txt, updated) ;
}
}
function insertChang( txt, updated ) {
"use strict";
$('ul#pagehistory > li ').each(function(){
//console.log (this + " " + $(this).text());
//si c'est pas du wikidata
if ($(this).text().substr(0,1) != 'd'){
var resH = i18n.reH.exec($(this).text());
if (resH !== null){
var d = new Date( parseInt(resH[5]), parseInt(i18n.revMonthes[resH[4]]-1), parseInt(pad(resH[3])), parseInt(resH[1]), parseInt(resH[2]), 0);
if (d<updated) {
$(this).before(txt);
return false;
}
}
}
});
}
{
mw.loader.using( [ 'ext.gadget.wefcore', ], function() {
showChanges();
} );
}
} );
}
//