MediaWiki:Gadget-AddContribNumberInNewPages.js
Apparence
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5./*
Auteur : Darkoneko 13/01/2009
Fonction : ajouter le nombre de contribs de la personne juste avant son lien "discuter", dans special:newpages
Réécritures : Dr Brains (26/10/2014), Od1n (26/12/2018)
{{Catégorisation JS|AddContribNumberInNewPages}}
*/
/* globals mw, $, AddContribNumberInNewPages_Texts */
if (mw.config.get('wgCanonicalSpecialPageName') === 'Newpages') {
mw.loader.using(['mediawiki.api', 'mediawiki.util', 'site', 'user'], function () {
var Texts = {
contributions: '$1 {{PLURAL:$1|contribution|contributions}}',
contributionsPage: 'Spécial:Contributions',
};
if (typeof AddContribNumberInNewPages_Texts !== 'undefined') {
Object.assign(Texts, AddContribNumberInNewPages_Texts);
}
function plural(text) {
return text.replace(/\{\{PLURAL:([^\}]+)\}\}/gi, function (match, p1) {
var params = p1.split('|');
var thisnumber = parseInt(params[0]);
var result = (thisnumber > 1) ? params[2] : params[1];
if (!result) result = '';
return result;
});
}
function addContribNumberInNewPages($) {
var content = document.getElementById('mw-content-text');
var linesList = content.getElementsByTagName('ul')[0].getElementsByTagName('li');
var users = {};
for (var a = 0, l = linesList.length; a < l; ++a) {
var userLink = linesList[a].getElementsByClassName('mw-userlink')[0];
var userName = userLink.textContent;
if (!users[userName]) {
users[userName] = {
isIP: userLink.classList.contains('mw-anonuserlink'),
lines: [],
};
}
users[userName].lines.push(linesList[a]);
}
$.each(users, function (userName, user) {
var deferred = $.Deferred();
if (user.isIP) {
getEditCountIP(userName, deferred);
} else {
getEditCountUser(userName, deferred);
}
deferred.then(function (editcount) {
updateLines(userName, user, editcount);
});
});
}
function getEditCountIP(userName, deferred, contribs, apiContinue) {
if (!contribs) {
contribs = 0;
}
var userGroups = mw.config.get('wgUserGroups');
var apiLimit = userGroups.includes('sysop') || userGroups.includes('bot') ? 5000 : 500;
var options = {
action: 'query',
list: 'usercontribs',
ucuser: userName,
ucprop: '',
uclimit: apiLimit,
formatversion: 2,
};
if (apiContinue) {
Object.assign(options, apiContinue);
}
new mw.Api()
.get(options)
.done(function (data) {
contribs += data.query.usercontribs.length;
if (data['continue']) {
getEditCountIP(userName, deferred, contribs, data['continue']);
} else {
deferred.resolve(contribs);
}
});
}
function getEditCountUser(userName, deferred) {
new mw.Api()
.get({
action: 'query',
list: 'allusers',
aufrom: userName,
auto: userName,
auprop: 'editcount',
formatversion: 2,
})
.done(function (data) {
var user = data.query.allusers[0];
if (user) {
deferred.resolve(user.editcount);
} else {
deferred.reject();
}
});
}
function updateLines(userName, user, editcount) {
var lines = user.lines;
var isIP = user.isIP;
for (var a = 0; a < lines.length; ++a) {
var usertoollinks = lines[a].getElementsByClassName('mw-usertoollinks')[0];
var linkText = plural(Texts.contributions.split('$1').join(editcount));
if (!isIP) {
var link = usertoollinks.getElementsByTagName('a')[1];
link.textContent = linkText;
link.classList.add('contribLink');
} else {
var newLink = document.createElement('a');
newLink.href = mw.util.getUrl(Texts.contributionsPage + '/' + userName);
newLink.textContent = linkText;
newLink.className = 'contribLink';
$(usertoollinks).find('a').first().after(' | ', newLink);
}
}
}
$(addContribNumberInNewPages);
});
}