User:R'n'B/dabcolorizer.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:R'n'B/dabcolorizer. |
// Version 0.3; alpha release
importScriptURI("http://en.wikipedia.org/w/index.php?title=User:Luasóg bot/framework.js&action=raw&ctype=text/javascript");
// 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, "@");
};
mw.RnB.log = function (arg) {
if (typeof console !== "undefined") {
console.log(arg);
} else {
mw.log(arg); // does nothing unless page is loaded in debug mode
}
};
// dabtag script
mw.RnB.dabtag = {
'initialize': function () {
var app = mw.RnB.dabtag; //shortcut
// wait until framework has been loaded
if (! window.Luasog) {
setTimeout(app.initialize, 125);
return;
}
// wrapper for Luasog bot request method
Luasog.prototype.raw_request = Luasog.prototype.request;
// wraps request with handling of maxlag
Luasog.prototype.request = function (__luas_args, __luas_callback) {
__luas_args.maxlag = "5";
var lagpattern = /Waiting for [\d.]+: (\d+) seconds? lagged/;
var __luas_this = this;
this.raw_request(__luas_args, function (response_data) {
// callback here once data is received
var lagmatch, lagtime;
if (response_data.error) {
if (response_data.error.code === "maxlag") {
lagmatch = lagpattern.exec(response_data.error.info);
lagtime = parseInt(lagmatch[1], 10);
// set throttle into the future for lag delay
__luas_this.timeoflastrequest = ((new Date()).getTime()
+ Math.min(Math.max(5000, 500*lagtime), 120000));
mw.RnB.statusDiv.text("dabcolorizer: paused");
// repeat request
__luas_this.request(__luas_args, __luas_callback);
} else {
Luasog.exception({
name: response_data.error.code,
message: response_data.error.info
});
}
} else {
// no error code, go to original callback
__luas_callback(response_data);
}
});
};
// initialize the bot
app.bot = new Luasog(mw.config.get("wgServer")
+ mw.config.get("wgScriptPath") + "/api.php");
app.bot.speed = 10;
// get the list of disambig templates
app.bot.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.bot.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) + '"]';
mw.RnB.log("Looking for " + urltitle);
jQuery(urltitle).addClass(pagetitle.indexOf("(disambiguation)") === -1 ?
"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) {
app.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.bot.request(app.params, app.botCallback);
}
mw.RnB.statusDiv.text("dabcolorizer: working");
// save any redirects for future reference
pqr = linkdata.query.redirects;
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];
mw.RnB.log
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.RnB.log("Saving data for page " + page.title);
}
// skip red links
if (pageid < 0) {
mw.RnB.log("Skipping redlink " + page.title);
continue;
}
// skip links we have already identified as disambigs
if (page.hasOwnProperty("known_dab")) {
mw.RnB.log("Skipping known dab " + page.title);
continue;
}
// see if this link contains any templates
if (page.hasOwnProperty("templates")) {
mw.RnB.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") || mw.RnB.action === "submit" ||
mw.RnB.action === "edit") {
mw.RnB.dabtag.initialize();
}