User:Bencmq/script/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

Modified based on [[User:Erwin/stewardlinks.js]]
 */
 
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|R}};{{NUMBERINGROUP:bureaucrat|R}};{{NUMBERINGROUP:checkuser|R}};{{NUMBERINGROUP:oversight|R}};';
    var request = sajax_init_object() ;
    if (request == null) {
        return null;
    }    
    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/sysop">s = ' + valueRe.exec(rtext)[1] + '</a>;';
            text += ' <a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:Listusers/bureaucrat">b = ' + valueRe.exec(rtext)[1] + '</a>;';
            text += ' <a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:Listusers/checkuser">c = ' + valueRe.exec(rtext)[1] + '</a>;';
            text += ' <a href="' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special:Listusers/oversight">o = ' + 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-Navigacion') == null && document.getElementById('p-Navigation') == null && document.getElementById('p-support') == null && !navigationPoint ) {
        // p-navigation not found.
        return false;
    }
    if (typeof navigation !== "undefined" && navigationPoint) {
         prevNode = navigationPoint;
    } else {
    
        prevNode = document.getElementById('p-navigation');
        if (prevNode==null) {prevNode = document.getElementById('p-Navigation');}
        if (prevNode==null) {prevNode = document.getElementById('p-support');}
        if (prevNode==null) {prevNode = document.getElementById('p-Navigacion');}

    }
    // 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 (wgUserGroups) {
        for (var i = 0; i < wgUserGroups.length; i++) {
            if (wgUserGroups[i] != "*" && wgUserGroups[i] != "user" && wgUserGroups[i] != "autoconfirmed" ) { //Exclude standard rights
                groups += ", " + 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=' + wgUserName + '&limit=1">Rights</a>: ' + groups + ' (<a href="//meta.wikimedia.org/wiki/Special:Userrights/' + wgUserName + '@' + 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="//toolserver.org/~pathoschild/stewardry/?wiki=' + 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></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>';
    if (wgCurRevisionId) {
        contents += '<li id="n-oldid">(oldid=' + wgCurRevisionId + ')</li>';
    }
 
    ul.innerHTML = contents;
    d.appendChild(ul);
 
    // Insert new node
    prevNode.parentNode.insertBefore(stewardToolbox, prevNode.nextSibling);
}
 
// OnloadHook
$(document).ready( function () {
    if (skin != 'cologneblue' && skin != 'nostalgia' && skin != 'standard') {
        stewardLinks();
    }
});
 
/**
 * End steward toolbox
 */