User:The Voidwalker/rfaStatsUpdater.js

//Have begun developement of a script to automatically update all the RfA stats page. Leaving it here for now. //UNTESTED, USE AT OWN RISK!!! if( mw.config.get( "wgPageName" ).startsWith( "Wikipedia:Requests_for_Adminship/" ) ) { var rfaUpdater = mw.util.addPortletLink(	   'p-cactions',	    '#',	    'RfA Updater',	    'pt-rfaupdater',    	'Update the RfA stats pages from the current closed RfA.'    	); $( rfaUpdater ).click( function {   	//Some things we need to get from the user :/    	var rfaTotal;		while ( !( typeof rfaTotal === "number" || rfaTotal > 0 ) ) {			rfaTotal = prompt("How many RfAs has the canidate had?\n(Enter 1 if this is their first, 2 if it is their second, ect.)");		}		var close = prompt("Enter a simplified close rational? (Hit cancel to have the script quote verbatim)\n" + "Syntax: S = Successful; W = Withdrawn; NN = WP:NOTNOW; SN = WP:SNOW; US: Unsuccessful; NC = No consensus.");		var wikitext;       $.getJSON( mw.util.wikiScript('api'), {               format: 'json', action: 'query', prop: 'revisions', rvprop: 'content', rvlimit: 1, titles: mw.config.get( "wgPageName" ) }       ).done( function ( data ) { try { var pageId = Object.keys(data.query.pages)[0]; wikitext = data.query.pages[pageId].revisions[0]['*']; //Get username var find = "==="; //rewriteable but handy               var username = wikitext.slice( wikitext.indexOf( find ) + find.length, wikitext.indexOf( "===" ) ); //Get votes var votePSN = wikitext.indexOf( "Final (" ) + 7;               var supports = wikitext.slice( votePSN, wikitext.indexOf( "/", votePSN ) );                votePSN = wikitext.indexOf( "/", votePSN );                var opposes = wikitext.slice( votePSN, wikitext.indexOf( "/", votePSN ) );                votePSN = wikitext.indexOf( "/", votePSN );                var neutrals = wikitext.slice( votePSN, wikitext.indexOf( "/", votePSN ) );                //Get date and close. Differs for successful and unsuccessful RFAs				var date;				var successful;				var closer = null; //damn rfarow                if ( wikitext.indexOf("Closed as successful by") > 0 ) {					date = wikitext.slice( wikitext.indexOf(" at "), wikitext.indexOf(" (UTC)"));					successful = true;					if( typeof close === 'undefined' )						close = 'S';                }                else {                	date = wikitext.slice( wikitext.indexOf( ", ", votePSN ), wikitext.indexOf( " (UTC)", votePSN ) ); successful = false; //Get close if( typeof close === 'undefined' ) { close = wikitext.slice( wikitext.indexOf( "(UTC) - ", votePSN ), wikitext.indexOf( "[[User", votePSN ) - 1 );               		//Trim out signature markup (if exists)                		var sigStuff = ["  0 ) {                				close = close.slice( 0, psnStuff );                			}                		}                	}                }                var record = "";                var rfarow = "";                rfaRecent( record );                //Should be good for the new methods. Make sure to test.                incRfaYear( successful );                yearRfas( record );                /*if( successful ) {                	srfas( rfarow );                	incUAC;                }                else {                	UAC( rfarow );                	incSRFA;                }*/            } catch ( e ) {                console.log( "Content request error: " + e.message );                console.log( "Content request response: " + JSON.stringify( data ) );            }        } ).fail( function  {            console.log( "While getting the wikitext, there was an AJAX error." );        } );    } ); }

//Updates Wikipedia:Requests for Adminship/Recent based on the param record passed to it above. function rfaRecent ( record ) { var wikitext; $.getJSON(           mw.util.wikiScript('api'),            {                format: 'json',                action: 'query',                prop: 'revisions',                rvprop: 'content',                rvlimit: 1,                titles: "Wikipedia:Requests for Adminship/Recent"            }        ).done( function ( data ) {            try {                var pageId = Object.keys(data.query.pages)[0];                wikitext = data.query.pages[pageId].revisions[0]['*'];                //Get the location of the last entry and add the new one before that.                var before = wikitext.slice( 0, wikitext.indexOf( "{{Recent" ) );                var after = wikitext.slice( wikitext.indexOf( "{{Recent" ) + 1 );                //Add in recent                before.concat( record + "\n" );                //Remove the least recent entry. Following is a complicated way of slicing from "{{Recent" to the next "\n". after = after.slice( after.lastIndexOf( "{{Recent" ), after.indexOf( after.indexOf( "\n" ), after.lastIndexOf( "{{Recent" ) ) ); //Put em back together var newWikitext = before.concat( after ); //Now, edit the page. $.ajax( {                   url: mw.util.wikiScript( 'api' ),                    type: 'POST',                    dataType: 'json',                    data: {                        format: 'json',                        action: 'edit',                        title: "Wikipedia:Requests for Adminship/Recent",                        summary: "Updating (using [[User:The Voidwalker/RfA Updater.js|RfA Updater)", //should be standard for all summaries                        token: mw.user.tokens.get( 'editToken' ),                        text: newWikitext                    }                } ).done ( function ( data ) {                    if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {                        console.log( "Wikipedia:Requests for Adminship/Recent has been updated." );                    } else {                        console.log( "While saving, the edit query returned an error. =(" );                    }                } ).fail ( function {                    console.log( "While saving, the AJAX request failed." );                } );            } catch ( e ) {                console.log( "Content request error: " + e.message );                console.log( "Content request response: " + JSON.stringify( data ) );            }        } ).fail( function  {            console.log( "While getting the wikitext, there was an AJAX error." );        } ); }

function incRfaYear( successful ) { //Incriment Wikipedia:Requests for adminship by year based on param successful }

function yearRfas( record ) { //Update Wikipedia:2017 requests for adminship //Build in to replace 2017 with variable for current year }