MediaWiki:Gadget-WhatReallyLinksHere.js

(function {	if (window.WhatReallyLinksHereLoaded) return;	window.WhatReallyLinksHereLoaded = true;	mw.loader.using( 'mediawiki.api' ).then(function { var api = new mw.Api({ ajax: { timeout: 60 * 1000 * 5 }}); var actualPageLinks = "No Links Found"; var linkBase = location.protocol + '//' + location.host + '/wiki/'; var whatReallyLinksHerePage = "Special:WhatReallyLinksHere"; var processingLinks = false; var maxNumProcessingAtOnce = 250; var numLinks = 0; var totalLinks = 0; var currNumProcessing = 0; var lasthash = location.hash; var mainLinkArea = '#AllWhatLinksHere'; var RedirectArea = '#AllRedirectArea'; var hasRedirects = false; var linkData = { links: [], Redirects: [] }; var wgPageName = mw.config.get('wgPageName'); function generateSpecialPage { var WhatReallyLinksHereContainer = $(' ');

var inputBox = $(' '); inputBox.append($(' ').append($(' ')).append($(' ')).append($(' ')).append($(' ')).append($(' '))); var searchBoxForm = $(''); var searchBoxInput = $(''); var searchBoxButton = $('<input type="submit" name="fulltext" class="mw-ui-button" style="background-color: #f8f9fa;color: #222222;display: inline-block;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;min-width: 4em;max-width: 28.75em;margin: 0;padding: 0.57142857em 0.9375em;border: 1px solid #a2a9b1;border-radius: 2px;font-family: inherit;font-size: 1em;font-weight: bold;line-height: 1;text-align: center;-webkit-appearance: none;zoom: 1;vertical-align: middle;cursor: pointer;" value="What Really Links Here" />'); searchBoxForm.append($(' ').append(searchBoxInput).append($(' ')).append(searchBoxButton)); var filterForm = $(' ').append(searchBoxInput.clone.prop('name','filter').prop('id','FilterTxt').css('width','300px')).append($(' ')).append(searchBoxButton.clone.prop('type','button').prop('name','filter').prop('id','FilterBtn').val('Link Text Filter').css('margin-top','5px')).append(searchBoxButton.clone.prop('type','button').prop('name','clearfilter').prop('id','ClearFilterBtn').val('Clear Filter').css({'margin-top':'5px','margin-left':'3px'})); filterForm.append($(' ')).append($(' ').append($(' ')).append($(' ').text('No Display Text'))); filterForm.append($(' ').append($(' ')).append($(' ').text('Partial Match'))); searchBoxForm.append(filterForm); searchBoxForm.submit(function(e) {               e.preventDefault;                if (processingLinks)                    return;                processingLinks = true;                $('#LoadingInfo').show;                $(mainLinkArea).html();                $(RedirectArea).html();                var searchText = $('#WhatReallyLinksHereForm').find('#searchBox').val;                if (searchText !== '') {                	updateTitleHeading('What Really Links Here');                    setTimeout(function { getLinksFromPage(decodeURIComponent(searchText), '', false); }, 200);               } else                 	updateTitleHeading(searchText);                return false;            }); searchBoxForm.find('#FilterBtn').click(function(e) {               var inputInfo = $('#FilterTxt').val;                if (inputInfo !== '') {                    highlightFilters(inputInfo);                }            }); searchBoxForm.find('#ClearFilterBtn').click(function(e) {           	$('#FilterCount').text();                $('.filterHighlights').css('background-color',).removeClass('filterHighlights');            }); inputBox.append(searchBoxForm); WhatReallyLinksHereContainer.append(inputBox).append($(' ')).append($(' '));

var loadingInfoBox = $(''); var center = $(' ').append($(' Loading. Please wait. 0/0 Done ')).append($('')); loadingInfoBox.append(center); WhatReallyLinksHereContainer.append(loadingInfoBox); $('#mw-content-text').children.remove; $('#mw-content-text').append(WhatReallyLinksHereContainer); var firstHeadingText = 'What Really Links Here'; if (wgPageName.indexOf('/') != -1) { var subSearchPage = wgPageName.substr(wgPageName.indexOf('/')+1); if (subSearchPage !== '') { $('#WhatReallyLinksHereForm').find('#searchBox').val(subSearchPage); $('#WhatReallyLinksHereForm').submit; firstHeadingText = 'Pages that really link to "' + subSearchPage + '"'; }           }            updateTitleHeading(firstHeadingText); }       function highlightFilters(filterText) { $('.filterHighlights').css('background-color','').removeClass('filterHighlights'); var filterCount = 0; if (linkData.links.length > 0) { $.each(linkData.links, function(link) {                   for (var i = 0; i < linkData.links[link].arr.length; i++) {                        if (linkData.links[link].arr[i].indexOf('|') > -1) {                            var linkText = linkData.links[link].arr[i].substr(linkData.links[link].arr[i].indexOf('|')+1);                            linkText = linkText.substr(0,linkText.indexOf(']'));                            if (linkText.toLowerCase.indexOf(filterText) > -1) {                            	filterCount++;                                $('#'+linkData.links[link].id).addClass('filterHighlights').css('background-color','#5ff765');                            }                        } else {                        	$('#'+linkData.links[link].id).addClass('filterHighlights').css('background-color','#FFC107');                        }                    }                }); }           if (linkData.Redirects.length > 0) { $.each(linkData.Redirects, function(link) {                   for (var i = 0; i < linkData.Redirects[link].arr.length; i++) {                        if (linkData.Redirects[link].arr[i].indexOf('|') > -1) {                            var linkText = linkData.Redirects[link].arr[i].substr(linkData.Redirects[link].arr[i].indexOf('|')+1);                            linkText = linkText.substr(0,linkText.indexOf(']'));                            if (linkText.toLowerCase.indexOf(filterText) > -1) {                            	filterCount++;                                $('#'+linkData.Redirects[link].id).addClass('filterHighlights').css('background-color','#5ff765');                            }                        }                    }                }); }           $('#FilterCount').text(filterCount+' Matches from filters.'); }       function updateTitleHeading(newText) { $('#firstHeading').text(newText); document.title = newText + ' - The Fallout Wiki'; }		function getLinksFromPage(pageName, lhContinue, isRedirectPage) { if (lhContinue === undefined) lhContinue = ''; api.get({				action: 'query',				prop: 'linkshere',				format: 'json',				lhlimit: 500,				titles: pageName,				lhcontinue: lhContinue			}).done(function(e) {				linkData = { links: [], Redirects: [] };				$('#LinkCount').html();				$('#RedirectCount').html();				$('#FilterCount').html('');				if (e.continue !== undefined)					getLinksFromPage(pageName, e.continue.lhcontinue, isRedirectPage);				var linkInfos = e.query.pages;		       for (var titleLink in linkInfos) {		        	if (linkInfos[titleLink].linkshere !== undefined) {			            numLinks += linkInfos[titleLink].linkshere.length;			            totalLinks += linkInfos[titleLink].linkshere.length;			            for (var linkInf in linkInfos[titleLink].linkshere) {			            	if (linkInfos[titleLink].linkshere[linkInf].redirect !== undefined) {                                numLinks -= 1;                                if (!hasRedirects) { $(RedirectArea).append(' Redirects: '); hasRedirects = true; }			           		$(RedirectArea).append(' '+linkInfos[titleLink].linkshere[linkInf].title+'  '); getLinksFromPage(linkInfos[titleLink].linkshere[linkInf].title, '', true); } else checkIfLinksReal(linkInfos[titleLink].linkshere[linkInf], pageName, isRedirectPage); }		       	} else { if (!isRedirectPage) { $('#LinkCount').html('0 Page Links Found'); $(mainLinkArea).append(actualPageLinks); $('#LoadingInfo').hide; $('#LoadingInfo').find('#RecordInfo').text('0/0 Records Done.'); processingLinks = false; }		       	}		        }			});		}		function checkIfLinksReal(pageLinks, basePageName, IsRedirectPage) {			if (currNumProcessing >= maxNumProcessingAtOnce)			{				setTimeout(function { checkIfLinksReal(pageLinks,basePageName, IsRedirectPage) }, 100);				return;			}			currNumProcessing += 1;			api.get({ action: 'parse', prop: 'wikitext', format: 'json', page: pageLinks.title }).done(function(data) { if (data.error === undefined) { var wikitext = data.parse.wikitext['*']; var pageLinkArr = findLinkInPage(basePageName, wikitext); if (pageLinkArr !== null) { var linkDataID = stripRegExp(basePageName)+'_'+stripRegExp(pageLinks.title); var checkContainer = mainLinkArea; var ContainerID = ' id="'+linkDataID+'"'; if (IsRedirectPage) checkContainer = '#'+stripRegExp(basePageName); if (IsRedirectPage) { linkData.Redirects[linkData.Redirects.length] = { id: linkDataID, arr: pageLinkArr}; } else linkData.links[linkData.links.length] = { id: linkDataID, arr: pageLinkArr}; $(checkContainer).append($(''+pageLinks.title+' ')); var allLinks = $(checkContainer+' div').sort(function (a,b) { 		               	var aText = $(a).find('a').text;		                	var bText = $(b).find('a').text;							if (aText > bText)								return 1;							if (aText < bText)								return -1;							return 0;						}); $(checkContainer).append(allLinks); }		           numLinks -= 1; currNumProcessing -= 1; $('#LoadingInfo').find('#RecordInfo').text((totalLinks-numLinks).toString+'/'+totalLinks.toString+ ' Records Done.'); if (numLinks <= 0) { if ($(mainLinkArea).children('div').length === 0 && $(RedirectArea).children('div').length === 0) $(mainLinkArea).append(actualPageLinks); if ($(RedirectArea).children('div').length > 0) $(RedirectArea).children('div').children('div').each(function(i, e) {                               if ($(e).children('div').length === 0)                                    $(e).append(actualPageLinks);                            }); $('#LoadingInfo').hide; $('#LoadingInfo').find('#RecordInfo').text('0/0 Records Done.'); $('#LinkCount').text(linkData.links.length+' page links found.'); $('#RedirectCount').text(linkData.Redirects.length+' from redirects.'); totalLinks = 0; processingLinks = false; }		       } else { numLinks -= 1; currNumProcessing -= 1; if (numLinks <= 0) { if ($(mainLinkArea).children('div').length === 0 && $(RedirectArea).children('div').length === 0) $(mainLinkArea).append(actualPageLinks); if ($(RedirectArea).children('div').length > 0) $(RedirectArea).children('div').children('div').each(function(i, e) {                               if ($(e).children('div').length === 0)                                    $(e).append(actualPageLinks);                            }); $('#LoadingInfo').hide; $('#LoadingInfo').find('#RecordInfo').text('0/0 Records Done.'); $('#LinkCount').text(linkData.links.length+' page links found.'); $('#RedirectCount').text(linkData.Redirects.length+' from redirects.'); totalLinks = 0; processingLinks = false; }		       	console.log(parseWikiTextUrl+pageLinks.title); console.log(data.error); }			});		}		function concatPageLink {			return linkBase+whatReallyLinksHerePage+'/'+wgPageName;		}		function escapeRegExp(text) {		 return text.replace(/[-[\]{}*+?.,\\^$|#\s]/g, '\\$&');		}		function stripRegExp(text) {		  return text.replace(/[-[\]{}*+?.,\\^$|#\s'":\/]/g, '');		}		function findLinkInPage(SearchPageTitle, wikiText) {		    var modPageTitle = escapeRegExp(SearchPageTitle).replace(/\\*[ _]/g,'[ _]');		    var regexStr = '\\[{2}' + modPageTitle + '([#\\|](([^\\]]|\\](?=[^\\]]))*))?\\]{2}';		    var testReg = new RegExp(regexStr, 'g');            var matches = wikiText.match(testReg);		    return matches;		}		$(document).ready(function {			if (wgPageName.indexOf(whatReallyLinksHerePage) === 0) {				generateSpecialPage;			}            var whatReallyLinksHereElem = $('').append($('').text('What really links here')); var baseLink = $('#cosmos-toolbar').find('#t-whatlinkshere'); if (baseLink.length > 0) { //baseLink = baseLink.parent; whatReallyLinksHereElem.insertAfter(baseLink); } else { baseLink = $('#cosmos-toolbar').find('.tools-customize'); whatReallyLinksHereElem.insertBefore(baseLink); }		});	}); });