User:94rain/Gadget-OnlineAdmin.js

/** * Original author: User:Alexander Misel/admin.js, modified by Revival of the Evil Spirit. * Taken from oldid=45739798 * * Modifications: * 1. Moved the menu to "More" instead of to the left of the username * 2. Fixed bugs * 3. Support for both traditional and simplified Chinese * 4. Execute after DOM is ready */

(function($, mw) {   $(function { var BLACKLIST = [];

// Create portlet link var portletLinkOnline = mw.util.addPortletLink(               'p-cactions',                '#',                'Online Administrators');

var rcstart, rcend, time; var users = []; var admins = [], interfaceadmins = [], bureaucrats = [], oversighters = [], checkusers = []; stewards = []; var api = new mw.Api;

// Bind click handler $(portletLinkOnline).find('a').click(function(e) {           e.preventDefault;

users = []; var usersExt = []; admins = []; interfaceadmins = []; bureaucrats = [], oversighters = [], checkusers = []; stewards = [];

// Get users who edited in the last 30 minutes time = new Date; rcstart = time.toISOString; time.setMinutes(time.getMinutes - 30); rcend = time.toISOString;

// API:RecentChanges api.get({               format: 'json',                action: 'query',                list: 'recentchanges',                rcprop: 'user',                rcstart: rcstart,                rcend: rcend,                rcshow: '!bot|!anon',                rclimit: 500            }).done(function(data) {                $.each(data.query.recentchanges, function(i, item) { users[i] = item.user; });               api.get({ format: 'json', action: 'query', list: 'logevents', leprop: 'user', lestart: rcstart, leend: rcend, lelimit: 500 }).done(function(data) { $.each(data.query.logevents, function(i, item) {                       usersExt[i] = item.user;                    });

Array.prototype.push.apply(users, usersExt);

// Remove duplicates and sort user names users = $.unique(users.sort);

var promises = []; var mark = function(data) { $.each(data.query.users, function(i, user) {                           // Find administrators and exclude adminbot                            if ($.inArray('bot', user.groups) === -1 && $.inArray(user.name, BLACKLIST)) {                                if ($.inArray('sysop', user.groups) > -1) {                                    admins[i] = user.name;                                }                                if ($.inArray('interface-admin', user.groups) > -1) {                                    interfaceadmins[i] = user.name;                                }                                if ($.inArray('bureaucrat', user.groups) > -1) {                                    bureaucrats[i] = user.name;                                }                                // Oversight permission is 'suppress', but users with this permission are called oversighters                                if ($.inArray('suppress', user.groups) > -1) { oversighters[i] = user.name; }                               if ($.inArray('checkuser', user.groups) > -1) { checkusers[i] = user.name; }                               if ($.inArray('steward', user.groups) > -1) { stewards[i] = user.name; }                           }                        });                    };                    for (var i = 0; i < (users.length + 50) / 50; i++) {                        promises.push(api.get({                                format: 'json',                                action: 'query',                                list: 'users',                                ususers: users.slice(i * 50, (i + 1) * 50).join('|'),                                usprop: 'groups'                            }).done(mark));                    }

// Query user permissions $.when.apply($, promises).done(function {                       // Remove empty values                        var filter = function(n) {                            return n;                        };

admins = admins.filter(filter); interfaceadmins = interfaceadmins.filter(filter); bureaucrats = bureaucrats.filter(filter); oversighters = oversighters.filter(filter); checkusers = checkusers.filter(filter); var userlink = function(user) { var user2 = user.replace(/&/g, '&amp;').replace(//g, '&lt;'); return '' + user2 + ' (Talk) 　'; };

if (admins.length + interfaceadmins.length + bureaucrats.length + oversighters.length + checkusers.length > 0) { var adminsstring = [' Here are the administrators online in the last 30 minutes: '];

if (admins.length > 0) { adminsstring.push('Administrators (' + admins.length + ' online):'); $.each(admins, function(i, e) {                                   adminsstring.push(userlink(e));                                }); adminsstring.push(' '); }

if (interfaceadmins.length > 0) { adminsstring.push('Interface Administrators (' + interfaceadmins.length + ' online):'); $.each(interfaceadmins, function(i, e) {                                   adminsstring.push(userlink(e));                                }); adminsstring.push(' '); }

if (bureaucrats.length > 0) { adminsstring.push('Bureaucrats (' + bureaucrats.length + ' online):'); $.each(bureaucrats, function(i, e) {                                   adminsstring.push(userlink(e));                                }); adminsstring.push(' '); }

if (oversighters.length > 0) { adminsstring.push('Oversighters (' + oversighters.length + ' online):'); $.each(oversighters, function(i, e) {                                   adminsstring.push(userlink(e));                                }); adminsstring.push(' '); }

if (checkusers.length > 0) { adminsstring.push('Check Users (' + checkusers.length + ' online):'); $.each(checkusers, function(i, e) {                                   adminsstring.push(userlink(e));                                }); adminsstring.push(' '); }

mw.notify($(adminsstring.join(''))); } else { mw.notify('There are no administrators currently online.'); }                   }).fail(function { mw.notify('An error occurred while querying. Please try again later.'); });               });            });        });    }); })(jQuery, mw);