MediaWiki:Linkscount.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * @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: '،'
		},
		ckb: {
			linksCountText: 'بەستەرەکان: ',
			transclusionCountText: 'ترانسکڵووژن: ',
			fileLinkCountText: 'بەکارھێنانی خۆماڵیی پەڕگە: ',
			fileGlobalLinkCountText: 'بەکارھێنانی سەرانسەریی پەڕگە: ',
			countText: 'ژمارە',
			invalidTitleText: '',
			serverErrorText: '',
			comma: '،'
		},
		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: '،'
		},
		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: ','
		},
		ru: {
			linksCountText: 'ссылок: ',
			transclusionCountText: 'включений: ',
			fileLinkCountText: 'локальных использований файла: ',
			fileGlobalLinkCountText: 'глобальных использований файла: ',
			countText: 'посчитать',
			invalidTitleText: 'некорректное название',
			serverErrorText: 'ошибка сервера — нажмите, чтобы повторить запрос',
			comma: ','
		},
		sd: {
			linksCountText: 'ڳنڌڻا: ',
			transclusionCountText: 'نتيجو: ',
			fileLinkCountText: 'عام طور تي فائيل استعمال: ',
			fileGlobalLinkCountText: 'گلوبل فائل استعمال: ',
			countText: 'ڳڻپ',
			invalidTitleText: 'غلط عنوان',
			serverErrorText: 'سرور جي غلطي، ٻيهر ڪوشش ڪرڻ لاء ڪلڪ ڪريو',
			comma: '،'
		},
		sv: {
			linksCountText: 'länkar: ',
			transclusionCountText: 'inkluderingar: ',
			fileLinkCountText: 'lokal filanvändning: ',
			fileGlobalLinkCountText: 'global filanvändning: ',
			countText: 'antal',
			invalidTitleText: 'ogiltig titel',
			serverErrorText: 'serverfel, klicka för att försöka igen',
			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);
    });
})();