ميدياويكي:Gadget-Linkscount.js

من ويكيبيديا، الموسوعة الحرة

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

/**
 * @description MediaWiki integrated backlinks count tool
 * @author User:Ebrahim, User:Tacsipacsi
 */
(function () {
	if (['Whatlinkshere', 'GlobalUsage'].indexOf(mw.config.get('wgCanonicalSpecialPageName')) === -1) return;
	var isGlobalUsagePage = mw.config.get('wgCanonicalSpecialPageName') === 'GlobalUsage';

	var userLanguage = mw.config.get('wgUserLanguage');
	var translations = {
		ar: {
			linksCountText: 'الوصلات: ',
			transclusionCountText: 'التضمينات: ',
			fileLinkCountText: 'استخدامات الملف محليًّا: ',
			fileGlobalLinkCountText: 'استخدامات الملف عالميًّا: ',
			countText: 'العدد',
			invalidTitleText: 'عنوان غير صالح',
			serverErrorText: 'خطأ في الخادم، اضغط للمحاولةً مجددًا',
			comma: '،'
		},
		en: {
			linksCountText: 'links: ',
			transclusionCountText: 'transclusions: ',
			fileLinkCountText: 'file local usages: ',
			fileGlobalLinkCountText: 'file global usages: ',
			countText: 'count',
			invalidTitleText: 'invalid title',
			serverErrorText: 'server error, click to retry',
			comma: ','
		},
	};

	var finished;
	function init() {
		finished = false;
        $.i18n().load( translations );
		$('#linkscount-tool').remove();
		$('fieldset:first').append($('<span>', {
			id: 'linkscount-tool'
		}).append(' ', $('<a>', {
			text: '(' + $.i18n('countText') + ')'
		}).click(function (e) {
			e.preventDefault();
			finished = true;
			var title = mw.Title.newFromText($('input[name=target]').val());
			if (title === null) {
				mw.notify($.i18n('invalidTitleText'));
				return;
			}
			var button = $(this).css('color', 'lightgray');
			var ns = title.getNamespaceId();
			var page = title.getMain();
			$.post('https://linkscount.toolforge.org/', {
				namespace: isGlobalUsagePage ? 6 : ns,
				p: page,
				fromNamespace: $('#namespace').val(),
				invertFromNamespace: isGlobalUsagePage ? false : $('#nsinvert')[0].checked,
				dbname: mw.config.get('wgDBname')
			}).then(function (response) {
				if (response['#error']) {
					init();
					mw.notify($.i18n('serverErrorText'));
					return;
				}
				var text = response.pagelinks.toLocaleString(userLanguage);
				if (response.templatelinks) {
					text = $.i18n('transclusionCountText') +
						response.templatelinks.toLocaleString(userLanguage) +
						$.i18n('comma') + ' ' + $.i18n('linksCountText') + text;
				}
				if (response.filelinks) {
					text = $.i18n('fileLinkCountText') +
						response.filelinks.toLocaleString(userLanguage) +
						$.i18n('comma') + ' ' + (response.templatelinks
							? text
							: $.i18n('linksCountText') + text);
				}
				if (response.globalfilelinks) {
					text = $.i18n('fileGlobalLinkCountText') +
						response.globalfilelinks.toLocaleString(userLanguage) +
						$.i18n('comma') + ' ' + (response.templatelinks || response.filelinks
							? text
							: $.i18n('linksCountText') + text);
				}
				button.replaceWith($('<span>').text('(' + text + ')'));
			}).catch(function () {
				init();
				mw.notify($.i18n('serverErrorText'));
			});
		})));
	}

	function reinit() { if (finished) { init(); }}

    mw.loader.using(['mediawiki.Title', 'jquery.i18n']).then(function () {
        $(init);
        $('#namespace, #nsinvert').change(reinit);
		$('input[name=target]').change(reinit);
		$('input[name=target]').keyup(reinit);
    });
})();