MediaWiki:Linkscount.js

From Wikidata
Jump to navigation Jump to search

Note: After saving, 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: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
// MediaWiki integrated backlinks count tool
// Author: User:Ebrahim
$(function () {
	if (mw.config.get('wgCanonicalSpecialPageName') !== 'Whatlinkshere') return;

	var userLanguage = mw.config.get('wgUserLanguage');
	var messages = (function () {
		var translations = {
			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: '،'
			},
			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: ','
			}
	    };
	    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').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: ns,
				p: page,
				fromNamespace: $('#namespace').val(),
				invertFromNamespace: $('#nsinvert')[0].checked,
				dbname: mw.config.get('wgDBname')
			}).then(function (response) {
				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);
	$('#mw-whatlinkshere-target').keyup(reinit);
});