User:Void/findInactiveSysops.js: Difference between revisions
Content added Content deleted
(proper handling for users with no contribs (shouldn't be admins anyways :P)) |
m (standardize) |
||
Line 17: | Line 17: | ||
$( scriptActivationLink ).click( function () { |
$( scriptActivationLink ).click( function () { |
||
var doThis = confirm('Do you want to run the script?'); |
var doThis = confirm('Do you want to run the script?'); |
||
if( doThis ){ |
if( doThis ) { |
||
$.getJSON( |
$.getJSON( |
||
//Get userlist |
//Get userlist |
||
mw.util.wikiScript('api'), |
mw.util.wikiScript( 'api' ), |
||
{ |
{ |
||
format: 'json', |
format: 'json', |
||
Line 32: | Line 32: | ||
var users = data.query.allusers; |
var users = data.query.allusers; |
||
var userlist = []; |
var userlist = []; |
||
users.forEach(function(object){ |
users.forEach( function( object ) { |
||
userlist.push(object.name); |
userlist.push( object.name ); |
||
}); |
}); |
||
filterUsers(userlist); |
filterUsers( userlist ); |
||
} |
} |
||
catch ( e ) { |
catch ( e ) { |
||
Line 47: | Line 47: | ||
} ); |
} ); |
||
function filterUsers (userlist){ |
function filterUsers ( userlist ) { |
||
var userstring = userlist.toString(); |
var userstring = userlist.toString(); |
||
var exempt = ["John","Pup","Void","NDKilla","Reception123","Revi","Southparkfan","Abuse filter"]; |
var exempt = ["John", "Pup", "Void", "NDKilla", "Reception123", "Revi", "Southparkfan", "Abuse filter"]; |
||
for (i = 0; i < exempt.length; i++){ |
for ( i = 0; i < exempt.length; i++ ) { |
||
userstring = userstring.replace(exempt[i]+",",""); |
userstring = userstring.replace( exempt[i] + ",", "" ); |
||
} |
} |
||
var reducedList = userstring.split(","); |
var reducedList = userstring.split( "," ); |
||
var tribsData; |
var tribsData; |
||
var logsData; |
var logsData; |
||
//Visual output onto the page you activate the script on |
//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>"); |
$( "#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++){ |
for ( x = 0; x < reducedList.length; x++ ) { |
||
checkUser(reducedList[x]); |
checkUser( reducedList[x] ); |
||
} |
} |
||
var updButton = new OO.ui.ButtonWidget( { |
var updButton = new OO.ui.ButtonWidget( { |
||
Line 65: | Line 65: | ||
id: 'upd-inactive-bttn' |
id: 'upd-inactive-bttn' |
||
} ); |
} ); |
||
$("#inactiveList").after(updButton.$element.click(function(){updList();})); |
$("#inactiveList").after( updButton.$element.click( function() { updList(); } ) ); |
||
} |
} |
||
Line 71: | Line 71: | ||
$.getJSON( |
$.getJSON( |
||
//Get contribs and log entries |
//Get contribs and log entries |
||
mw.util.wikiScript('api'), |
mw.util.wikiScript( 'api' ), |
||
{ |
{ |
||
format: 'json', |
format: 'json', |
||
Line 92: | Line 92: | ||
var tribsInfo; |
var tribsInfo; |
||
if(typeof(logsData[0].timestamp) != "undefined") { |
if( typeof( logsData[0].timestamp ) != "undefined" ) { |
||
activeLogs = compareDates( logsData[0].timestamp, "logs" ); |
activeLogs = compareDates( logsData[0].timestamp, "logs" ); |
||
} |
} |
||
Line 98: | Line 98: | ||
activeLogs = false; |
activeLogs = false; |
||
} |
} |
||
if( typeof(tribsData[0]) != "undefined" ) { |
if( typeof( tribsData[0] ) != "undefined" ) { |
||
tribsInfo = |
tribsInfo = tribsData[0].revid + "|" + tribsData[0].timestamp; |
||
activeTribs = compareDates( tribsInfo, "tribs" ); |
activeTribs = compareDates( tribsInfo, "tribs" ); |
||
} |
} |
||
Line 106: | Line 106: | ||
} |
} |
||
if( activeLogs === false && activeTribs === false ) { |
if( activeLogs === false && activeTribs === false ) { |
||
console.log(user + " is inactive"); |
console.log( user + " is inactive" ); |
||
listInactiveUsers( user, tribsData, logsData ); |
listInactiveUsers( user, tribsData, logsData ); |
||
} |
} |
||
Line 117: | Line 117: | ||
} |
} |
||
function compareDates ( data, dataType ){ |
function compareDates ( data, dataType ) { |
||
//Gets current date in yyyymmdd |
//Gets current date in yyyymmdd |
||
var today = new Date(); |
var today = new Date(); |
||
Line 125: | Line 125: | ||
//Set back 3 months |
//Set back 3 months |
||
mm -= 3; |
mm -= 3; |
||
if |
if( mm < 0 ) { |
||
mm += 12; |
mm += 12; |
||
yyyy -= 1; |
yyyy -= 1; |
||
} |
} |
||
if(dd<10) { |
if( dd < 10 ) { |
||
dd='0'+dd; |
dd = '0' + dd; |
||
} |
} |
||
if(mm<10) { |
if( mm<10 ) { |
||
mm='0'+mm; |
mm = '0' + mm; |
||
} |
} |
||
today = ''+yyyy+mm+dd; //This is a string |
today = '' + yyyy + mm + dd; //This is a string |
||
var date; |
var date; |
||
var isActive; |
var isActive; |
||
if( dataType === "logs" ) { |
if( dataType === "logs" ) { |
||
date = data.slice(0,data.indexOf('T')); |
date = data.slice( 0, data.indexOf('T') ); |
||
date = date.replace("-",""); |
date = date.replace( "-", "" ); |
||
} |
} |
||
else if( dataType === "tribs" ) { |
else if( dataType === "tribs" ) { |
||
date = data.slice(data.indexOf('|') + 1, data.indexOf('T')); |
date = data.slice( data.indexOf('|') + 1, data.indexOf('T') ); |
||
date = date.replace("-",""); |
date = date.replace( "-", "" ); |
||
} |
} |
||
if (date < today) { |
if ( date < today ) { |
||
isActive = false; |
isActive = false; |
||
} |
} |
||
Line 161: | Line 161: | ||
var userLink = "[[User:<a href=\"https://publictestwiki.com/wiki/Special:Contribs/" + userName + "\">" + userName + "</a>|" + userName + "]]"; |
var userLink = "[[User:<a href=\"https://publictestwiki.com/wiki/Special:Contribs/" + userName + "\">" + userName + "</a>|" + userName + "]]"; |
||
var tribsInfo; |
var tribsInfo; |
||
if(tribsArray[0] !== undefined) { |
if( tribsArray[0] !== undefined ) { |
||
tribsInfo = tribsArray[0].timestamp; |
tribsInfo = tribsArray[0].timestamp; |
||
tribsInfo = tribsInfo.slice(0, tribsInfo.indexOf("T")); |
tribsInfo = tribsInfo.slice( 0, tribsInfo.indexOf("T") ); |
||
tribsInfo = tribsArray[0].revid + "|" + tribsInfo; |
tribsInfo = tribsArray[0].revid + "|" + tribsInfo; |
||
tribsInfo = "[[Special:PermanentLink/" + tribsInfo + "]]"; |
tribsInfo = "[[Special:PermanentLink/" + tribsInfo + "]]"; |
||
Line 171: | Line 171: | ||
} |
} |
||
var logsInfo = logsArray[0].timestamp; |
var logsInfo = logsArray[0].timestamp; |
||
logsInfo = logsInfo.slice(0, logsInfo.indexOf("T")); |
logsInfo = logsInfo.slice( 0, logsInfo.indexOf("T") ); |
||
logsInfo = "[[Special:Log/" + userName + "|" + logsInfo + "]]"; |
logsInfo = "[[Special:Log/" + userName + "|" + logsInfo + "]]"; |
||
$("#inactiveList").append( "<li>" + userLink + " || " + tribsInfo + " || " + logsInfo + "</li>" ); |
$("#inactiveList").append( "<li>" + userLink + " || " + tribsInfo + " || " + logsInfo + "</li>" ); |
||
Line 179: | Line 179: | ||
var list = $("#inactiveList")[0].innerText; |
var list = $("#inactiveList")[0].innerText; |
||
list = '|-\n|' + list; |
list = '|-\n|' + list; |
||
while(list.indexOf("\n[[User:") > 0) { |
while( list.indexOf("\n[[User:") > 0 ) { |
||
list = list.substr(0,list.indexOf("\n[[User:")) + '\n|-\n|' + list.substr(list.indexOf("\n[[User:") + 1); |
list = list.substr( 0, list.indexOf( "\n[[User:" ) ) + '\n|-\n|' + list.substr( list.indexOf( "\n[[User:" ) + 1 ); |
||
} |
} |
||
$.getJSON( |
$.getJSON( |
||
mw.util.wikiScript('api'), |
mw.util.wikiScript( 'api' ), |
||
{ |
{ |
||
format: 'json', |
format: 'json', |
||
Line 194: | Line 194: | ||
try { |
try { |
||
var wtext = data.query.pages[440].revisions[0]['*']; |
var wtext = data.query.pages[440].revisions[0]['*']; |
||
wtext = wtext.substr(0, wtext.indexOf("|-")) + list + wtext.substr(wtext.indexOf("|}")); |
wtext = wtext.substr( 0, wtext.indexOf("|-") ) + list + wtext.substr( wtext.indexOf( "|}" ) ); |
||
$.ajax( { |
$.ajax( { |
||
url: mw.util.wikiScript( 'api' ), |
url: mw.util.wikiScript( 'api' ), |
||
Line 207: | Line 207: | ||
token: mw.user.tokens.get( 'csrfToken' ), |
token: mw.user.tokens.get( 'csrfToken' ), |
||
} |
} |
||
} ).done(function() { |
} ).done( function() { |
||
alert( "Updated TestWiki:Inactivity/Inactive administrators" ); |
alert( "Updated TestWiki:Inactivity/Inactive administrators" ); |
||
var reload = new OO.ui.ButtonWidget({ label: 'Load origional page' }); |
var reload = new OO.ui.ButtonWidget( { label: 'Load origional page' } ); |
||
$('#upd-inactive-bttn').replaceWith(reload.$element.click(function(){window.location.reload();})); |
$('#upd-inactive-bttn').replaceWith( reload.$element.click( function() { window.location.reload(); } ) ); |
||
} ).fail( function ( e, data ) { |
} ).fail( function ( e, data ) { |
||
console.log( e.message ); |
console.log( e.message ); |
||
Line 216: | Line 216: | ||
} ); |
} ); |
||
} |
} |
||
catch(e) { |
catch( e ) { |
||
// ignore |
// ignore |
||
} |
} |
Revision as of 20:34, 4 November 2017
//<nowiki>
//This script automates the process of finding inactive sysops.
//Warning: the script will replace the contents of the page you are currently viewing when you click the link at the top right of the page.
//Output is in the format of "[[User:Example|Example]] || [[Special:PermanentLink/1234|timestamp]] || [[Special:Log/Example|timestamp]]"
mw.loader.using(['oojs-ui', 'mediawiki.util']).done( function () {
var scriptActivationLink = mw.util.addPortletLink(
'p-personal',
'#',
'Find Inactive Sysops',
'pt-testscript',
'Replaces the contents of the current page with a list of inactive sysops',
null,
'#pt-adminlinks'
);
$( scriptActivationLink ).click( function () {
var doThis = confirm('Do you want to run the script?');
if( doThis ) {
$.getJSON(
//Get userlist
mw.util.wikiScript( 'api' ),
{
format: 'json',
action: 'query',
list: 'allusers',
augroup: 'sysop',
aulimit: 50, //Set limit to 50 as there are no more than 50 sysops
}
).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 exempt = ["John", "Pup", "Void", "NDKilla", "Reception123", "Revi", "Southparkfan", "Abuse filter"];
for ( i = 0; i < exempt.length; i++ ) {
userstring = userstring.replace( exempt[i] + ",", "" );
}
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++ ) {
checkUser( reducedList[x] );
}
var updButton = new OO.ui.ButtonWidget( {
label: 'Update Inactive Administrators?',
id: 'upd-inactive-bttn'
} );
$("#inactiveList").after( updButton.$element.click( function() { updList(); } ) );
}
function checkUser( user ) {
$.getJSON(
//Get contribs and log entries
mw.util.wikiScript( 'api' ),
{
format: 'json',
action: 'query',
list: 'logevents|usercontribs',
leprop: 'timestamp',
ledir: 'older',
leuser: user,
lelimit: 1, //We only need the most recent log action/edit
uclimit: 1,
ucuser: user,
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 ) {
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." )*/);
}
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 3 months
mm -= 3;
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://publictestwiki.com/wiki/Special:Contribs/" + userName + "\">" + userName + "</a>|" + userName + "]]";
var tribsInfo;
if( tribsArray[0] !== undefined ) {
tribsInfo = tribsArray[0].timestamp;
tribsInfo = tribsInfo.slice( 0, tribsInfo.indexOf("T") );
tribsInfo = tribsArray[0].revid + "|" + tribsInfo;
tribsInfo = "[[Special:PermanentLink/" + tribsInfo + "]]";
}
else {
tribsInfo = 'None';
}
var logsInfo = logsArray[0].timestamp;
logsInfo = logsInfo.slice( 0, logsInfo.indexOf("T") );
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: 440,
prop: 'revisions',
rvprop: 'content'
}
).done( function ( data ) {
try {
var wtext = data.query.pages[440].revisions[0]['*'];
wtext = wtext.substr( 0, wtext.indexOf("|-") ) + list + wtext.substr( wtext.indexOf( "|}" ) );
$.ajax( {
url: mw.util.wikiScript( 'api' ),
type: 'POST',
dataType: 'json',
data: {
format: 'json',
action: 'edit',
title: 'TestWiki:Inactivity/Inactive administrators',
text: wtext,
summary: 'automatically updating list',
token: mw.user.tokens.get( 'csrfToken' ),
}
} ).done( function() {
alert( "Updated TestWiki:Inactivity/Inactive administrators" );
var reload = new OO.ui.ButtonWidget( { label: 'Load origional page' } );
$('#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 ) {
// ignore
}
} ).fail( function ( e, data ) {
console.log( e.message );
console.log( JSON.stringify( data ) );
} );
}
});
//</nowiki>