User:Eckserah/common.js

(function {	if (window.WhatReallyLinksHereLoaded) return;	window.WhatReallyLinksHereLoaded = true;	mw.loader.using( 'mw.Api' ).then(function { var api = new mw.Api; 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'; function generateSpecialPage { var WhatReallyLinksHereContainer = $(' ');

var inputBox = $(' '); 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(searchBoxInput).append($(' ')).append(searchBoxButton); searchBoxForm.submit(function(e) {               e.preventDefault;                if (processingLinks)                    return;                processingLinks = true;                $('#LoadingInfo').show;                $(mainLinkArea).html();                $(RedirectArea).html();                var searchText = $('#WhatReallyLinksHereForm').find('input[type=text]').val;                if (searchText !== '') {                	updateTitleHeading('What Really Links Here');                    setTimeout(function { getLinksFromPage(decodeURIComponent(searchText), '', false); }, 200);               } else                 	updateTitleHeading(searchText);                return false;            }); 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); $('#WhatReallyLinksHereForm').find('input[type=text]').val(subSearchPage); $('#WhatReallyLinksHereForm').submit; firstHeadingText = 'Pages that really link to "' + subSearchPage + '"'; }           updateTitleHeading(firstHeadingText); }       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',				//lhshow: 'redirect|!redirect',				lhlimit: 500,				titles: pageName,				lhcontinue: lhContinue			}).done(function(e) {				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;			            		$(RedirectArea).append(' '+linkInfos[titleLink].linkshere[linkInf].title+'  '); getLinksFromPage(linkInfos[titleLink].linkshere[linkInf].title, '', true); } else checkIfLinksReal(linkInfos[titleLink].linkshere[linkInf], pageName, isRedirectPage); }		       	} else { $(mainLinkArea).append(actualPageLinks); $('#LoadingInfo').hide; $('#LoadingInfo').find('#RecordInfo').text('0/0 Records Done.'); }		       }			});		}		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) { if (IsRedirectPage) debugger; var wikitext = data.parse.wikitext['*']; if (findLinkInPage(basePageName, wikitext)) { var checkContainer = mainLinkArea; if (IsRedirectPage) checkContainer = '#'+stripRegExp(basePageName); $(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) { debugger; if ($(mainLinkArea).find('div').length === 0 && $(RedirectArea).find('div').length === 0) $(mainLinkArea).append(actualPageLinks); if ($(RedirectArea).find('div').length > 0) $(RedirectArea).find('div').find('div').each(function(i, e) {                               if ($(e).find('div').length === 0)                                    $(e).append(actualPageLinks);                            }); $('#LoadingInfo').hide; $('#LoadingInfo').find('#RecordInfo').text('0/0 Records Done.'); processingLinks = false; }		       } else { numLinks -= 1; currNumProcessing -= 1; if (numLinks <= 0) { debugger; if ($(mainLinkArea).find('div').length === 0 && $(RedirectArea).find('div').length === 0) $(mainLinkArea).append(actualPageLinks); $('#LoadingInfo').hide; $('#LoadingInfo').find('#RecordInfo').text('0/0 Records Done.'); 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 = '\\[\\[' + modPageTitle + '*[^\\]]+\\]\\]';		    var regexStr = '\\[{2}' + modPageTitle + '[|#\]*.*\\]{2}';		    var testReg = new RegExp(regexStr);		    return testReg.test(wikiText);		}		$(document).ready(function { if (wgPageName.indexOf(whatReallyLinksHerePage) === 0) { generateSpecialPage; }           var whatReallyLinksHereElem = $(' ').append($('').text('What really links here')); var baseLink = $('#WikiaBarWrapper').find('[data-name=whatlinkshere]'); if (baseLink.length > 0) { baseLink = baseLink.parent; whatReallyLinksHereElem.insertAfter(baseLink); } else { baseLink = $('#WikiaBarWrapper').find('.tools-customize'); whatReallyLinksHereElem.insertBefore(baseLink); }		});	}); });