MediaWiki:Gadget-mark-proofread.js

Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

// ==================================================================
// Mark Page: pages that the current user can progress.
// ==================================================================

// Make sure the necessary modules are loaded
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function () {

  // Only active on Index:-namespace pages.
  if (mw.config.get('wgCanonicalNamespace' ) !== 'Index') {
    return;
  }
  // Only active when in view mode.
  if (mw.config.get('wgAction') !== 'view') {
      return;
  }
  // Anonymous users are not supported
  if (mw.config.get('wgUserName') == null) {
    return;
  }


  // Wait for the page to be parsed (new-style $(document).ready())
  $(function () { 
    var batchSize = 50; // API action=query limit.
    var allPages = $('.prp-pagequality-3').map(function() {
      return $(this).attr('title');
    }).toArray();
    for (var i = 0; i < allPages.length; i += batchSize) {
      var batch = allPages.slice(i, i + batchSize).join('|');
      var query = makeQuery(batch);
      var api = new mw.Api();
      api.get(query).done(createCallback(batch));
    }
  }); // END: $(document).ready()
}); // END: mw.loader.using()


//
// Create a callback to handle API responses.
//
// The use of a factory function is because API requests that need to be
// continued will have to trigger a new request from the callback; in other
// words we have multiple call sites where this function is needed.
//
// The .bind() is because mw.Api() tramples all over the argument list when it
// calls the callback. To get the necessary parameter to the call site inside
// the callback we have to .bind() a dummy "this" and the "batch" parameter.
function createCallback(batch) {
  return function(batch, data) {
    if (data.hasOwnProperty('continue')) {
      var query = makeQuery(batch, data);
      var api = new mw.Api();
      api.get(query).done(createCallback(batch));
    }

    for (var k in data.query.pages) { // ES6 for…of would be nice…
      var page = data.query.pages[k];

      if (page.hasOwnProperty('missing') && page.missing) {
        continue; // Page does't exist for some reason.
      }
      if (page.hasOwnProperty('invalid') && page.invalid) {
        continue; // Page is invalid for some reason.
      }

      var user = page.revisions[0].content.match(/ user="([^"]+)" /)[1];
      if (user !== mw.config.get('wgUserName')) {
        $('a[title="' + page.title + '"]').addClass('wsg-user-can-progress');
      }
    }
  }.bind(this, batch);
}

//
// Construct a parameter object (associative array) for mw.api().
//
function makeQuery (batch, data) {
  var query = {
    action: 'query',
    titles: batch,
    prop: 'revisions',
    rvprop: 'content',
    format: 'json',
    formatversion: 2
  };
  if (typeof data !== 'undefined' && data.hasOwnProperty('continue')) {
    $.extend(query, data.continue);
  }
  return query;
}