User:Bencmq/script/CheckUser.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// <nowiki>
// see User:Bencmq/script/CheckUser
$(function(){
    if (mw.config.get( 'wgPageName' ).indexOf("Special:用户查核") === 0) {
 
        // prevent checks without a given reason
 
        $('#checkuserform').submit(function(e) {
            if ($.trim($('#checkreason').val()) === "") {
                alert("请填写查核理由");
                e.preventDefault();
            }
        });
 
        // add 3 buttons to the checkuser interface
        $('#checkuserform > fieldset').append(
                $("<input>", {
                    type: "button",
                    value: "WP:CUP#1 已封禁用户",
                    click: function() {
                        reason = $('#checkreason').val();
                        $('#checkreason').val("[[WP:CUP#1]]: " + reason);
                    }
                })
            );
        $('#checkuserform > fieldset').append(
                $("<input>", {
                    type: "button",
                    value: "WP:CUP#2 检查封禁影响",
                    click: function() {
                        reason = $('#checkreason').val();
                        $('#checkreason').val("[[WP:CUP#2]]: " + reason);
                    }
                })
            );
        $('#checkuserform > fieldset').append(
                $("<input>", {
                    type: "button",
                    value: "WP:CUP#3 IPBE授权",
                    click: function() {
                        reason = $('#checkreason').val();
                        $('#checkreason').val("[[WP:CUP#3]]: " + reason);
                    }
                })
            );
 
        // and one more
        $('#checkuserform > fieldset').append(
            $("<input>", {
                type: "button",
                value: "理由同前",
                click: function() {
                    $('#checkreason').val("同前");
                }
            })
            );
 
        if ($('#usetag').length > 0 && Array.includes(mw.config.get( 'wgGlobalGroups' ), "steward") ) {
            // usetag indicates it's on list user mode
     
            var mytoken;
     
            // get token if not present, then lock account
            function performLock(username, reason) {
                if (mytoken) {
                    lock(username, reason, mytoken);
                } else {
                    $.ajax({
                        type: 'GET',
                        url: '//meta.wikimedia.org/w/api.php',
                        data: {
                            action: "tokens",
                            type: "setglobalaccountstatus",
                            format: "json",
                            origin: window.location.protocol+ '//zh.wikipedia.org'
                        },
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function(data) {
                            if (data.tokens) {
                                mytoken = data.tokens.setglobalaccountstatustoken
                                lock(username, reason, mytoken);
                            }
                        }
                    })
                }
            }
     
     
            $('#checkuserresults').find('.mw-userlink').each(
                function() {
                    var username = $(this).text().replace(/ /g, '_');
                    var lockLTA = $('<a href="#">LTA </a>').click(function(){
                        performLock(username, 'Long-term abuse');
                        return false;
                    });
                    var lockSpam = $('<a href="#">SPAM </a>').click(function(){
                        performLock(username, 'Spam-only account');
                        return false;
                    });
                    var lockSpambot = $('<a href="#">BOT </a>').click(function(){
                        performLock(username, 'Spam-only account: spambot');
                        return false;
                    });
                    var wrapper = $('<span class="quick-lock>').attr('id', username).append(lockLTA).append(lockSpam).append(lockSpambot);
                    $(this).before(wrapper);
                }
            );
     
            function lock(username, reason, token) {
                $.ajax({
                    type: 'POST',
                    url: '//meta.wikimedia.org/w/api.php',
                    data: {
                        action: "setglobalaccountstatus",
                        format: "json",
                        user: username,
                        locked: 'lock',
                        reason: reason,
                        token: token,
                        origin: window.location.protocol+ '//zh.wikipedia.org'
                    },
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function(data) {
                        console.log("Locking User:" + username);
                        if (data.setglobalaccountstatus) {
                            $('#checkuserresults').find('#'+data.setglobalaccountstatus.user.replace(/ /g, '_')).text("Locked ");
                        } else {
                            alert('Error when locking ' + username);
                            console.log(data);
                        }
                    }
                })
            }
        }
    } else if (mw.config.get( 'wgPageName' ) === "Wikipedia:用戶查核請求") {
        // add permalink into the reason field
 
        var oldid = mw.config.get('wgCurRevisionId');
        $("h3:has(.mw-headline)").each(function() {
            var section = $('.mw-headline', $(this)).attr('id');
            $(".culink > a",$(this).next().next()).each(function(){
                this.href+= "&reason=[[Special:PermaLink/" + oldid +"%23" +section + "]]";
            });
        });
    }
});
 
