Jump to content

User:R'n'B/dabcolorizer.js

From Wikipedia, the free encyclopedia
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.6; 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
console.log("dabtag: Initialization");
		if (! mw.RnB.Wiki) {
			setTimeout(app.initialize, 100);
			return;
		}
		app.api = new mw.RnB.Wiki();
		// set document ready function
		jQuery(function() {
			var myDiv = document.createElement('div');
console.log("dabtag: Document ready");
			myDiv.appendChild(
					document.createTextNode('dabtag: loading'));
			myDiv.id = "colorizerStatus";
			jQuery('#siteSub').after(myDiv);
			app.statusDiv = jQuery('#colorizerStatus');
			app.statusDiv.css({
				'display': 'block',
				'font-size': '92%',
				'font-weight': 'normal'
			});
			app.api.request(app.params, app.botCallback);
			app.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("Highlighting " + 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|pageprops',
		gplnamespace: '0',
		gpllimit: 'max',
		ppprop: 'disambiguation',
		titles: mw.config.get("wgPageName"),
		redirects: '',
		rawcontinue: ''
	},
	pagelinkdata: {}, // holds results from API response
	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
			i,
			isDisambig,
			page,
			pageid,
			pqr,
			qc,
			r,
			tmpl;
		if (! linkdata.query) {
			console.log("Page link query failed!");
			app.statusDiv.fadeOut('slow');
			return;
		}
console.log("dabtag: Linkdata", linkdata);
		qc = linkdata["query-continue"];
		app.queryContinue = qc; /* for debugging */
		if (qc) {
			// query-continue was set, so launch another query
			if (qc.links) {
				app.params.gplcontinue = qc.links.gplcontinue;
			}
			app.api.request(app.params, app.botCallback);
		}
		app.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;
				if (app.redirectsToFrom[ pqr[r].to ] === undefined) {
					app.redirectsToFrom[ pqr[r].to ] = [];
				}
				app.redirectsToFrom[ pqr[r].to ].push(pqr[r].from);
			}
		}
		// check each page link in the response
		for (pageid in linkdata.query.pages) {
			if (linkdata.query.pages.hasOwnProperty(pageid)) {
				page = linkdata.query.pages[pageid];
				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;
				}
/* SHOULD BE UNNECESSARY
				// 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 has the disambiguation property
				if (page.hasOwnProperty("pageprops") 
						&& page.pageprops.hasOwnProperty("disambiguation")) {
					app.pagelinkdata[pageid].known_dab = true;
//						mw.log("Marked " + page.title + " as known dab");
					// 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]) {
						for (i in app.redirectsToFrom[page.title]) {
							if (app.redirectsToFrom[page.title].hasOwnProperty(i)) {
								app.doHighlighting(app.redirectsToFrom[page.title][i]);
							}
						}
					}
				}
			}
		}
		if (qc) {   // query is being continued
			app.statusDiv.text("dabcolorizer: waiting for data");
		}
		else {      // query was not continued, we're done
			app.statusDiv.text("dabcolorizer: done!");
			app.statusDiv.fadeOut('slow');
		}
	}
};

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