User:Technical 13/SandBox/UserContribs.js

// Set up variables var isLogging = false; var namespaceIndex = []; var allUsers = {};

// Function to make it easy to toggle TRACERs on or off function logEvent( eventLog ) { if ( isLogging ) { console.log( eventLog ); } else { /* Logging is turned off. */ } }

// Function to transform text into ucfirst function upperFirst( textString ) { return textString.substr( 0, 1 ).toUpperCase + textString.substr( 1 ); }

// Initialize namespace, move log and patrol log arrays function getNamespaces { logEvent( 'Obtaining list of namespaces' );// TRACER return new mw.Api.get( {   action: 'query',    meta: 'siteinfo',    siprop: 'namespaces',    format: 'json'  } ).then( function ( siResponse ) {    var namespaces = {      '-2': 'media',      '-1': 'special',      '0': 'main',      '4': mw.config.get( 'wgSiteName' ),      '5': mw.config.get( 'wgSiteName' ) + ' talk'    };    var i = 0;    $.each( siResponse.query.namespaces, function ( ns ) { var nsIndex = siResponse.query.namespaces[ ns ].id; if ( nsIndex > -1 ) { if ( nsIndex > 0 && nsIndex != 4 && nsIndex != 5 && nsIndex != 111 ) { namespaces[ nsIndex ] = siResponse.query.namespaces[ ns ].canonical; }       namespaceIndex[nsIndex ] =  i;        i++; }   } );    console.info( '  Done building namespaceIndex: %o', namespaceIndex );// TRACER    console.info( '  Done building namespaces: %o', namespaces );// TRACER    return namespaces;  } ); }

// Get the contributions of a user function getContributions( thisUser ){ return new mw.Api.get( {   action: 'query',    ucuser: thisUser,    uclimit: 'max',    ucprop: 'ids|timestamp|flags',    format: 'json'  } ).done( function ( ucResponse ) {    /* do stuff */    logEvent( 'Skipping ' + thisUser + '\'s raw contributions work for now, just use mwEditCount.' );// TRACER    return ucResponse;  } ); }

// Log array function that will let me reuse the code for differnt types of logs. function logCount( logType, logUser ) { var logTypes = [ 'block', 'delete', 'import', 'merge', 'move', 'newusers', 'patrol', 'protect', 'stable', 'rights', 'upload' ]; if ( $.inArray( logType, logTypes ) != -1 ) { logEvent( ' Building an object to process ' + logUser + '\'s ' + logType + ' log:' );// TRACER switch ( logType ) { case 'delete': logTypeName = 'contributionsDeleted'; allLogs = 'allDeletions'; break; case 'move': logTypeName = logType + 'dPages'; allLogs = 'allMoves'; break; case 'import': case 'protect': logTypeName = logType + 'edPages'; allLogs = 'all' + upperFirst( logType ) + 's'; break; case 'patrol': logTypeName = logType + 'ledPages'; allLogs = 'all' + upperFirst( logType ) + 's'; break; case 'merge': logTypeName = logType + 'dUsers'; allLogs = 'all' + upperFirst( logType ) + 's'; break; case 'block': logTypeName = logType + 'edUsers'; allLogs = 'all' + upperFirst( logType ) + 's'; break; case 'newusers': logTypeName = 'accountsCreated'; allLogs = 'all' + upperFirst( logType ); break; case 'rights': logTypeName = logType + 'Changed'; allLogs = 'all' + upperFirst( logType ); break; case 'upload': logTypeName = logType + 'edFiles'; allLogs = 'all' + upperFirst( logType ) + 's'; break; case 'stable': default: logTypeName = 'Unknown'; }   allUsers[ logUser ][ allLogs ] = 0; allUsers[ logUser ][ logTypeName ] = []; $.each( namespaceIndex, function ( index ) {     if ( $.isNumeric( namespaceIndex[ index ] ) ) {        allUsers[ logUser ][ logTypeName ][ namespaceIndex[ index ] ] = 0;      }    } ); return new mw.Api.get( {     action: 'query',      list: 'logevents',      letype: logType,      leuser: logUser,      lelimit: 'max',      leprop: 'title',      format: 'json'    } ).done( function ( logResponse ) {      $.each( logResponse.query.logevents, function ( index ) { allUsers[ logUser ][ logTypeName ][ namespaceIndex[ logResponse.query.logevents[ index ].ns ] ] += 1; } );     $.each( allUsers[ logUser ][ logTypeName ], function( i ) { allUsers[ logUser ][ allLogs ] += allUsers[ logUser ][ logTypeName ][ i ]; } );   } );    console.info( '\t%o', allUsers[ logUser ][ logTypeName ] );// TRACER } else { console.error( '%s is not a valid log type', logType ); } }

// Get a list of all users function getUsers { logEvent( 'Obtaining list of wiki users with edits' );// TRACER return new mw.Api.get( {   action: 'query',    list: 'allusers', aufrom: 'Technical 13',//TEMPORARY TO TEST LOG STUFFS    auprop: 'blockinfo|editcount|registration',    auwitheditsonly: true,    aulimit: 10,//'max',    format: 'json'  } ).then( function ( auResponse ) {    $.each( auResponse.query.allusers, function ( index ) { var thisUser = auResponse.query.allusers[ index ].name; logEvent( 'Creating an object for ' + thisUser + ':' );// TRACER if ( allUsers[ thisUser ] === undefined ) { allUsers[ thisUser ] = {}; }     allUsers[ thisUser ].blockCount = logCount( 'block', thisUser ); allUsers[ thisUser ].mwEditCount = auResponse.query.allusers[ index ].editcount; if ( auResponse.query.allusers[ index ].blockedby !== undefined ) { allUsers[ thisUser ].isBlocked = true; }     getContributions( thisUser ).then( function ( namespaces ) {        return logCount( 'delete', thisUser );      } ).then( function ( namespaces ) {        return logCount( 'move', thisUser );      } ).then( function ( namespaces ) {        return logCount( 'patrol', thisUser );      } ).then( function ( namespaces ) {        return logCount( 'protect', thisUser );      } ).done( function  {        console.info( '\t%s\'s completed object: %o', thisUser, allUsers[ thisUser ] );// TRACER        allUsers[ thisUser ].calculatedEditCount =          allUsers[ thisUser ].mwEditCount +          allUsers[ thisUser ].allBlocks +          allUsers[ thisUser ].allDeletions +          allUsers[ thisUser ].allMoves +          allUsers[ thisUser ].allPatrols +          allUsers[ thisUser ].allProtects;      } ); } );   return allUsers;  } ); }

// Run the code in order to get a result getNamespaces.then( function ( namespaces ) { return getUsers; } ).done( function  {  console.info( "All processed Users: %o", allUsers );//TRACER } );