MediaWiki:ShowUserGroups.js

/* ShowUserGroups - list all groups a user is in on their user page */

/*jslint browser */ /*global window, jQuery, mediaWiki */

(function ($, mw) {   "use strict";

// double-run protection if (window.loadedShowUserGroups) { return; }   window.loadedShowUserGroups = true;

// translations var lang = mw.config.get("wgUserLanguage"); var msg = { en: { label: "User groups: ", nogroups: "(none)" }   };    // use user language, with English as fallback msg = msg[lang] || msg[lang.split("-")[0]] || msg.en;

var inUserNamespace = [2, 3, 1200].indexOf(mw.config.get("wgNamespaceNumber")) !== -1; var isOasis = mw.config.get("skin") === "oasis";

function addGroupsToPage(data) { var groups = data.query.users[0].groups; var $content = isOasis ? $("") : $(" ").css({               cssFloat: document.dir === "rtl" ? "left" : "right",                fontSize: "small"            });

if (!groups) { // likely a not-existing or invalid username return; }

// remove '*' and 'user' groups groups.splice(groups.indexOf("*"), 1); groups.splice(groups.indexOf("user"), 1);

// stringify groups = groups.join(", ") || msg.nogroups;

// add groups + label to newly created element, then add to DOM $content.text(groups).prepend(           $(" ").css("fontWeight", "bold").text(msg.label)        ); $(".masthead-info > div > .details, .firstHeading").append($content); }

function getUserNameFromPage { var userName;

if (isOasis) { // for oasis skin, run on all pages with the user masthead userName = $(".masthead-info > hgroup > h1").text; } else if (inUserNamespace) { // wgRelevantUserName available in MW 1.23+ - fallback to wgTitle when unavailable userName = mw.config.get("wgRelevantUserName", mw.config.get("wgTitle").split("/")[0]); }

// ignore anon user pages if (mw.util.isIPv4Address(userName) || mw.util.isIPv6Address(userName)) { userName = null; }

return userName || null; }

function main { var userName = getUserNameFromPage;

if (userName) { $.get(mw.util.wikiScript("api"), {               action: "query",                format: "json",                list: "users",                ususers: userName,                usprop: "groups"            }).done(addGroupsToPage); }   }

mw.loader.using(["mediawiki.util"], function {        $(main);    });

}(jQuery, mediaWiki));