User:Poke/GuildWarsWikiTools/ContributionCount.js

/* */ /**** class ContributionCount.js * (c) 2008-2009 by Patrick Westerhoff [poke] */ function ContributionCount ( userName ) { var i, doc, ul; var xmlHttp, baseUrl, url; var resultTable, resultHeader, resultStatus; var namespaces = new Object( {     '0' : { name : 'Main',                 contribs : 0 },      '1' : { name : 'Talk',                 contribs : 0 },      '2' : { name : 'User',                 contribs : 0 },      '3' : { name : 'User talk',            contribs : 0 },      '4' : { name : 'Guild Wars Wiki',      contribs : 0 },      '5' : { name : 'Guild Wars Wiki talk', contribs : 0 },      '6' : { name : 'Image',                contribs : 0 },      '7' : { name : 'Image talk',           contribs : 0 },      '8' : { name : 'MediaWiki',            contribs : 0 },      '9' : { name : 'MediaWiki talk',       contribs : 0 },     '10' : { name : 'Template',             contribs : 0 },     '11' : { name : 'Template talk',        contribs : 0 },     '12' : { name : 'Help',                 contribs : 0 },     '13' : { name : 'Help talk',            contribs : 0 },     '14' : { name : 'Category',             contribs : 0 }, '15' : { name : 'Category talk',       contribs : 0 }, '100' : { name : 'Guild',               contribs : 0 }, '101' : { name : 'Guild talk',          contribs : 0 }, '102' : { name : 'Game link',           contribs : 0 }, '103' : { name : 'Game link talk',      contribs : 0 }, '200' : { name : 'ArenaNet',            contribs : 0 }, '201' : { name : 'ArenaNet talk',       contribs : 0 }, '202' : { name : 'Feedback',            contribs : 0 }, '203' : { name : 'Feedback talk',       contribs : 0 }, '-2' : { name : 'Media',               contribs : 0 }, '-1' : { name : 'Special',             contribs : 0 } } );  if ( userName == null )    userName = window.prompt( 'Enter username:', wgUserName ) || wgUserName || '';    try    {      xmlHttp = new XMLHttpRequest;    }    catch ( e )    {      return;    }    // initialization  baseUrl    = '/api.php?format=xml&action=query&list=usercontribs&ucprop=title';  baseUrl   += '&uclimit=' + ( wgUserGroups && ( wgUserGroups.indexOf( 'sysop' ) > -1 || wgUserGroups.indexOf( 'bot' ) > -1 ) ? '5000' : '500' ); baseUrl   += '&ucuser=' + userName.replace( ' ', '_' );  url        = baseUrl;  initOutput;  window.setTimeout( getContributions, 500 );  /** private void initOutput  **/  function initOutput   {    var contentSub = document.getElementById( 'contentSub' );    resultTable    = document.createElement( 'table' );    resultHeader   = document.createElement( 'th' );    resultStatus   = document.createElement( 'td' );    resultTable.style.width       = '350px';    resultTable.style.margin      = '0.5em auto';    resultTable.style.border      = '1px solid silver';    resultTable.style.fontSize    = '85%';    resultTable.cellPadding       = '2px';    resultTable.cellSpacing       = '2px';    resultHeader.colSpan          = '2';    resultHeader.style.background = '#EEE';    resultHeader.appendChild( document.createTextNode( 'Contribution counter for User:' + userName ) ); resultStatus.colSpan         = '2' resultStatus.style.fontStyle = 'italic'; resultStatus.style.textAlign = 'center'; resultStatus.appendChild( document.createTextNode( 'Loading...' ) ); resultTable.appendChild( document.createElement( 'tr' ) ); resultTable.lastChild.appendChild( resultHeader ); resultTable.appendChild( document.createElement( 'tr' ) ); resultTable.lastChild.appendChild( resultStatus ); contentSub.parentNode.insertBefore( resultTable, contentSub.nextSibling ); } /** private void showContributions  **/ function showContributions {   var ulLeft  = document.createElement( 'ul' ); var ulRight = document.createElement( 'ul' ); var sum    = 0; for ( i in namespaces ) {     ul = ( i % 2 == 0 ) ? ulLeft : ulRight; ul.appendChild( document.createElement( 'li' ) ); ul.lastChild.appendChild( document.createTextNode( namespaces[i].name + ': ' ) ); ul.lastChild.appendChild( document.createElement( 'tt' ) ); ul.lastChild.lastChild.appendChild( document.createTextNode( namespaces[i].contribs ) ); sum += namespaces[i].contribs; }   resultTable.appendChild( document.createElement( 'tr' ) ); resultTable.lastChild.style.verticalAlign       = 'top'; resultTable.lastChild.appendChild( document.createElement( 'td' ) ); resultTable.lastChild.lastChild.appendChild( ulLeft ); resultTable.lastChild.appendChild( document.createElement( 'td' ) ); resultTable.lastChild.lastChild.appendChild( ulRight ); resultTable.appendChild( document.createElement( 'tr' ) ); resultTable.lastChild.appendChild( document.createElement( 'td' ) ); resultStatus.innerHTML = sum + ' contributions total.'; } /** private void actualizeContributions  **/ function actualizeContributions {   if ( xmlHttp.readyState != 4 ) return; doc = xmlHttp.responseXML; for ( i in namespaces ) {     namespaces[i].contribs += doc.evaluate( 'count( /api/query/usercontribs/item[ @ns = ' + i + ' ] )',        doc, null, XPathResult.NUMBER_TYPE, null ).numberValue; }   if ( doc.getElementsByTagName( 'query-continue' ).length > 0 ) {     url  = baseUrl + '&ucstart=' + doc.evaluate( '/api/query-continue/usercontribs/@ucstart',        doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue.nodeValue; window.setTimeout( getContributions, 500 ); resultStatus.innerHTML += '..'; }   else showContributions; } /** private void getContributions **/ function getContributions {   if ( url != null ) {     xmlHttp = new XMLHttpRequest; xmlHttp.onreadystatechange = actualizeContributions; xmlHttp.open( 'GET', url, true ); xmlHttp.send( null ); url = null; } } }

/* start */ new ContributionCount; /* */