$(function(){
    if (mw.config.get('wgPageName').indexOf("Special:用户查核日志") !==0) return;
 
    var stats = $('<input/>').attr({type:'button', value:'   Go!   '}).click(calcStats);
    $('#mw-content-text fieldset').append($('<p/>'));
    $('#mw-content-text fieldset p:last').append('使用上方“截止年月”作为时间统计当月操作数量').append(stats);
    $('#mw-content-text fieldset').append($('<textarea id="stats" style="display:none"></textarea>')).append($('<p id="loading">Loading...</p>').hide());
    function calcStats() {
        $('#loading').show();
        var totalReturn = 0;
        $.ajax({
            type: "GET",
            url: mw.util.wikiScript('api'),
            data: {
                action: 'query',
                list: 'allusers',
                format: 'json',
                augroup: 'checkuser',
                aulimit: '10'
            },
            dataType: 'json',
            success: function(data) {
                totalReturn = data.query.allusers.length;
                for (i=0;i<data.query.allusers.length;i++) {
                    getLog(data.query.allusers[i].name);
                }
            }
        });
        var today = new Date();
        var year = $('#year').val();
        year = (year === "") ? (today.getFullYear()) : year;
        var month = $('#month').val();
        month = (month === "-1") ? (today.getMonth() + 1) : month;
        var lastday = new Date(year, month, 0).getDate();
        month = (month < 10)? "0" + month : month.toString();
        var startOfMonth = year.toString() + month + "01000000";
        var endOfMonth = year.toString() + month + lastday + "235959";
 
        function getLog(username) {
            $.ajax({
                type:'GET',
                url: mw.util.wikiScript('api'),
                data: {
                    action: 'query',
                    list: 'checkuserlog',
                    format: 'json',
                    culuser: username,
                    cullimit: 5000,
                    culdir: 'newer',
                    culfrom: startOfMonth,
                    culto: endOfMonth
                },
                dataType: 'json',
                success: function(data) {
                    processLog(data.query.checkuserlog.entries, username);
                }
            })
        }
 
        var sum =[0, 0, 0, 0, 0];
        var requestReturn = 0;
        var wikitext = "{| class='wikitable' style='text-align: center;'\n|-\n! scope='col' width='120px' |查核员 \n! scope='col' width='100px' | [[WP:RFCU]] \n! scope='col' width='100px' | [[WP:CUP#1]]\n! scope='col' width='100px' | [[WP:CUP#2]] \n! scope='col' width='100px' | [[WP:CUP#3]] \n! scope='col' width='100px' | 其他 \n! scope='col' width='100px' | 总数\n";
        var wikitextarray = [];
        function processLog(entries, username) {
            function test(comment) {
                        var flag = false;
                        var catIndex = 0;
                        while (!flag && catIndex < 5) {
                            flag = category[catIndex].test(comment);
                            catIndex ++;
                        }
                        return (flag)? catIndex-1 : catIndex;
                    }
            requestReturn++;
            var count = [0,0,0,0,0,0];
            var category =[/同前/, /CUP#1/, /CUP#2/, /CUP#3/, /Special:PermaLink/i];
 
            for (var i = 0; i < entries.length; i++) {
                var cat = test(entries[i].reason);
                if (cat === 0) {
                    count[count[0]]++;
                    sum[count[0]-1]++;
                } else {
                    count[cat]++
                    sum[cat - 1]++;
                    count[0] = cat;
                }
                if (cat === 5) {
                    console.log(entries[i].timestamp, entries[i].checkuser, entries[i].target, entries[i].reason );
                }
            }
            wikitextarray[requestReturn-1]= "|-\n| [[User:"+username+"|"+username+"]] || " + count[4] + " || " + count[1] + " || " + count[2] + " || " + count[3] + " || " + count[5]  + " || '''" + (count[1]+count[2]+count[3]+count[4]+count[5])+"'''\n"
            if (totalReturn === requestReturn) {
                cleanup();
            }
        }
 
        function cleanup() {
            wikitextarray.sort();
            for (var i = 0; i < wikitextarray.length; i++) {
                wikitext+=wikitextarray[i];
            }
            wikitext += "|-\n| 总数 || '''" + sum[3] + "''' || '''" + sum[0] + "''' || '''" + sum[1] + "''' || '''" + sum[2] + "''' || '''" + sum[4] + "''' || '''" + (sum[1]+sum[2]+sum[3]+sum[4]+sum[0]) + "'''\n|}";
            $('#loading').hide();
            $('#stats').show().val(wikitext).height('220px').width('45%');
            if (sum[4] > 0) alert("有理由无法归类的日志项,查看控制台输出。");
        }
    }
});
// </nowiki>