User:Erwin/stewardlinks.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Steward toolbox
 * Adds a toolbox in the left sidebar:
 * - Rights: lists your rights
 * - Users per group: Uses Ajax to get the number of sysops etc.
 * - Links to tools
 * @author [[m:User:Erwin]] <https://meta.wikimedia.org/wiki/User:Erwin>
 * @copyright Erwin 2009-2010; Other maintainers 2010 - present
 * @license GFDL-1.2-or-later AND CC-BY-SA-3.0
 */
 
function loadGroups() {
    document.getElementById('n-groups').innerHTML = 'Loading…';
    var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=parse&text={{NUMBERINGROUP:sysop}};{{NUMBERINGROUP:interface-admin}};{{NUMBERINGROUP:bureaucrat}};{{NUMBERINGROUP:checkuser}};{{NUMBERINGROUP:suppress}};{{NUMBERINGROUP:bot}};';
    var request = new XMLHttpRequest();
    request.open('GET', url, true);
    request.onreadystatechange = function () {
        if(request.readyState==4) {
            parseGroups(request);
        }
    };
    request.setRequestHeader('Pragma', 'cache=yes');
    request.setRequestHeader('Cache-Control', 'no-transform');
    request.send(null);
}
 
function parseGroups(request) {
    var xml = request.responseXML;
    if ( xml !== null ) {
        rtext = xml.getElementsByTagName('text')[0].childNodes[0].nodeValue;
        text = '';
 
        var valueRe = RegExp ('([0-9,]+);', 'g');
        try {
            text += ' <a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:ListUsers&group=sysop">a = ' + valueRe.exec(rtext)[1] + '</a>;';
            text += ' <a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:ListUsers&group=interface-admin">ia = ' + valueRe.exec(rtext)[1] + '</a>;';
            text += ' <a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:ListUsers&group=bureaucrat">b = ' + valueRe.exec(rtext)[1] + '</a>;';
            text += ' <a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:ListUsers&group=checkuser">c = ' + valueRe.exec(rtext)[1] + '</a>;';
            text += ' <a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:ListUsers&group=suppress">o = ' + valueRe.exec(rtext)[1] + '</a>;';
            text += ' <a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:ListUsers&group=bot">bot = ' + valueRe.exec(rtext)[1] + '</a>.';
        } catch (someError) {
            text = 'Regex error (<a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:Listusers">User list</a>)';
        }
    } else {
        text = 'AJAX error (<a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:Listusers">User list</a>)';
    }
    document.getElementById('n-groups').innerHTML = text;
}    
 
function stewardLinks() {
    // Add toolbox underneath p-navigation. 
    if (document.getElementById('p-navigation') === null && document.getElementById('p-Navigation') === null) {
        // p-navigation not found.
        console.log('no p-navigation or p-Navigation ID on this wiki');
        return false;
    }

    prevNode = document.getElementById('p-navigation');
    if (prevNode === null) {
       prevNode = document.getElementById('p-Navigation');
    }
    
    // Clone previous node including child nodes.
    stewardToolbox = prevNode.cloneNode(true);
    stewardToolbox.id = 'p-steward';

    // Set header of new toolbox
    h = stewardToolbox.getElementsByTagName('h5');
    if(h[0]) {
        h[0].innerHTML = 'Steward toolbox';
    }

    // The links are included in the first and only div of the toolbox
    d = stewardToolbox.getElementsByTagName('div');
    if(d[0]) {
        d = d[0];
    } else {
        return false;
    }

    // Remove current children, i.e. current ul element.
    while(d.hasChildNodes()) {
        d.removeChild(d.childNodes[0]);
    }

    // Create new list with links
    ul = document.createElement('ul');
    
    // Determine user groups.
    var groups = '';
    if (mw.config.get('wgUserGroups')) {
        for (var i = 0; i < mw.config.get('wgUserGroups').length; i++) {
            if (mw.config.get('wgUserGroups')[i] != "*" && mw.config.get('wgUserGroups')[i] != "user" && mw.config.get('wgUserGroups')[i] != "autoconfirmed" ) { //Exclude standard rights
                groups += ", " + mw.config.get('wgUserGroups')[i];
            }
        }
    }
 
    if (groups) {
        groups = groups.substr(2);
    } else {
        groups = 'none';
    }
    contents = '<li id="n-rights"><a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special%3AListUsers&username=' + mw.config.get('wgUserName') + '&limit=1">Rights</a>: ' + groups + ' (<a href="//meta.wikimedia.org/wiki/Special:Userrights/' + mw.config.get('wgUserName') + '@' + mw.config.get('wgDBname') + '">e</a>)</li>';
    contents += '<li id="n-recentchanges"><a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:RecentChanges">Recent changes</a></li>';
    contents += '<li id="n-groups"><a href="javascript:loadGroups()">Users per group</a></li>';
    contents += '<li id="n-stewardry"><a href="//meta.toolforge.org/stewardry/' + mw.config.get('wgDBname') + '">Stewardry</a></li>';
    contents += '<li id="n-checkuser"><a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:CheckUser">CheckUser</a> (<a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:CheckUserLog">log</a>)</li>';
    contents += '<li id="n-investigate"><a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:Investigate">Investigate</a></li>';
    contents += '<li id="n-suppres"><a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:Log&type=suppress">Suppression log</a></li>';
    contents += '<li id="n-afprivdet"><a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:Log&type=abusefilterprivatedetails">A.F. private log</a></li>'; 
    if (mw.config.get('wgCurRevisionId')) {
        contents += '<li id="n-oldid">(oldid=' + mw.config.get('wgCurRevisionId') + ')</li>';
    }

    ul.innerHTML = contents;
    d.appendChild(ul);

    // Insert new node
    prevNode.parentNode.insertBefore(stewardToolbox, prevNode.nextSibling);
}
 
// OnloadHook
if (mw.config.get('skin') != 'cologneblue' && mw.config.get('skin') != 'nostalgia' && mw.config.get('skin') != 'standard') {
	$( stewardLinks );
}

/**
 * End steward toolbox
 */