User:Yair rand/GetArbPrinciples.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.
// Run this from the JS console on an enwiki page, it will generate (to log)
// [[Wikipedia:Arbitration/Index/Principles]] from all pages under
// WP:Arbitration/Requests/Case/. Is quite messy (no var names, etc), as it was
// primarily built with one-time-use in mind, but it should still work for later
// updates as necessary. Note that principles from courtesy-blanked cases will
// be removed in updates.
( async function () {
  var x = await ( new mw.Api() ).get( {
    action: 'query', list: 'allpages', apfrom:'Arbitration/Requests/Case/',
    apnamespace: 4, aplimit: 500, apfilterredir: 'nonredirects'
  } )
    .then( x => { return x.query.allpages; } );

  var xx = x.map( x => x.title ).filter( x => x.match(/\//g).length === 3 );

  var ap = xx.slice( 0 );
  function gp() {
    return (new mw.Api()).get({action:'query',prop:'revisions',rvslots:'*',rvprop:'content',titles:ap.splice(0,50)}).then( x => {
      if ( ap.length ) {
        return gp().then( y => [ x, ...y ] );
      } else {
        return [ x ];
      }
    } );
  }
  var xxx = await gp();

  var aw = xxx.map( x => Object.values( x.query.pages ) ).flat();

  function alphabetically( a, b ) {
    return a === b ? 0 : a < b ? -1 : 1;
  }

  var qq = Object.values( aw ).map( t => {
    var page = t.revisions[ 0 ].slots.main["*"],
      m = page.match(/\n==\s*Principles\s*==\n([\s\S]*?)\n==[^=]/),
      closeString = page.match( /<big>'''Case closed''' on \d\d\:\d\d\, ([^(]+)/i),
      closeDString = closeString && new Date( closeString[ 1 ].trim() ),
      closeDate = closeDString && closeDString.getFullYear() + '-' + ( closeDString.getMonth() + 1 ).toString().padStart(2,0);
    if ( !closeDString ) { console.error( t.title ); return []; }
    if ( m && m[ 1 ] ) {
      var b = m[ 1 ],
        r = [ ...b.matchAll( /(?:^|\n)===+\s*([^\n]+?)===+\n[\dA-Z]+(?:\.\d+)?\)\s*([\s\S]*?)(?=\n==|$)/g ) ]
          .map( x => [
            x[ 1 ].trim().replace( /\s*\((?:\d+|[Aa]lternate|I+V?|V)\)$/, '' ), // header
            x[ 2 ].replace( /<p>/g, '' ).replace(/\n\:+\s*(?:\'\')?\s*Passed.+[\n\s]*$/, '' ).replace( /  /g, ' ' ).trim(),
            t.title,
            x[ 1 ].trim(), // anchor
            closeDate
          ] );
      return r;
    }
  } ).flat().sort( ( a, b ) => alphabetically( a[ 0 ], b[ 0 ] ) || alphabetically( a[ 4 ] || b[ 4 ] ) ).filter(x=>x);

  var result = [...new Set(qq.map(x=>x[0]))].map( header =>{
    var h = '==' + header + '==\n',
      g = qq.filter( y => y[ 0 ] === header ),
      // by paragraph
      p = [ ...new Set( g.map( x => x[ 1 ] ) ) ],
      // paragraph groups
      l = p.map( pp => {
        var ppp = g.filter( x => x[ 1 ] === pp );
        return pp + ' <small> -' + ppp.sort( ( a, b ) => alphabetically( a[ 4 ], b[ 4 ] ) ).map( ( x, i ) => '[[' + x[ 2 ] + '#' + x[ 3 ] + '|' + (x[ 4 ]) + ']]' ).join(', ') + '</small>'; } );
    return h + l.join('\n----\n');
  } ).join('\n');
  
  console.log( result );
  
  return result;
} )();