MediaWiki:Gadget-RevdelInfo.js

// /*jshint undef:true */ /*global mw, jQuery */ (function{

var api = new mw.Api;

function el(tag, child, attr, events) { var node = document.createElement(tag); if (child) { if (typeof child !== 'object') child = [child]; for (var i = 0; i < child.length; ++i) { var ch = child[i]; if ((ch === void(null)) || (ch === null)) continue; else if (typeof ch !== 'object') ch = document.createTextNode(String(ch)); node.appendChild(ch); }	}	if (attr) for (var key in attr) { node.setAttribute(key, String(attr[key])); }	if (events) for (var key in events) { node.addEventListener(key, events[key], false); }	return node; }

function fmtDate(date) { // XXX: uses browser's time zone instead of preferences date = new Date(date);

switch (mw.user.options.get('date')) { case 'dmy'    : return date.toLocaleTimeString + ', ' + date.getDate + ' ' + mw.language.months.genitive[date.getMonth] + ' ' + date.getFullYear; case 'mdy'    : return date.toLocaleTimeString + ', ' + mw.language.months.genitive[date.getMonth] + ' ' + date.getFullYear + ', ' + date.getDate; case 'ymd'    : return date.toLocaleTimeString + ', ' + date.getFullYear + ' ' + mw.language.months.genitive[date.getMonth] + ' ' + date.getDate; case 'default' : return date.toLocaleString; case 'ISO 8601': return date.toISOString; } }

function makeBox(rev1, rev2) { var query = { 'action': 'query', 'list': 'logevents', 'leaction': 'delete/revision', 'letitle': mw.config.get('wgPageName'), 'continue': '' };	var list, node = el('div', [		el('p', 'Details from the deletion log:'),		list = el('ul')	], {		'class': 'mw-warning-with-logexcerpt',		'style': 'display: none;'	}); function processResult(result) { var events = result.query.logevents; for (var i = 0; i < events.length; ++i) { if ((events[i][1] === rev1) || (events[i][1] === rev2)) { var item = events[i]; // XXX: item[2] and item[3] contain flags... might as well show these too // XXX: change link to plaintext based on whether the user has the 'deleterevision' right

list.appendChild(el('li', [ fmtDate(item.timestamp), ": ", el('a', item.user, { href: mw.util.getUrl('User:' + item.user) }), " changed visibility of ", el('a', ["revision ", item[1]], { href: mw.util.getUrl(mw.config.get('wgPageName'), { oldid: item[1], unhide: '1' }) }), item.comment && " (", item.comment, item.comment && ")" ]));				node.style.display = ''; }		}		if (result['continue']) { for (var key in result['continue']) query[key] = result['continue'][key]; api.get(query).then(processResult); }	}	api.get(query).then(processResult);

return node; }

if (mw.config.get('wgAction') !== 'view') return; if (!(mw.util.getParamValue('diff') || mw.util.getParamValue('oldid'))) return;

if (document.getElementById('mw-rev-deleted-no-diff') || document.getElementById('mw-rev-deleted-unhide-diff')) { var rev1, rev2;

var ot = document.getElementById('mw-diff-otitle1'); var nt = document.getElementById('mw-diff-ntitle1'); if (ot) { ot = new mw.Uri(ot.getElementsByTagName('a')[0].href); rev1 = ot.query.oldid; } if (nt) { nt = new mw.Uri(nt.getElementsByTagName('a')[0].href); rev2 = nt.query.oldid; }

document.getElementById('mw-content-text').appendChild(makeBox(rev1, rev2)); } else if (document.getElementById('mw-content-text').firstChild.classList.contains('mw-warning')) { // ugly, ugly hack var rev = mw.util.getParamValue('oldid'); document.getElementById('mw-content-text').appendChild(makeBox(rev, null)); }

});