User:Technical 13/SandBox/UserContribs.js

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

// Initialize namespace, move log and patrol log arrays function getNamespaces { console.log( '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.log( '  Done building namespaceIndex: %o', namespaceIndex );//TRACER    console.log( '  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 */    console.log( 'skipping contributions work for now, just use mwEditCount' );    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 ) { console.log( ' 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' + logType + 's'; break; case 'patrol': logTypeName = logType + 'ledPages'; allLogs = 'all' + logType + 's'; break; case 'merge': logTypeName = logType + 'dUsers'; allLogs = 'all' + logType + 's'; break; case 'block': logTypeName = logType + 'edUsers'; allLogs = 'all' + logType + 's'; break; case 'newusers': logTypeName = 'accountsCreated'; allLogs = 'all' + logType; break; case 'rights': logTypeName = logType + 'Changed'; allLogs = 'all' + logType; break; case 'upload': logTypeName = logType + 'edFiles'; allLogs = 'all' + logType + 's'; break; case 'stable': default: logTypeName = 'Unknown'; }   allUsers[ logUser ][ logTypeName ] = {}; allUsers[ logUser ][ logTypeName ].namespaceNumber = []; $.each( namespaceIndex, function ( index ) {     if ( $.isNumeric( namespaceIndex[ index ] ) ) {        allUsers[ logUser ][ logTypeName ].namespaceNumber[ 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 ) { if ( true ) { allUsers[ logUser ][ logTypeName ].namespaceNumber[ namespaceIndex[ logResponse.query.logevents[ index ].ns ] ] += 1; }     } );    } );    allUsers[ thisUser ][ logTypeName ] = 0; $.each( allUsers[ thisUser ][ logTypeName ].namespaceNumber, function( i ) {     allUsers[ thisUser ][ allLogs ] += allUsers[ thisUser ][ logTypeName ].namespaceNumber[ i ];    } ); console.log( '\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 { console.log( 'Obtaining list of wiki users with edits' );//TRACER return new mw.Api.get( {   action: 'query',    list: 'allusers', aufrom: 'Technical',//TEMPORARY TO TEST LOG STUFFS    auprop: 'blockinfo|editcount|registration',    auwitheditsonly: true,    aulimit: 50,//'max',    format: 'json'  } ).then( function ( auResponse ) {    $.each( auResponse.query.allusers, function ( index ) { var thisUser = auResponse.query.allusers[ index ].name; console.log( 'Creating an object for %s:', thisUser );//TRACER if ( allUsers[ thisUser ] === undefined ) { allUsers[ thisUser ] = {}; }     if ( allUsers[ thisUser ].blockCount === undefined ) { allUsers[ thisUser ].blockCount = 0; }     allUsers[ thisUser ].mwEditCount = auResponse.query.allusers[ index ].editcount; if ( auResponse.query.allusers[ index ].blockedby !== undefined ) { if ( allUsers[ auResponse.query.allusers[ index ].blockedby ] === undefined ) { allUsers[ auResponse.query.allusers[ index ].blockedby ] = {}; allUsers[ auResponse.query.allusers[ index ].blockedby ].blockCount = 0; }       allUsers[ thisUser ].isBlocked = true; allUsers[ auResponse.query.allusers[ index ].blockedby ].blockCount++; }     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.log( '\t%s\'s completed object: %o', thisUser, allUsers[ thisUser ] );//TRACER        allUsers[ thisUser ].calculatedEditCount =          allUsers[ thisUser ].mwEditCount +          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.log( "All processed Users: %o", allUsers );//TRACER } );