Kontent qismiga oʻtish

MediaWiki:Gadget-BlockInfo.js

Vikilug‘atdan olingan

Eslatma: Saqlaganingizdan soʻng, oʻzgarishlarni koʻrish uchun brauzeringiz keshini tozalashingizga toʻgri kelishi mumkin.

  • Firefox / Safari: Shift tugmasini bosgan holda, Yangilash unsurlar darchasini bosing, yoki Ctrl-F5 yoki Ctrl-R (Macda ⌘-R) ni bosing
  • Google Chrome: Ctrl-Shift-R (Macda ⌘-Shift-R) ni bosing
  • Internet Explorer / Edge: Ctrlni bosgan holda, Yangilashni bosing, yoki Ctrl-F5ni bosing
  • Opera: Ctrl-F5ni bosing.
// {{documentation}}
/*jshint undef:true */
/*global jQuery, mw */
(function(){
'use strict';

var api = new mw.Api();

function el(tag, child, attr, events) {
	var key, 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 (key in attr) {
		node.setAttribute(key, String(attr[key]));
	}
 
	if (events) for (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(); 
	}
}

var wd2iw = {
	'metawiki': 'm'
};

function buildList(user, callback) {
	var isAnon = mw.util.isIPv4Address(user) || mw.util.isIPv6Address(user);

	var rq = api.get({
		action: 'query',
		list: 'blocks' + (isAnon ? '|globalblocks' : '|globalallusers'),
		
		// blocks
		bkip: isAnon ? user : void(window.warranty),
		bkusers: isAnon ? void(window.warranty) : user,
		bkprop: 'id|user|range|timestamp|expiry|by|reason|flags',
		bkdir: 'older',
		
		// globalblocks
		bgip: isAnon ? user : void(window.warranty),
		bgprop: isAnon ? 'id|address|range|timestamp|expiry|by|reason' : void(window.warranty),
		bgdir: isAnon ? 'older' : void(window.warranty),
		
		// globalallusers
		agufrom: !isAnon ? user : void(window.warranty),
		aguto: !isAnon ? user : void(window.warranty),
		agulimit: !isAnon ? 1 : void(window.warranty),
		aguprop: !isAnon ? 'lockinfo' : void(window.warranty)
	});
	
	rq.then(function (result) {
		var list = el('ul'), item, i;

		if (result.query.globalallusers && result.query.globalallusers[0] && ('locked' in result.query.globalallusers[0])) {
			list.appendChild(el('li', [
				"This account is globally locked", 
				" (", el('a', "globalauth log", { href: mw.util.getUrl('m:Special:Log/globalauth', { page: 'User:' + user + '@global' }) }), ")."
			]));
		}
		
		for (i = 0; i < result.query.blocks.length; ++i) {
			item = result.query.blocks[i];
			list.appendChild(el('li', [
				fmtDate(item.timestamp),
				': ', el('a', item.by, { href: mw.util.getUrl('User:' + item.by) }),
				" blocked ", el('b', item.user),
				" (", el('a', 'block log', { href: mw.util.getUrl('Special:Log/block', { page: 'User:' + item.user }) }), "),",
				' expiring ', el('b', item.expiry === 'infinity' ? 'never' : fmtDate(item.expiry)),
				item.reason && " (", item.reason, item.reason && ")",
			]));
		}
	
		if (result.query.globalblocks)
		for (i = 0; i < result.query.globalblocks.length; ++i) {
			item = result.query.globalblocks[i];
			list.appendChild(el('li', [
				fmtDate(item.timestamp),
				': ', el('a', item.by, { href: mw.util.getUrl(wd2iw[item.bywiki] + ':User:' + item.by) }),
				" at ", el('code', item.bywiki),
				" globally blocked ", el('b', item.address),
				" (", el('a', "gblblock log", { href: mw.util.getUrl(wd2iw[item.bywiki] + ':Special:Log/gblblock', { page: 'User:' + item.address }) }), "),",
				" expiring ", el('b', item.expiry === 'infinity' ? "never" : fmtDate(item.expiry)),
				item.reason && " (", item.reason, item.reason && ")",
			]));
		}
		
		callback(list);
	});
	
	return rq;
}

function makeBox(user, relTo) {
	if (!user)
		return;
	relTo = relTo || document.getElementById('mw-content-text');

	var afterList, box = el('div', [
		el('p', [el('strong', user), ' is currently blocked.']),
		afterList = el('small', [
			el('a', "block log"        , { href: mw.util.getUrl('Special:Log/block', { page: 'User:' + user }) })
		])
	], {
		'class': 'mw-warning-with-logexcerpt'
	});

	relTo.rq = buildList(user, function (list) {
		relTo.rq = null;
		if (!list.hasChildNodes())
			return;

		box.insertBefore(list, afterList);
		relTo.parentNode.insertBefore(box, relTo);
	});

	return box;
}

var user;

var wgNamespaceNumber = mw.config.get('wgNamespaceNumber');
var wgNamespaceIds = mw.config.get('wgNamespaceIds');
var wgTitle = mw.config.get('wgTitle');

if ((wgNamespaceNumber & ~1) === wgNamespaceIds.user) {
	makeBox(wgTitle.replace(/\/.*$/, ''));
} else if ((mw.config.get('wgCanonicalSpecialPageName') === 'Contributions') || (mw.config.get('wgCanonicalSpecialPageName') === 'DeletedContributions')) {
	makeBox(wgTitle.indexOf('/') !== -1 ? wgTitle.replace(/^.*?\//, '') : mw.util.getParamValue('target'));
} else if (mw.config.get('wgCanonicalSpecialPageName') === 'Log') {
	user = mw.util.getParamValue('user');
	if (/^[^\/]+\/(block|delete)$/.test(wgTitle))
		return;
	if (wgTitle.indexOf('/') !== -1)
		user = wgTitle.replace(/^.*\//, '');
	makeBox(user);
} else if (mw.config.get('wgCanonicalSpecialPageName') === 'Block') {
	var ipinput = document.getElementById('mw-bi-target');
	var relTo = ipinput.form.getElementsByTagName('legend')[0].nextSibling;
	var warnnode = null;

	var refresh = function () {
		if (relTo.rq)
			relTo.rq.abort();

		if (warnnode && warnnode.parentNode) {
			warnnode.parentNode.removeChild(warnnode);
			warnnode = null;
		}

		if (!ipinput.value)
			return;

		warnnode = makeBox(ipinput.value, relTo);
	};

	var tmout;
	ipinput.addEventListener('input', function () {
		clearTimeout(tmout);
		tmout = setTimeout(refresh, 500);
	}, false);
	ipinput.addEventListener('change', function () {
		refresh();
	}, false);
	refresh();
	return;
}

})();