Jump to content

User:The Voidwalker/latestContribs.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
// Script designed to find the most recent contributions by a large set of users.
// To activate, go to [[Special:LatestContributions]]

if( mw.config.get('wgNamespaceNumber') === -1 && /Special:[Ll]atest[Cc]ontrib(s|utions)/.test(mw.config.get('wgPageName')) ) {
  mw.loader.using( ['oojs-ui', 'mediawiki.util', 'jquery.tablesorter'] ).then( function() {
    createLCInterface();
  } );
}

function createLCInterface() {
  $('#firstHeading').text( mw.config.get('wgPageName') );
  text = $('#mw-content-text');
  text.html(''); // Blank slate
  text.append('<p>Enter a list of usernames in the box below, or add them from a category (any "User:" prefix is stripped when submitted). Default limit is 50.</p>');
  var groupInput = new OO.ui.ActionFieldLayout( new OO.ui.TextInputWidget({ placeholder: 'Enter a usergroup' }), new OO.ui.ButtonWidget({ label: 'Add' }), { align: 'top' } ), // align top so button is attached to input widget
    nameInput = new OO.ui.MultilineTextInputWidget({ placeholder: 'Enter a list of usernames separated by a newline', autosize: true }),
    limitSelect = new OO.ui.FieldLayout( new OO.ui.DropdownInputWidget({ options: [{ data: 10, label: '10' }, {data: 25, label: '25'}, {data: 50, label: '50'}, {data: 100, label: '100'}, {data: 250, label: '250'}, {data: 500, label: '500'}, {data: 0, label: 'all'}], value: 50 }), { label: 'Limit:', align: 'right' } );
    submit = new OO.ui.ButtonWidget({ label: 'Submit' });
  groupInput.$button.click(function() {
    if(groupInput.getField().value.length === 0)
      return alert("Please enter a usergroup");
    addGroup();
  });
  nameInput.$element.css('margin-top', '1em'); // spacing
  text.append(groupInput.$element);
  text.append(nameInput.$element.css('margin-top', '1 em'));
  text.append(limitSelect.$element);
  text.append(submit.$element.click(function(){submit.setDisabled(true); makeList();}));
  
  function addGroup(cont) {
    var val = groupInput.getField().value;
    var query = {
      action: 'query',
      format: 'json',
      list: 'allusers',
      augroup: val,
      aulimit: 500,
    };
    if(cont){
      query.continue = cont.continue;
      query.aufrom = cont.aufrom;
    }
    $.getJSON(mw.util.wikiScript('api'), query).done(function(data) {
      var users = data.query.allusers;
      for(var i = 0; i < users.length; i++) {
        nameInput.setValue(nameInput.value + (nameInput.value.length === 0 ? '' : '\n') + users[i].name);
      }
      if(data.continue) {
        addGroup(data.continue);
      } else {
        alert('Added users from group ' + val);
        groupInput.getField().setValue('');
      }
    });
  }
  
  function makeList(users, limit) {
    if( !users ) {
      if( limitSelect.getField().value === '0' && !confirm("You have set the limit to all. Do you wish to procede? (This could take very long time)") )
        return submit.setDisabled(false);
      users = nameInput.value.split('\n');
      users.reverse();
      limit = limitSelect.getField().value === '0' ? Infinity : limitSelect.getField().value;
      text.html('<p id="patience-plz">Fetching list, please be patient (this may take a while).</p><table class="wikitable"><thead><tr><th>Page</th><th>Revision</th><th>User</th></tr></thead><tbody id="ca-list"></tbody></table>');
    }
    var query = {
      action: 'query',
      format: 'json',
      list: 'usercontribs',
      uclimit: 1,
      ucprop: 'title|ids|timestamp',
      ucuser: users.pop(),
    };
    $.getJSON( mw.util.wikiScript('api'), query).done(function(data) {
      var tribs = data.query.usercontribs;
      if(tribs[0]) {
        appendRev(tribs[0]);
        limit--;
      }
      if( limit > 0 && users.length > 0 ) {
        makeList(users, limit);
      } else {
      	$('#patience-plz').remove();
        $('table').tablesorter();
      }
    } );
  }
  function appendRev(revision) {
    var text = '<tr><td><a href="/wiki/' + revision.title + '">' + revision.title + '</a></td><td><a href="/w/index.php?oldid=' + revision.revid + '">' +
               revision.timestamp + '</a> (<a href="/w/index.php?diff=' + revision.revid + '">prev</a>)</td><td><a href="/wiki/User:' + 
               revision.user + '">' + revision.user + '</a> (<a href="/wiki/User_talk:' + revision.user + '">talk</a> | <a href="/wiki/Special:Contribs/' +
               revision.user + '">contribs</a>)</td></tr>';
    $('#ca-list').append(text);
  }
}