सामग्री पर जाएँ

मीडियाविकि:Gadget-mark-proofread.js

विकिस्रोत से

ध्यान दें: प्रकाशित करने के बाद बदलाव देखने के लिए आपको अपने ब्राउज़र के कैश को हटाना पड़ सकता है।

  • Firefox/Safari: Reload क्लिक समय Shift दबाएँ, या फिर Ctrl-F5 या Ctrl-R दबाएँ (Mac पर ⌘-R)
  • Google Chrome: Ctrl-Shift-R दबाएँ (Mac पर ⌘-Shift-R)
  • Internet Explorer/Edge: Refresh पर क्लिक करते समय Ctrl दबाएँ, या Ctrl-F5 दबाएँ
  • Opera: Ctrl-F5 दबाएँ।
// ==================================================================
// 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;
}