User:WhatamIdoing/common.js

// based on http://en.wikipedia.org/wiki/User:Fran Rogers/dimorphism.js // and on http://en.wikipedia.org/wiki/User:Splarka/sysopdectector.js

/*global window, wgNamespaceNumber, wgTitle, wgFormattedNamespaces wgArticlePath, wgScriptPath, addOnloadHook, importScriptURI, sajax_init_object*/

// for non-Firefox browsers if(!Array.prototype.map) { Array.prototype.map = function(fun /*, thisp*/) { var len = this.length >>> 0; if (typeof fun != "function") { throw new TypeError; }               var res = new Array(len); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) { res[i] = fun.call(thisp, this[i], i, this); }       }        return res; }; }

function UserinfoJsParseDate(utcDate) { // The ISO 8601 date format used by MediaWiki var s = /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/.exec(utcDate); if(s === null) { return null; }   var d = new Date; d.setUTCFullYear(s[1], s[2] - 1, s[3]); d.setUTCHours(s[4], s[5], s[6]); return d; }

function UserinfoJsFormatQty(qty, singular, plural) { return String(qty).replace(/\d{1,3}(?=(\d{3})+(?!\d))/g, "$&,") + "\u00a0" + (qty == 1 ? singular : plural); }

function UserinfoJsFormatDateRel(old) { // The code below requires the computer's clock to be set correctly. var age = new Date.getTime - old.getTime; var ageNumber, ageRemainder, ageWords; if(age < 60000) { // less than one minute old ageNumber = Math.floor(age / 1000); ageWords = UserinfoJsFormatQty(ageNumber, "second", "seconds"); } else if(age < 3600000) { // less than one hour old ageNumber = Math.floor(age / 60000); ageWords = UserinfoJsFormatQty(ageNumber, "minute", "minutes"); } else if(age < 86400000) { // less than one day old ageNumber = Math.floor(age / 3600000); ageWords = UserinfoJsFormatQty(ageNumber, "hour", "hours"); ageRemainder = Math.floor((age - ageNumber * 3600000) / 60000); } else if(age < 604800000) { // less than one week old ageNumber = Math.floor(age / 86400000); ageWords = UserinfoJsFormatQty(ageNumber, "day", "days"); } else if(age < 2592000000) { // less than one month old ageNumber = Math.floor(age / 604800000); ageWords = UserinfoJsFormatQty(ageNumber, "week", "weeks"); } else if(age < 31536000000) { // less than one year old ageNumber = Math.floor(age / 2592000000); ageWords = UserinfoJsFormatQty(ageNumber, "month", "months"); } else { // one year or older ageNumber = Math.floor(age / 31536000000); ageWords = UserinfoJsFormatQty(ageNumber, "year", "years"); ageRemainder = Math.floor((age - ageNumber * 31536000000) / 2592000000); if(ageRemainder) { ageWords += " " + UserinfoJsFormatQty(ageRemainder, "month", "months"); }          }           return ageWords; }

// If on a user or user talk page, and not a subpage... if((wgNamespaceNumber == 2 || wgNamespaceNumber == 3) && !(/\//.test(wgTitle))) { // add a hook to... mw.loader.using( 'mediawiki.util', function { addOnloadHook(function{ // Request the user's information from the API. // Note that this is allowed to be up to 5 minutes old. var et = encodeURIComponent(wgTitle); var x = sajax_init_object; x.open("GET", wgScriptPath + "/api.php?format=json&maxage=300&action=query&list=users|usercontribs&usprop=blockinfo|editcount|gender|registration|groups&uclimit=1&ucprop=timestamp&ususers=" + et + "&ucuser=" + et, true); x.onreadystatechange = function { if(x.readyState != 4 || x.status != 200) { return; }           // When response arrives extract the information we need. var query = eval("(" + x.responseText + ")"); if(!query.query) { return; } // Suggested by Gary King to avoid JS errors --PS 2010-08-25 query = query.query; var user, invalid, missing, groups, editcount, registration, blocked, gender, lastEdited; try { user = query.users[0]; invalid = typeof user.invalid != "undefined"; missing = typeof user.missing != "undefined"; groups = (typeof user.groups == "object") ? user.groups : []; editcount = (typeof user.editcount == "number") ? user.editcount : null; registration = (typeof user.registration == "string") ? UserinfoJsParseDate(user.registration) : null; blocked = typeof user.blockedby != "undefined"; gender = (typeof user.gender == "string") ? user.gender : null; lastEdited = (typeof query.usercontribs[0] == "object") && (typeof query.usercontribs[0].timestamp == "string") ? UserinfoJsParseDate(query.usercontribs[0].timestamp) : null; } catch(e) { return; // Not much to do if the server is returning an error (e.g. if the username is malformed). }

// Format the information for on-screen display var statusText = ""; var ipUser = false; var ipv4User = false; var ipv6User = false;

// User status if(blocked) { statusText += "blocked "; }           if (missing) { statusText += "username not registered"; } else if (invalid) { ipv4User = mw.util.isIPv4Address(user.name); ipv6User = mw.util.isIPv6Address(user.name); ipUser = ipv4User || ipv6User; if (ipv4User) { statusText += "anonymous IPv4 user"; } else if (ipv6User) { statusText += "anonymous IPv6 user"; } else { statusText += "invalid username"; }           } else { // User is registered and may be in a privileged group. Below we have a list of user groups. // Only need the ones different from the software's name (or ones to exclude), though. var friendlyGroupNames = { // Exclude implicit user group information provided by MW 1.17 --PS 2010-02-17 '*': false, 'user': false, 'autoconfirmed': false, sysop: "administrator", accountcreator: "account creator", "import": "importer", transwiki: "transwiki importer", "ipblock-exempt": "IP block exemption", oversight: "oversighter", confirmed: "confirmed user", abusefilter: "edit filter manager", autoreviewer: "autopatrolled user" // Group has been renamed --PS 2010-07-06 };               var friendlyGroups = []; for(var i = 0; i < groups.length; ++i) { var s = groups[i]; if(friendlyGroupNames.hasOwnProperty(s)) { if(friendlyGroupNames[s]) { friendlyGroups.push(friendlyGroupNames[s]); }                   } else { friendlyGroups.push(s); }               }                switch(friendlyGroups.length) { case 0: // User not in a privileged group // Changed to "registered user" by request of User:Svanslyck // --PS 2010-05-16 // statusText += "user"; if(blocked) { statusText += "user"; } else { statusText += "registered user"; }                       break; case 1: statusText += friendlyGroups[0]; break; case 2: statusText += friendlyGroups[0] + " and " + friendlyGroups[1]; break; default: statusText += friendlyGroups.slice(0, -1).join(", ") + ", and " + friendlyGroups[friendlyGroups.length - 1]; break; }           }            // Registration date if(registration) { statusText += ", " + UserinfoJsFormatDateRel(registration) + " old"; }           // Edit count if(editcount !== null) { statusText += ", with " + "" + UserinfoJsFormatQty(editcount, "edit", "edits") + ""; }           // Prefix status text with correct article if("AEIOaeio".indexOf(statusText.charAt(0)) >= 0) { statusText = "An " + statusText; } else { statusText = "A " + statusText; }

// Add full stop to status text statusText += ".";

// Last edited --PS 2010-06-27 // Added link to contributions page --PS 2010-07-03 if(lastEdited) { statusText += " Last edited " + UserinfoJsFormatDateRel(lastEdited) + " ago."; }

// Show the correct gender symbol var fh = window.document.getElementById("firstHeading") || window.document.getElementById("section-0"); // Add classes for blocked, registered, and anonymous users var newClasses = []; if(blocked) { newClasses.push("ps-blocked"); }           if(ipUser) { newClasses.push("ps-anonymous"); } else if(invalid) { newClasses.push("ps-invalid"); } else { newClasses.push("ps-registered"); }           fh.className += (fh.className.length ? " " : "") + groups.map(function(s) {                return "ps-group-" + s;            }).concat(newClasses).join(" "); var genderSpan = window.document.createElement("span"); genderSpan.id = "ps-gender-" + (gender || "unknown"); genderSpan.style.paddingLeft = "0.25em"; genderSpan.style.fontFamily = '"Lucida Grande", "Lucida Sans Unicode", "sans-serif"'; genderSpan.style.fontSize = "75%"; var genderSymbol; switch(gender) { case "male": genderSymbol = "\u2642"; break; case "female": genderSymbol = "\u2640"; break; default: genderSymbol = ""; break; }           genderSpan.appendChild(window.document.createTextNode(genderSymbol)); fh.appendChild(genderSpan);

// Now show the other information. Non-standard? Yes, but it gets the job done. // Add a period after the tagline when doing so. --PS 2010-07-03

var ss = window.document.getElementById("siteSub"); if(!ss) { ss = window.document.createElement("div"); ss.id = "siteSub"; ss.innerHTML = "From Wikipedia, the free encyclopedia"; var bc = window.document.getElementById("bodyContent"); bc.insertBefore(ss, bc.firstChild); }           ss.innerHTML = ' ' + statusText + ' ' + ss.innerHTML + '.'; };           x.send(null); }); }); }