MediaWiki:Gadget-show-sysop-activity.js
Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.
- Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
- Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
- Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5.
//
// This is a big nasty javascript which does a *lot* of callbacks. It will do 8*numberofsysops calls...
//
// By [[:no:Bruker:Stigmj]] - 2008
//
Date.prototype.setISO8601 = function (string) {
var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\\.([0-9]+))?)?" +
"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
var d = string.match(new RegExp(regexp));
var offset = 0;
var date = new Date(d[1], 0, 1);
if (d[3]) { date.setMonth(d[3] - 1); }
if (d[5]) { date.setDate(d[5]); }
if (d[7]) { date.setHours(d[7]); }
if (d[8]) { date.setMinutes(d[8]); }
if (d[10]) { date.setSeconds(d[10]); }
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
if (d[14]) {
offset = (Number(d[16]) * 60) + Number(d[17]);
offset *= ((d[15] == '-') ? 1 : -1);
}
offset -= date.getTimezoneOffset();
time = (Number(date) + (offset * 60 * 1000));
this.setTime(Number(time));
};
function getSysopList() {
var numa = mw.util.$content.find( 'span.sysop-activity' );
if(numa.length > 0) {
for(var i=0;i<numa.length;i++) {
var username = _getTextS(numa[i]);
numa[i].style.display = 'block';
_remTextS(numa[i]);
var userlist = username.split('|');
var querystring = {
query: {
allusers: []
},
special: {
search: {
name: username
}
}
};
for(var j=0;j<userlist.length;j++) {
uname = userlist[j].replace(/\s*(=|\(.*?\))\s*/g, '');
querystring.query.allusers.push({ name: uname });
}
processAllSysops(querystring);
}
}
if (mw.config.get( 'wgPageName' ) == 'Wikipédia:Administradores/Relatório') {
$.getJSON( mw.util.wikiScript( 'api' ), {
action:'query',
format:'json',
list:'allusers',
augroup:'sysop',
aulimit:'500'
}, processAllSysops );
}
}
function _getTextS(obj) {
if(obj.nodeType == 3) {
return obj.nodeValue;
}
var txt = [];
var i = 0;
while(obj.childNodes[i]) {
txt[txt.length] = _getTextS(obj.childNodes[i]);
i++;
}
return txt.join('');
}
function _remTextS(obj) {
while(obj.firstChild) {
obj.removeChild(obj.firstChild);
}
}
function processAllSysops(obj) {
// Get the bodyContent-container to construct the table
var docobj = mw.util.$content[0];
var tc, i;
if (obj.special) {
var searchName = obj.special.search.name.replace(/\s/g, '_');
searchName = encodeURIComponent(searchName).replace(/%/g, '.');
docobj = document.getElementById('sysop-activity-'+searchName);
}
if (obj.query && docobj) {
var header = [ 'Nome','Patrulhamento','Bloqueio','Eliminação','Proteção',/*'Importação',*/'Concessão de direitos','Renomeação','Edição MW','Última edição','50ª edição', 'Total', 'E-mail' ];
var types = [ 'name','lastpatrol','lastblock','lastdelete','lastprotect',/*'lastimport',*/'lastrights','lastrenameuser','lastmwedit','lastedit','last50edit', 'editcount', 'emailable' ];
var itemlist = obj.query.allusers;
// Create a table object
var table = document.createElement('table');
table.setAttribute('class', 'wikitable');
var thead = document.createElement('thead');
var tfoot = document.createElement('tfoot');
var tbody = document.createElement('tbody');
var hr = document.createElement('tr');
for (i=0; i<header.length; i++) {
tc = document.createElement('th');
tc.appendChild(document.createTextNode(header[i]));
hr.appendChild(tc);
}
thead.appendChild(hr);
table.appendChild(thead);
hr = document.createElement('tr');
tc = document.createElement('td');
tc.setAttribute('style', 'font-size:x-small');
tc.appendChild(document.createTextNode('Todas as colunas mostram quando ocorreu a última ação correspondente'));
tc.setAttribute('colSpan', header.length);
hr.appendChild(tc);
tfoot.appendChild(hr);
table.appendChild(tfoot);
table.appendChild(tbody);
docobj.appendChild(table);
for(i=0;i<itemlist.length;i++) {
hr = document.createElement('tr');
for (var j=0; j<types.length; j++) {
tc = document.createElement('td');
tc.setAttribute('id', 'td-'+types[j]+'-' + itemlist[i].name);
tc.style.backgroundColor = '#cccccc';
tc.appendChild(document.createTextNode((j===0?itemlist[i].name:'n/d')));
hr.appendChild(tc);
}
tbody.appendChild(hr);
processSysop(itemlist[i].name, null);
}
}
}
function setCellS(obj, otype) {
// find the cell and write the value
// Let's get the username
if (obj.special) {
return;
}
var name = obj.user;
// and the timestamp
var ts = obj.timestamp;
// let's find the table-cell
var tc = document.getElementById('td-last' + otype + '-' + name);
if (!tc) {
return;
}
// now calculate the color of the cell
var tsDate = new Date();
tsDate.setISO8601(ts);
var date = new Date();
var days = (date - tsDate) / 86400000;
var a = document.createElement('a');
if ($.inArray( otype, [ 'edit' , '50edit' , 'mwedit' ]) > -1) {
a.setAttribute('href', mw.util.getUrl( 'Special:Contributions' ) + '?&contribs=user&limit=50&target='+ name + (otype == 'mwedit'?'&namespace=8':''));
} else {
a.setAttribute('href', mw.util.getUrl( 'Special:Log' ) + '?&type='+otype+'&user='+name);
}
if (days < 1) {
a.appendChild(document.createTextNode('hoje'));
} else if (days < 2) {
a.appendChild(document.createTextNode('ontem'));
} else {
a.appendChild(document.createTextNode(Math.ceil(days) + ' dias atrás'));
}
tc.replaceChild(a,tc.firstChild);
date.setDate(date.getDate()-180);
if (tsDate>date) {
tc.style.backgroundColor = '#00ff33';
} else {
date.setDate(date.getDate()-180);
if (tsDate>date) {
tc.style.backgroundColor = '#00ccff';
} else {
tc.style.backgroundColor = '#ff3300';
}
}
}
function processPatrolActionS(obj) {
if (obj) {
var len = obj.length;
for (var i=0; i<obj.length; i++) {
if (obj[i].patrol && obj[i].patrol.auto === 0) {
setCellS(obj[i], obj[i].type);
return 1;
}
}
}
return null;
}
function patrol_handlerS(obj) {
var r = obj;
if (r.query) {
if (r['query-continue'] && r.query) {
var ret = processPatrolActionS(r.query.logevents);
if (!ret) {
// Put in a safeguard. Anything over 1 year is quite enough
var tsDate = new Date();
tsDate.setISO8601(r['query-continue'].logevents.lestart);
var date = new Date();
date.setDate(date.getDate()-365);
if (tsDate>date) {
processSysop(r.query.logevents[0].user, r['query-continue'].logevents.lestart);
} else {
var tc = document.getElementById('td-lastpatrol-' + r.query.logevents[0].user);
if (!tc) {
return;
}
tc.innerHTML = '> 1 ano a ∞';
tc.style.backgroundColor = '#ff3300';
}
}
} else if (r.query) {
processPatrolActionS(r.query.logevents, null);
}
}
}
function processSysop(obj, from) {
var shouldcontinue = true;
var url = '';
var query = { action:'query', format:'json' };
var logs = { list:'logevents', lelimit:1, leuser: obj};
var edits = { list:'usercontribs', ucuser: obj };
if (from) {
$.extend(logs, { lestart: from, lelimit: 500 });
} else {
$.extend(logs, { lestart: from, lelimit: 100 });
}
var APICalls = {
'patrol': [$.extend({}, query, { list:'logevents', lelimit:500, leuser: obj, letype: 'patrol' }), patrol_handlerS ],
'block': [$.extend({}, query, logs, { letype: 'block' }), getLastActionS ],
'delete': [$.extend({}, query, logs, { letype: 'delete' }), getLastActionS ],
'protect': [$.extend({}, query, logs, { letype: 'protect' }), getLastActionS ],
//'import': [$.extend({}, query, logs, { letype: 'import' }), getLastActionS ],
'rights': [$.extend({}, query, logs, { letype: 'rights' }), getLastActionS ],
'rename-user': [$.extend({}, query, logs, { letype: 'renameuser' }), getLastActionS ],
'mw-edits': [$.extend({}, query, edits, { uclimit: 1, ucnamespace: 8 }), getLastMWeditS ],
'last-edit': [$.extend({}, query, edits, { uclimit: 1 }), getLastEditS ],
'last-50-edits': [$.extend({}, query, edits, { uclimit: 50 }), getLast50EditS ],
'user-info': [$.extend({}, query, { list:'users', usprop: 'editcount|emailable', ususers: obj }), getUserinfoS ]
};
if (!from) {
$.each( APICalls, function(api, data){
try {
$.getJSON( mw.util.wikiScript( 'api' ), data[0], data[1] );
} catch(e) {
mw.log( 'Error using API for ' + api );
}
});
}
}
function getLastActionS(obj) {
// Did we get something?
if (obj.query && obj.query.logevents[0]) {
setCellS(obj.query.logevents[0], obj.query.logevents[0].type);
}
}
function getLastMWeditS(obj) {
// Did we get something?
if (obj.query && obj.query.usercontribs[0]) {
setCellS(obj.query.usercontribs[0], 'mwedit');
}
}
function getLastEditS(obj) {
// Did we get something?
if (obj.query && obj.query.usercontribs[0]) {
setCellS(obj.query.usercontribs[0], 'edit');
}
}
function getLast50EditS(obj) {
// Did we get something?
if (obj.query && obj.query.usercontribs[0]) {
var len = obj.query.usercontribs.length;
if (len == 50) {
setCellS(obj.query.usercontribs[len-1], '50edit');
} else {
setCellS( {
special: {
nothing: {
really: 'nothing'
}
}
}, '50edit');
}
}
}
function getUserinfoS(obj) {
var txt, tc;
// Did we get something?
if (obj.query && obj.query.users[0]) {
// find the cell and write the value
// Let's get the username
var name = obj.query.users[0].name;
// let's find the table-cell for emailable
tc = document.getElementById('td-emailable-' + name);
if (!tc) {
return;
}
if (typeof obj.query.users[0].emailable != 'undefined') {
// Let's get the emailable-info
var emailable = obj.query.users[0].emailable;
var a = document.createElement('a');
a.setAttribute('href', mw.util.getUrl( 'Special:EmailUser/' + name ) );
a.appendChild(document.createTextNode('Sim'));
tc.replaceChild(a,tc.firstChild);
tc.style.backgroundColor = '#00ff33';
} else if (typeof obj.warnings == 'undefined') {
txt = document.createTextNode('Não');
tc.replaceChild(txt,tc.firstChild);
tc.style.backgroundColor = '#ff3300';
}
if (obj.query.users[0].editcount) {
// Let's get the editcount-info
var editcount = obj.query.users[0].editcount;
// let's find the table-cell for editcount
tc = document.getElementById('td-editcount-' +name);
if (!tc) {
return;
}
txt = document.createTextNode(editcount);
tc.replaceChild(txt,tc.firstChild);
tc.style.backgroundColor = '#00ff33';
}
}
}
var isDiffS = (document.location.search && (document.location.search.indexOf("diff=") != -1 || document.location.search.indexOf("oldid=") != -1));
if ((mw.config.get( 'wgAction' )=='view') && !isDiffS && $.inArray( mw.config.get( 'wgNamespaceNumber' ), [ 2, 3, 4, 5 ]) > -1 ) {
$(getSysopList);
}