MediaWiki:Linkscount.js: Difference between revisions

From Wikidata
Jump to navigation Jump to search
Content deleted Content added
m Fulfilling edit request by Kaleem Bhatti
Use serverErrorText if the server responded with error
Line 112: Line 112:
dbname: mw.config.get('wgDBname')
dbname: mw.config.get('wgDBname')
}).then(function (response) {
}).then(function (response) {
if (response['#error']) {
reinit();
mw.notify(messages.serverErrorText);
return;
}
var text = response.pagelinks.toLocaleString(userLanguage);
var text = response.pagelinks.toLocaleString(userLanguage);
if (response.templatelinks) {
if (response.templatelinks) {

Revision as of 14:23, 24 December 2019

// MediaWiki integrated backlinks count tool
// Author: User:Ebrahim
$(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 messages = (function () {
		var translations = {
			de: {
				linksCountText: 'Links: ',
				transclusionCountText: 'Vorlageneinbindungen: ',
				fileLinkCountText: 'lokale Dateiverwendung: ',
				fileGlobalLinkCountText: 'globale Dateiverwendung: ',
				countText: 'zählen',
				invalidTitleText: 'ungültiger Titel',
				serverErrorText: 'Serverfehler; klicke für einen weiteren Versuch',
				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: ','
			},
			fa: {
				linksCountText: 'پیوندها: ',
				transclusionCountText: 'تراگنجایش‌ها: ',
				fileLinkCountText: 'استفادهٔ محلی پرونده: ',
				fileGlobalLinkCountText: 'استفادهٔ سراسری پرونده: ',
				countText: 'تعداد',
				invalidTitleText: 'عنوان نادرست',
				serverErrorText: 'خطای سرور، برای تلاش مجدد کلیک کنید',
				comma: '،'
			},
			ckb: {
				linksCountText: 'بەستەرەکان: ',
				transclusionCountText: 'ترانسکڵووژن: ',
				fileLinkCountText: 'بەکارھێنانی خۆماڵیی پەڕگە: ',
				fileGlobalLinkCountText: 'بەکارھێنانی سەرانسەریی پەڕگە: ',
				countText: 'ژمارە',
				invalidTitleText: '',
				serverErrorText: '',
				comma: '،'
			},
			he: {
				linksCountText: 'קישורים: ',
				transclusionCountText: 'הכללות: ',
				fileLinkCountText: 'שימוש מקומי בקבצים: ',
				fileGlobalLinkCountText: 'שימוש גלובלי בקבצים: ',
				countText: 'ספירה',
				invalidTitleText: 'כותרת לא חוקית',
				serverErrorText: 'שגיאת שרת, לחץ כדי לנסות שוב',
				comma: ','
			},
			hu: {
				linksCountText: 'Linkek: ',
				transclusionCountText: 'Beillesztések: ',
				fileLinkCountText: 'Fájlhivatkozások: ',
				fileGlobalLinkCountText: 'Globális fájlhivatkozások: ',
				countText: 'Hivatkozások száma',
				invalidTitleText: 'Érvénytelen cím',
				serverErrorText: 'Szerverhiba, kattints az újrapróbálkozáshoz',
				comma: ','
			},
			sd: {
				linksCountText: 'ڳنڌڻا: ',
				transclusionCountText: 'نتيجو: ',
				fileLinkCountText: 'عام طور تي فائيل استعمال: ',
				fileGlobalLinkCountText: 'گلوبل فائل استعمال: ',
				countText: 'ڳڻپ',
				invalidTitleText: 'غلط عنوان',
				serverErrorText: 'سرور جي غلطي، ٻيهر ڪوشش ڪرڻ لاء ڪلڪ ڪريو',
				comma: '،'
			},
		};
		return translations[userLanguage]
			? translations[userLanguage]
			: translations.en;
	}());
	
	var finished;
	function init() {
		finished = false;
		$('#linkscount-tool').remove();
		$('fieldset:first').append($('<span>', {
			id: 'linkscount-tool'
		}).append(' ', $('<a>', {
			text: '(' + messages.countText + ')'
		}).click(function (e) {
			e.preventDefault();
			finished = true;
			var button = $(this).css('color', 'lightgray');
			var page = $('#mw-whatlinkshere-target, input[name=target]').val();
			var ns = 0;
			if (page.indexOf(':') !== -1) {
				var possibleNs = page.split(':')[0].toLowerCase();
				if (Object.keys(mw.config.get('wgNamespaceIds')).indexOf(possibleNs) !== -1) {
					ns = mw.config.get('wgNamespaceIds')[possibleNs];
					page = page.split(':').slice(1).join(':');
				}
			}
			$.post('//tools.wmflabs.org/linkscount/', {
				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']) {
					reinit();
					mw.notify(messages.serverErrorText);
					return;
				}
				var text = response.pagelinks.toLocaleString(userLanguage);
				if (response.templatelinks) {
					text = messages.transclusionCountText +
						response.templatelinks.toLocaleString(userLanguage) +
						messages.comma + ' ' + messages.linksCountText + text;
				}
				if (response.filelinks) {
					text = messages.fileLinkCountText +
						response.filelinks.toLocaleString(userLanguage) +
						messages.comma + ' ' + (response.templatelinks
							? text
							: messages.linksCountText + text);
				}
				if (response.globalfilelinks) {
					text = messages.fileGlobalLinkCountText +
						response.globalfilelinks.toLocaleString(userLanguage) +
						messages.comma + ' ' + (response.templatelinks || response.filelinks
							? text
							: messages.linksCountText + text);
				}
				button.replaceWith($('<span>').text('(' + text + ')'));
			});
		})));
	}
	
	init();
	function reinit() { if (finished) { init(); }}

	$('#namespace, #nsinvert').change(reinit);
	$('input[name=target]').change(reinit);
	$('#mw-whatlinkshere-target').keyup(reinit);
});