User:94rain/Gadget-OnlineAdmins.js

(function($, mw) {   // Create portlet link    var portletLinkOnline = mw.util.addPortletLink( 'p-cactions', '#',       wgULS('Online administrators', 'Online administrators') );

var rcstart, rcend, time; var users = []; var admins = [], iadmins = [], bureaucrats = [], translationadmins = []; var api = new mw.Api;

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

users = []; var usersExt = []; admins = []; iadmins = []; bureaucrats = []; translationadmins = [];

// 最近更改30分钟内的编辑用户 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);

// 使用者名稱去重與分割 users = $.unique(users.sort);

var promises = []; var mark = function(data) { $.each(data.query.users, function(i, user) {                       // 找到管理员，去除adminbot                        if ($.inArray('bot', user.groups) === -1) {                            if ($.inArray('sysop', user.groups) > -1) {                                admins[i] = user.name;                            }                            if ($.inArray('interface-admin', user.groups) > -1) {                                iadmins[i] = user.name;                            }                            if ($.inArray('bureaucrat', user.groups) > -1) {                                bureaucrats[i] = user.name;                            }                            if ($.inArray('translationadmin', user.groups) > -1) {                                translationadmins[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)); }

// 查询用户权限 $.when.apply($, promises).done(function {                    // 消除空值                    var filter = function(n) {                        return n;                    };                    admins = admins.filter(filter);                    iadmins = iadmins.filter(filter);                    bureaucrats = bureaucrats.filter(filter);                    translationadmins = translationadmins.filter(filter);                    var userlink = function(user) {                        var user2 = user.replace(/&/g, '&amp;').replace(//g, '&lt;');                        return '' + user2 + ' (留言) 　';                    }                    if (admins.length + iadmins.length + bureaucrats.length + translationadmins.length > 0) { var adminsstring = [wgULS(' Online administrators ', ' Online administrators ')];

if (admins.length > 0) { adminsstring.push('' + wgULS('sysop', 'sysop') + ' (' + admins.length + wgULS('个在线', '個在線') + ')：'); $.each(admins, function(i, e) {                               adminsstring.push(userlink(e));                            }); adminsstring.push(' '); }                       if (iadmins.length > 0) { adminsstring.push('' + wgULS('interface-admin', 'interface-admin') + ' (' + iadmins.length + wgULS('Online', 'Online') + ')：'); $.each(iadmins, function(i, e) {                               adminsstring.push(userlink(e));                            }); adminsstring.push(' '); }                       if (bureaucrats.length > 0) { adminsstring.push('' + wgULS('bureaucrat', 'bureaucrat') + ' (' + bureaucrats.length + wgULS('Online', 'Online') + ')：'); $.each(Bureaucrats, function(i, e) {                               adminsstring.push(userlink(e));                            }); adminsstring.push(' '); }                       if (translationadmins.length > 0) { adminsstring.push('' + wgULS('translationadmin', 'translationadmin') + ' (' + translationadmins.length + wgULS('Online', 'Online') + ')：'); $.each(translationadmins, function(i, e) {                               adminsstring.push(userlink(e));                            }); adminsstring.push(' '); }

mw.notify($(adminsstring.join(''))); } else { mw.notify(wgULS('目前没有管理人员在线. ', '目前沒有管理人員在線. '));                   }                }).fail(function  { mw.notify(wgULS('查询时发生错误，请稍后重试. ', '查詢時發生錯誤，請稍後重試')); });           });        });    }); })(jQuery, mw);