Jump to content

User:R'n'B/dabcolorizer.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by R'n'B (talk | contribs) at 15:42, 28 June 2011 (minor updates). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Version 0.5; beta release

importScript("User:R'n'B/wrappi.js");

// create a private namespace for this script
if (! mw.RnB) {
	mw.RnB = {};
}
// Utility functions
mw.RnB.titleUrl = function (title) {
	var normalized = mw.util.wikiUrlencode(title);
	normalized = normalized.slice(0,1).toUpperCase() + normalized.slice(1);
	return normalized.replace(/%21/g, "!").replace(/%24/g, "$").
			// .replace(/'/g, "%27")
			replace(/%28/g, "(").replace(/%29/g, ")").
			replace(/%2A/g, "*").replace(/%2C/g, ",").
			replace(/%3B/g, ";").replace(/%40/g, "@");
};
// dabtag script
mw.RnB.dabtag = {
	initialize: function () {
		var app = mw.RnB.dabtag; //shortcut
		if (! mw.RnB.Wiki) {
			setTimeout(app.initialize, 100);
			return;
		}
		app.api = new mw.RnB.Wiki(),
		// get the list of disambig templates
		app.api.request(
			// query parameters
			{	'action': 'query',
				'prop': 'links',
				'titles': 'MediaWiki:Disambiguationspage',
				'plnamespace': '10',
				'pllimit': 'max'
			},
			// callback upon response
			function (data) {
				var link, pageid, thispage;
				app.dabTemplates = [];
				for (pageid in data.query.pages) {
					if (data.query.pages.hasOwnProperty(pageid)) {
						thispage = data.query.pages[pageid];
						for (link in thispage.links) {
							if (thispage.links.hasOwnProperty(link)) {
								app.dabTemplates.push(
										thispage.links[link].title);
							}
						}
					}
				}
				// set document ready function
				jQuery(function() {
					var myDiv = document.createElement('div');
					myDiv.appendChild(
							document.createTextNode('dabtag: loading'));
					myDiv.id = "colorizerStatus";
					jQuery('#siteSub').after(myDiv);
					mw.RnB.statusDiv = jQuery('#colorizerStatus');
					mw.RnB.statusDiv.css({	'display': 'block',
								'font-size': '92%',
								'font-weight': 'normal'
					});
					app.api.request(app.params, app.botCallback);
					mw.RnB.statusDiv.text("dabcolorizer: waiting for data");
				});
			}
		);
	},
	doHighlighting: function (pagetitle) {
		// finds all links on the dabpage to the target page, and
		// changes their CSS class
		var urltitle = 'a[href="/wiki/' + mw.RnB.titleUrl(pagetitle) + '"]',
		    urlsection = 'a[href^="/wiki/' + mw.RnB.titleUrl(pagetitle) + '#"]';
		mw.log("Looking for " + urltitle);
		jQuery(urltitle).add(urlsection).addClass(
				pagetitle.indexOf("(disambiguation)") === -1 ?
				"dabpagelink" : "dabpagelink intdablink");
	},
	params: {  // API query parameters for page queries
		'action': 'query',
		'generator': 'links',
		'prop': 'info|revisions|templates',
		'gplnamespace': '0',
		'gpllimit': 'max',
		'titles': mw.config.get("wgPageName"),
		'tllimit': 'max',
		'redirects': ''
	},
	pagelinkdata: {}, // holds results from API response
	ready: false,
	redirectsFromTo: {}, // maps redirects to targets, from API response
	redirectsToFrom: {}, // maps targets to redirects, from API response
	botCallback: function (linkdata) {
		// process results of the last API query, continue if necessary,
		// and change CSS class of found links to disambig pages
		var app = mw.RnB.dabtag, //shortcut
			isDisambig,
			page,
			pageid,
			pqr,
			qc,
			r,
			tmpl;
		if (! linkdata.query) {
			mw.log("Page link query failed!");
			return;
		}
		qc = linkdata["query-continue"];
		app.queryContinue = qc; /* for debugging */
		if (qc) {
			// query-continue was set, so launch another query
			if (qc.templates) {
				app.params.tlcontinue = qc.templates.tlcontinue;
				delete app.params.gplcontinue;
			} else if (qc.links) {
				delete app.params.tlcontinue;
				app.params.gplcontinue = qc.links.tlcontinue;
			}
			app.api.request(app.params, app.botCallback);
		}
		mw.RnB.statusDiv.text("dabcolorizer: working");
		// save any redirects for future reference
		pqr = linkdata.query.redirects;
		if (pqr !== undefined) {
			for (r=0; r < pqr.length; r++) {
				app.redirectsFromTo[ pqr[r].from ] =  pqr[r].to;
				app.redirectsToFrom[ pqr[r].to ] = pqr[r].from;
			}
		}
		for (pageid in linkdata.query.pages) {
			if (linkdata.query.pages.hasOwnProperty(pageid)) {
				page = linkdata.query.pages[pageid];
				if (app.pagelinkdata.hasOwnProperty(pageid)) {
					// this link already loaded, so see if there are
					// any new templates
					if (page.hasOwnProperty("templates")) {
						if (app.pagelinkdata[pageid]
								.hasOwnProperty("templates")) {
							jQuery.merge(app.pagelinkdata[pageid].templates,
										 page.templates);
						} else {
							app.pagelinkdata[pageid].templates = page.templates;
						}
					}
				} else {
					// first time we've seen this link
					app.pagelinkdata[pageid] = page;
					mw.log("Saving data for page " + page.title);
				}
				// skip red links
				if (pageid < 0) {
					mw.log("Skipping redlink " + page.title);
					continue;
				}
				// skip links we have already identified as disambigs
				if (page.hasOwnProperty("known_dab")) {
					mw.log("Skipping known dab " + page.title);
					continue;
				}
				// see if this link contains any templates
				if (page.hasOwnProperty("templates")) {
					mw.log("Checking templates on page " + page.title);
					// check templates to see if this is a disambig page
					isDisambig = false;
					for (tmpl=0; tmpl < page.templates.length; tmpl++) {
						if (app.dabTemplates
							   .indexOf(page.templates[tmpl].title) !== -1) {
							isDisambig = true;
							break;
						}
					}
					if (isDisambig) {
						page.known_dab = true;
						// find the link element(s) that match the title
						app.doHighlighting(page.title);
						// if page is target of a redirect, also highlight links
						//	 to the redirect
						if (app.redirectsToFrom[page.title]) {
							app.doHighlighting(app.redirectsToFrom[page.title]);
						}
					}
				}
			}
		}
		if (qc) {   // query is being continued
			mw.RnB.statusDiv.text("dabcolorizer: waiting for data");
		}
		else {      // query was not continued, we're done
			mw.RnB.statusDiv.text("dabcolorizer: done!");
			mw.RnB.statusDiv.fadeOut('slow');
		}
	}
};

mw.RnB.action = mw.config.get("wgAction");
if (mw.config.get("wgIsArticle") || jQuery("#wikiPreview").length > 0) {
	mw.RnB.dabtag.initialize();
}