Jump to content

User:Yair rand/TopEditors.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Get a list of the top editors of a set of articles linked to from a certain page.

// TODO: Filter out bots
function getTopEditors( categoryTitle ) {
  var f = c => fetch( `https://wiki.riteme.site/w/api.php?action=query&format=json&list=categorymembers&cmnamespace=1&cmtitle=${ categoryTitle }&cmlimit=500`+(c?'&cmcontinue='+c:'')).then(x=>x.json()).then(x=>{

    var p = x.query.categorymembers.map( x => x.title.substr( 5 ) );
    // p = p[ Object.keys(p)[0] ].links;
    if ( x.continue ) {
      return f( x.continue.cmcontinue ).then( r => p.concat( r ) );
    } else {
      return p;
    }
  });
  f().then(x=>{
    var allRevs = [];
    var getRevs = ( t, c ) => fetch( 
        `https://wiki.riteme.site/w/api.php?action=query&format=json&prop=revisions&titles=${ t }&rvprop=user&maxlag=5&rvlimit=500&rvstart=${new Date('2021').toISOString()}&rvend=`+new Date('2020').toISOString() + ( c || '' )
      ).then( x => x.json() ).then( x => {
        var y = x.query && x.query.pages,
          revs = y && y[ Object.keys( y )[ 0 ] ].revisions || [];
        return x.continue ? getRevs( t, '&rvcontinue='+x.continue.rvcontinue ).then( a => revs.concat( a ) ) : revs;
      } );
    var nextPage = () => {
      var page = x.shift();
      if ( page ) {
        return getRevs( page ).then( x => { allRevs.push( ...x ); return nextPage() } );
      } else {
        return allRevs;
      }
    };
    return nextPage()
      .then( x => {
        var results = {};
        x.forEach( rev => {
          results[ rev.user ] = results[ rev.user ] || 0;
          results[ rev.user ]++;
        } );
        return results;
      } );
  }).then( x=> {
    console.log( Object.keys(x).sort((a,b)=>x[b]-x[a]).map(a=>[a,x[a]]).map((a,i)=>`|-
|${i+1}
|[[User:${a[0]}]]
|${a[1]}
`).join('') );
    
  } );
}

// Use a list of all medical articles
getTopEditors( 'Category:All_WikiProject_Medicine_articles' );