User:H4stings/wef-history.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. |
![]() | Documentation for this user script can be added at User:H4stings/wef-history. |
/**
* wef-history
*
* This script displays in the history pages of Wikipedia changes of the corresponding Wikidata item.
*
* Author : H4stings.
*/
// Doc :
// 1. Add in your commons.js file : importScript('User:H4stings/wef-history.js');
// 2. Go to any Revision history page, it shoud work !
//
//////////////////////ZONE PERSONNALISABLE//////////////////////
if (typeof cacherModifLabel == 'undefined') { //hide wbsetlabel
cacherModifLabel = true;
}
if (typeof cacherModifAlias == 'undefined') { //hide wbsetaliases
cacherModifAlias = true;
}
if (typeof cacherModifLiens == 'undefined') { //hide wbsetsitelink & clientsitelink
cacherModifLiens = true;
}
if (typeof afficherTout == 'undefined') { //display all
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: 'Retrieving Wikidata revisions',
actionIntegrate: 'Displaying Wikidata revisions',
linePartLetter: 'd',
linePartTooltip: 'Wikidata',
linePartChange: 'diff.',
linePartHistory: 'history',
// 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]+) (\w+) ([0-9][0-9][0-9][0-9]) à ([0-9][0-9]):([0-9][0-9])/, // /!\ works only for french wikipedia. has to be rewritted for each wikipedia
reH : /([0-9][0-9]):([0-9][0-9]), ([0-9]+) (\w+) ([0-9][0-9][0-9][0-9])/, //14:52, 13 September 2014
};
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.en.value;
if(valr.labels && valr.labels.en && valr.labels.en.value){
//console.log(keyr + " " + valr + " " + valr.labels.en.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("|en") == -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 + " hided because not en");
}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();
} );
}
} );
}