User:94rain/Gadget-OnlineAdmin.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * 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;').replace(/>/g, '&lt;');
                            return '<a href="/wiki/User:' + user2 + '" target="_blank">' + user2 + '</a>&nbsp;<small style="opacity:.75;">(<a href="/wiki/User talk:' + user2 + '" target="_blank">Talk</a>)</small> ';
                        };

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

                            if (admins.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">Administrators (' + admins.length + ' online):');
                                $.each(admins, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            if (interfaceadmins.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">Interface Administrators (' + interfaceadmins.length + ' online):');
                                $.each(interfaceadmins, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            if (bureaucrats.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">Bureaucrats (' + bureaucrats.length + ' online):');
                                $.each(bureaucrats, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            if (oversighters.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">Oversighters (' + oversighters.length + ' online):');
                                $.each(oversighters, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            if (checkusers.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">Check Users (' + checkusers.length + ' online):');
                                $.each(checkusers, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            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);