User:小躍/test3.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
var IAMnum=0;
var IAMgroup1= mw.util.addPortletLink(
    'p-navigation',
    '#',
    '不活躍巡查員',
    null,
    '列出不活躍巡查成員',
    null,
    null
);
var IAMgroup2= mw.util.addPortletLink(
    'p-navigation',
    '#',
    '不活躍回退員',
    null,
    '列出不活躍回退成員',
    null,
    null
);
var IAMgroup3= mw.util.addPortletLink(
    'p-navigation',
    '#',
    '不活躍IP查封豁免員',
    null,
    '列出不活躍IP查封豁免權成員',
    null,
    null
);
var IAMgroup4= mw.util.addPortletLink(
    'p-navigation',
    '#',
    '不活躍巡查豁免員',
    null,
    '列出不活躍巡查豁免權成員',
    null,
    null
);
var IAMgroup5= mw.util.addPortletLink(
    'p-navigation',
    '#',
    '不活躍機器人',
    null,
    '列出不活躍機器人成員',
    null,
    null
);

mw.loader.using(['oojs-ui','mediawiki.util']).done( function () {
$( IAMgroup1 ).click( function () {
	IAMnum=1;
	var doThis = confirm('想運行此腳本嗎?');
	if( doThis ){
		$.getJSON(
			//Get userlist
            mw.util.wikiScript('api'),
            {
                format: 'json',
                action: 'query',
                list: 'allusers',
                augroup: 'patroller',
                aulimit: 1000
            }
        ).done( function ( data ) {
            try {
            	var users = data.query.allusers;
            	var userlist = [];
            	users.forEach(function(object){
            		userlist.push(object.name);
            	});
            	filterUsers(userlist);
            }
        catch ( e ) {
            console.log( "Content request error: " + e.message );
            console.log( "Content request response: " + JSON.stringify( data ) );
        }
    } ).fail( function () {
        console.log( "While getting the userlist, there was an AJAX error." );
    } );
}
} );
$( IAMgroup2 ).click( function () {
	IAMnum=2;
	var doThis = confirm('想運行此腳本嗎?');
	if( doThis ){
		$.getJSON(
			//Get userlist
            mw.util.wikiScript('api'),
            {
                format: 'json',
                action: 'query',
                list: 'allusers',
                augroup: 'rollbacker',
                aulimit: 1000
            }
        ).done( function ( data ) {
            try {
            	var users = data.query.allusers;
            	var userlist = [];
            	users.forEach(function(object){
            		userlist.push(object.name);
            	});
            	filterUsers(userlist);
            }
        catch ( e ) {
            console.log( "Content request error: " + e.message );
            console.log( "Content request response: " + JSON.stringify( data ) );
        }
    } ).fail( function () {
        console.log( "While getting the userlist, there was an AJAX error." );
    } );
}
} );
$( IAMgroup3 ).click( function () {
	IAMnum=3;
	var doThis = confirm('想運行此腳本嗎?');
	if( doThis ){
		$.getJSON(
			//Get userlist
            mw.util.wikiScript('api'),
            {
                format: 'json',
                action: 'query',
                list: 'allusers',
                augroup:'ipblock-exempt',
                aulimit: 1000
            }
        ).done( function ( data ) {
            try {
            	var users = data.query.allusers;
            	var userlist = [];
            	users.forEach(function(object){
            		userlist.push(object.name);
            	});
            	filterUsers(userlist);
            }
        catch ( e ) {
            console.log( "Content request error: " + e.message );
            console.log( "Content request response: " + JSON.stringify( data ) );
        }
    } ).fail( function () {
        console.log( "While getting the userlist, there was an AJAX error." );
    } );
}
} );
$( IAMgroup4 ).click( function () {
	IAMnum=4;
	var doThis = confirm('想運行此腳本嗎?');
	if( doThis ){
		$.getJSON(
			//Get userlist
            mw.util.wikiScript('api'),
            {
                format: 'json',
                action: 'query',
                list: 'allusers',
                augroup:'autoreviewer',
                aulimit: 1000
            }
        ).done( function ( data ) {
            try {
            	var users = data.query.allusers;
            	var userlist = [];
            	users.forEach(function(object){
            		userlist.push(object.name);
            	});
            	filterUsers(userlist);
            }
        catch ( e ) {
            console.log( "Content request error: " + e.message );
            console.log( "Content request response: " + JSON.stringify( data ) );
        }
    } ).fail( function () {
        console.log( "While getting the userlist, there was an AJAX error." );
    } );
}
} );
$( IAMgroup5 ).click( function () {
	IAMnum=5;
	var doThis = confirm('想運行此腳本嗎?');
	if( doThis ){
		$.getJSON(
			//Get userlist
            mw.util.wikiScript('api'),
            {
                format: 'json',
                action: 'query',
                list: 'allusers',
                augroup:'bot',
                aulimit: 1000
            }
        ).done( function ( data ) {
            try {
            	var users = data.query.allusers;
            	var userlist = [];
            	users.forEach(function(object){
            		userlist.push(object.name);
            	});
            	filterUsers(userlist);
            }
        catch ( e ) {
            console.log( "Content request error: " + e.message );
            console.log( "Content request response: " + JSON.stringify( data ) );
        }
    } ).fail( function () {
        console.log( "While getting the userlist, there was an AJAX error." );
    } );
}
} );

function filterUsers (userlist){
	var userstring = userlist.toString();
	var reducedList = userstring.split(",");
	var tribsData;
	var logsData;
	//Visual output onto the page you activate the script on
	$("#mw-content-text").replaceWith("The following users are inactive:" + "<ul id=\"inactiveList\" style=\"list-style-type:none;list-style-image:none;\"></ul>");
	for (x = 0; x < reducedList.length; x++){
		$.getJSON(
		//Get contribs and log entries
            mw.util.wikiScript('api'),
            {
                format: 'json',
                action: 'query',
                list: 'logevents|usercontribs',
                leprop: 'timestamp',
                ledir: 'older',
                leuser: reducedList[x],
                lelimit: 1, //We only need the most recent log action/edit
                uclimit: 1,
                ucuser: reducedList[x],
                ucdir: 'older',
                ucprop: 'timestamp|ids'
            }
        ).done( function ( data ) {
            try {
            	tribsData = data.query.usercontribs;
            	logsData = data.query.logevents;
            	var activeLogs, activeTribs, active;
            	var tribsInfo;

            	if(typeof(logsData[0].timestamp) != "undefined") {
            		activeLogs = compareDates( logsData[0].timestamp, "logs" );
            	}
            	else {
            		activeLogs = false;
            	}
            	if( typeof(tribsData[0]) != "undefined" ) {
            		tribsInfo = (tribsData[0].revid+"|"+tribsData[0].timestamp);
            		activeTribs = compareDates( tribsInfo, "tribs" );
            	}
            	else {
            		activeTribs = false;
            	}
            	if( activeLogs === false && activeTribs === false ) {
            		var user = tribsData[0].user;
            		console.log(user + " is inactive");
            		listInactiveUsers( user, tribsData, logsData );
            	}
            }
        catch ( e ) {
            console.log( "Content request error: " + e.message );
            console.log( "Content request response: " + JSON.stringify( data ) );
        }
    } ).fail(/*console.log( "While getting the userlist, there was an AJAX error." )*/);
	}
	var updButton = new OO.ui.ButtonWidget( {
		label: '更新不活躍人員清單?',
		id: 'upd-inactive-bttn'
	} );
	$("#inactiveList").after(updButton.$element.click(function(){updList();}));
}
function compareDates ( data, dataType ){
	//Gets current date in yyyymmdd
	var today = new Date();
	var dd = today.getDate();
	var mm = today.getMonth() + 1; //January is 0!
	var yyyy = today.getFullYear();
	//Set back 6 months
	mm -= 6;
	if (mm < 0){
		mm += 12;
		yyyy -= 1;
	}
	
	if(dd<10) {
    	dd='0'+dd;
	} 

	if(mm<10) {
	    mm='0'+mm;
	} 
	today = ''+yyyy+mm+dd; //This is a string

	var date;
	var isActive;
	if( dataType === "logs" ){
		date = data.slice(0,data.indexOf('T'));
		date = date.replace("-","");
	}
	else if( dataType === "tribs" ){
		date = data.slice(data.indexOf('|') + 1, data.indexOf('T'));
		date = date.replace("-","");
	}
	if (date < today){
		isActive = false;
	}
	else{
		isActive = true;
	}
	return isActive;
}

function listInactiveUsers( userName, tribsArray, logsArray ){
	var userLink = "[[User:<a href=\"https://zh.wikipedia.org/wiki/Special:Contribs/" + userName + "\">" + userName + "</a>|" + userName + "]]";
	var tribsInfo = tribsArray[0].timestamp;
	tribsInfo = tribsInfo.slice(0, tribsInfo.indexOf("T"));
	tribsInfo = tribsArray[0].revid + "|" + tribsInfo;
	var logsInfo = logsArray[0].timestamp;
	logsInfo = logsInfo.slice(0, logsInfo.indexOf("T"));
	tribsInfo = "[[Special:PermanentLink/" + tribsInfo + "]]";
	logsInfo = "[[Special:Log/" + userName + "|" + logsInfo + "]]";
	$("#inactiveList").append( "<li>" + userLink + " || " + tribsInfo + " || " + logsInfo + "</li>" );
}


function updList() {
	var list = $("#inactiveList")[0].innerText;
	list = '|-\n|' + list;
	while(list.indexOf("\n[[User:") > 0){
		list = list.substr(0,list.indexOf("\n[[User:")) + '\n|-\n|' + list.substr(list.indexOf("\n[[User:") + 1);
	}
	$.getJSON(
		mw.util.wikiScript('api'),
            {
                format: 'json',
                action: 'query',
                pageids: 6067711,
                prop: 'revisions',
                rvprop: 'content',
                rvsection:IAMnum.toString(),
            }
        ).done( function ( data ) {
            try {
            	var wtext = data.query.pages[6067711].revisions[0]['*'];
            	wtext = wtext.substr(0, wtext.indexOf("|-")) + list +"\n"+wtext.substr(wtext.indexOf("|}"));
            	$.ajax( {
        			url: mw.util.wikiScript( 'api' ),
			        type: 'POST',
			        dataType: 'json',
			        data: {
			            format: 'json',
			            action: 'edit',
			            title: 'User:小躍/Sandbox/Inactive members of right',
			            section:IAMnum.toString(),
			            text: wtext,
			            summary: '輔助更新清單',
			            token: mw.user.tokens.get( 'csrfToken' ),
			        }
    			} ).done(function() {
    				alert( "已更新 User:小躍/Sandbox/Inactive members of right" );
    				var reload = new OO.ui.ButtonWidget({ label: '重新讀取原頁面' });
    				$('#upd-inactive-bttn').replaceWith(reload.$element.click(function(){window.location.reload();}));
    			}
    			).fail( function ( e, data ){
    				console.log( e.message );
    				console.log( JSON.stringify( data ) );
    			});
            }
            catch(e){}
        }).fail( function ( e, data ){
    		console.log( e.message );
    		console.log( JSON.stringify( data ) );
    });
}
});