User:Yair rand/Petition scan.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// This is more of a "follow these instructions in the console" then a script to run, sorry about that.
// Also, this is a bit more thwacking the API than is probably good practice.

// Run this from the edit screen of the page with the user links.
allusers = [...new Set(wpTextbox1.value.match(/\[\[User:[^\|\]]+(?=[\|\]])/g).map(x=>x.substr(7).trim()))]
  .filter(x=>!x.match(/^\d+\.\d+\.\d+\.\d+$/)) // Rm IPs.
  .filter(x=>!x.match(/\//)) // Users, not user subpages
  .map(x=>x.replace(''', "'" ) ); // whyyy

// Run this, then wait for the results to show up in the console before running the rest.
Promise.all(allusers.map(u => (new mw.Api()).get({
  action:'query',meta:'globaluserinfo',guiuser:u,guiprop:'groups|merged|rights|editcount',
  smaxage:60*60*24,maxage:60*60*24,
}))).then(x=>{
  return x.map( x => {
    var q = x.query.globaluserinfo, e = q.editcount, g = q.groups, m = q.merged, r = q.registration;
    var lrights = m.map( x => x.groups || [] ).flat();
    var mWiki = m.reduce( ( m, c ) => m.editcount < c.editcount ? c : m, { editcount: 0 } ).wiki;
    var registration = [ r, ...m.map( x => x.registration )].sort()[ 0 ];
    return { ggroups: g, registration, mWiki, lrights, editcount: e };
  } );
}).then( x => console.log( window.d = x ) )

// Afterwards, running each of these in the console will output the relevant data:

// Home wiki stats
Object.entries(d.map(x=>x.mWiki).reduce((w,d)=>(d&&(w[d]=(w[d]||0)+1),w),{})).map(x=>x.join('=')).sort().join('; ')

// Count rights
['sysop','interface-admin','rollbacker','bureaucrat','checkuser','oversight'].forEach(g=>console.log(g,d.filter(x=>x.lrights.includes(g)).length))

// Global rights
['steward','otrs-member','global-sysop','global-rollbacker'].forEach(g=>console.log(g,d.filter(x=>x.ggroups.includes(g)).length))

// Rights instances:
['sysop','interface-admin','rollbacker','bureaucrat','checkuser','oversight'].forEach(g=>console.log('instances: ',g,d.map(x=>x.lrights.filter(x=>x===g)).flat().length))

// Total edit count:
d.reduce((a,b)=>a+b.editcount,0)

// Median registration
d.map(x=>x.registration).sort()[d.length>>1]

// Edit count quartiles
[.25,.5,.75].map(y=>d.map(x=>x.editcount).sort((a,b)=>a-b)[(d.length*y)|0])