MediaWiki:Autopromote.js

( function ( mw, $ ) { // Initialize some constants  var promotionCandidates = {};  var ecVIP = 100;// DDO:Ddowikivip: "The general consensus seems to be any user with a triple digit edit count (C:100 edit users) is qualified..."  var ecSU = 250;// DDO:Superuser: "Users that are up to at least C:250 edit users should be in this group."  var toVipTopic = 'Congratulations! You\'re a DDO wiki VIP!';  var toSuTopic = 'Congratulations! You\'re a SuperUser!';  var editsSummary = encodeURIComponent( 'Updated edit count with Auto-promote' );  var toVipSummary = encodeURIComponent( 'Promoted to DDO wiki VIP with Auto-promote' );  var toSuSummary = encodeURIComponent( 'Promoted to SuperUser with Auto-promote' );  var toVipTalk = ;  var toSuTalk = ;  var toVipEmail = ;  var toSuEmail = ; // Function to send an email through the API function sendEmail( userName = 'Unknown user' ) { } // Get a complete list of users, edit counts, account ages, and current groups. var aufrom = '';// start at the beginning while( aufrom !== '#' ) { $.ajax( {//* REALLY DON'T LIKE THIS!!! *//*     url: 'http://ddowiki.com/api.php?action=query&format=json&aulimit=max&list=allusers&auexcludegroup=sysop|bureaucrat|steward&auprop=groups|blockinfo|editcount|registration&auwitheditsonly=1&aufrom=' + aufrom,      dataType: 'json',      async: false,//* REALLY DON'T LIKE THIS!!! IT WILL FAIL IN THE FUTURE!!! *//*      success: function( successData ) {        var resultObject = successData.query.allusers;        $.each( resultObject, function { var isBlocked = ( $( this )[ 0 ].blockedby !== undefined ? true : false ); var editCount = $( this )[ 0 ].editcount;// Get the edit count from the API (doesn't include deleted edits) var isSU = ( $.inArray( 'superuser', $( this )[ 0 ].groups ) !== -1 ? true : false );// Is the user a superuser var isVIP = ( $.inArray( 'ddowikivip', $( this )[ 0 ].groups ) !== -1 ? true : false );// Is the user a ddowikivip // If the user is not blocked, and should be a VIP or SU but isn't, then... if ( !isBlocked && ( ( !isVIP && editCount >= ecVIP ) || ( !isSU && editCount >= ecSU ) ) ) { promotionCandidates[ $( this )[ 0 ].name ] = {};// Create an object with the user name promotionCandidates[ $( this )[ 0 ].name ].editcount = editCount;// Put edit count in user's object var currDate = new Date;// Define a new date constructor var currDateInMonths = currDate.getFullYear * 12 + currDate.getMonth;// Calculate the current number of months since Jan 2000 var regSince = ( $( this )[ 0 ].registration === '' ? new Date( 1999, 11, 30 ) : new Date( $( this )[ 0 ].registration ) );// Date object for registration date var age = ( currDateInMonths - ( ( regSince.getFullYear * 12 ) + regSince.getMonth ) );// Calculate age in months promotionCandidates[ $( this )[ 0 ].name ].age = age;// Put the age in the user's object promotionCandidates[ $( this )[ 0 ].name ].isSU = isSU;// Toggle the superuser object element promotionCandidates[ $( this )[ 0 ].name ].isVIP = isVIP;// Toggle the ddowikivip object element }       } );        if ( successData[ 'query-continue' ] !== undefined ) {          aufrom = successData[ 'query-continue' ].allusers.aufrom;// More users to process        } else {          aufrom = '#';// Last page - User: can not exits and returns "Bad title" if you try. Seems like a fitting way to end the while loop        }      },      fail: function( failureData ) {        alert( 'Sorry, failed to retrieve information from API.\nPlease check the console for more details if available.' );        console.error( '%O', failureData );      }    } ); } $( 'li#ca-nstab-special span a' ).attr( { 'href': './Special:BlankPage?withJS=MediaWiki:Autopromote.js' } );// Reload the script -- needs to be adjusted later $( '#firstHeading span' ).text( 'Semi-auto-promote' );// The title for the page, probably should come up with something better later $( '#mw-content-text' ).html( ' This is a list of users who meet the requirements for promotion to one or more user groups. ' );// Page description $( '.mw-semiautopromote-intro' ).after( '' );// Create an empty unordered list function userAgeEdits( age = 0, editCount = 0 ) { return ' ( ' + editCount + '/ + age +  ' + ( Math.floor( ( editCount / age ) * 100 ) / 100 ) + ' /mo ) '; } function userPageLink( userName = 'Unknown user' ) { return '' + userName + ''; } function userTalkLink( userName = 'Unknown user' ) { return 'talk'; } function userEmailLnk( userName = 'Unknown user' ) { $.ajax( {//* REALLY DON'T LIKE THIS!!! *//*     url: 'http://ddowiki.com/api.php?action=query&format=json&list=users&usprop=emailable&ususers=' + encodeURIComponent( userName ),      dataType: 'json',      success: function( userData ) {        promotionCandidates[ userName ].isEmailable = ( userData.query.users[ 0 ].emailable !== undefined ? true : false );		if ( !promotionCandidates[ userName ].isEmailable ) {         $( '#' + userName.replace( ' ', '_' ) + '_uel' ).remove;        }      },      fail: function( failureData ) {        alert( 'Sorry, failed to retrieve information from API.\nPlease check the console for more details if available.' );        console.error( '%O', failureData );      }    } ); return ' · email '; } function userPageEdit( userName = 'Unknown user', editCount = 0 ) { // Function to update T:UserEditAward on the user's page with current count } function userToVIP( userName = 'Unknown user', editCount = 0, isEmailable = false ) { // Function to add to ddowikivip group, update T:UserEditAward on the user's page, leave a message on their talk page congratulating them, and send an email } function userToSU( userName = 'Unknown user', editCount = 0, isEmailable = false ) { // Function to add to superuser group, update T:UserEditAward on the user's page, leave a message on their talk page congratulating them, and send an email } $.each( promotionCandidates, function( name ) {    $( '#mw-semiautopromote-canididates' ).append( '' + userAgeEdits( $( this )[ 0 ].age, $( this )[ 0 ].editcount) + ' ' + userPageLink( name ) + ' ( ' + userTalkLink( name ) + userEmailLnk( name ) + ' )' );  } ); console.log( '%O', promotionCandidates );// Log final object } )( mediaWiki, jQuery );