Jump to content

User:Technical 13/Scripts/Edit counter.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Technical 13 (talk | contribs) at 13:14, 10 April 2013 (No calculating for now). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/********************************************************************************************/
/* Edit counter script based on strategywiki.org/wiki/User:Najzere/edit_counter.js          */
/*   author:        User:Technical_13                                                       */
/*   source:        http://en.wikipedia.org/wiki/User:Technical_13/edit_counter.js          */
/*   documentation: http://en.wikipedia.org/wiki/User_talk:Technical_13/edit_counter.js     */ //Not yet available.
/*   contact:       http://en.wikipedia.org/wiki/User_talk:Technical_13                     */
/*   license:       CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)           */
/********************************************************************************************/

// Only add edit count button on user pages
if (mw.config.get('wgTitle') == mw.config.get('wgUserName') && mw.config.get('wgNamespaceNumber') == 2) {
$(document).ready(function() {

// Add a new link after "User contributions" in the toolbox
/* Don't do this for now.
  if ($('#t-contributions').length==0) {
    alert('Expected element not found. Please see http://strategywiki.org/wiki/User_talk:Najzere to have your wiki added.');
    return;
  };
*/
  var $editCountLink = $('<a>Edit counter</a>').attr( { id: 't-editcounter', href: '#', title: 'Run edit counter script' } );
  $('#t-contributions').after($('<li></li>').html($editCountLink));
// start process
  $('#t-editcounter').click( function() {

// Set up variables
  $(this).html('Initializing<br />variables...');
  if (mw.config.get('wgServer') == 'http://en.wikipedia.org') {
    var editSummary = 'Edited with [[User_talk:Technical_13/edit_counter.js|edit counter.js]]';
    var tableTop = '[[User_talk:Technical_13/edit_counter.js|Edit count]]';
  } else {
    var editSummary = 'Edited with [[W:User_talk:Technical_13/edit_counter.js|edit counter.js]]';
    var tableTop = '[[W:User_talk:Technical_13/edit counter.js|Edit count]]';
  };
  if (typeof(editPage) == 'undefined') {editPage = 'Edit count'};
  if (typeof(tableClass) == 'undefined') {tableClass = ''};
  if (typeof(tableStyle) == 'undefined') {tableStyle = ''};
  if (editPage == '') {editPage = 'Edit count'};
  var nslist = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 100, 101, 108, 109, 446, 447, 710, 711, 828, 829);
  var skipcaptcha = false;
  var oldpage = false;
  var edits;
  var namespaces = [];
  var movelog = [];
  var protectlog = [];
  var next;
  var edittoken;
  var succeeded = false;
  var brace = '{';
var list = '';//testing line
  var table = '{| class="wikitable '+tableClass+'" style="'+tableStyle+'"\n!colspan="2"| '+tableTop+': '+brace+brace+'subst:formatnum:';

// Check if the user is autoconfirmed
  $(this).html('CAPTCHA test...');
  for (var right in wgUserGroups)
    if (wgUserGroups[right] == 'autoconfirmed') {skipcaptcha = true};

// Get full edit count, as tracked by MW, add it to table
  $(this).html('Gathering total edits...');
  $.ajax({
    url: '/w/api.php?action=query&meta=userinfo&uiprop=editcount&format=json',
    dataType: 'json',
    async: false,
    success: function(uiResponse) {
      table += uiResponse.query.userinfo.editcount+'}}';
    }
  });

// Initialize namespace, move log and patrol log arrays
  $(this).html('Gathering<br />namespaces...');
  $.ajax({
    url: '/w/api.php?action=query&meta=siteinfo&siprop=namespaces&format=json',
    dataType: 'json',
    async: false,
    success: function(siResponse) {
      for (var ns in siResponse.query.namespaces) {
        if (siResponse.query.namespaces[ns].id > -1) {
          if(siResponse.query.namespaces[ns].id != nslist[namespaces.length])
            break;
          if (siResponse.query.namespaces[ns].id == 0) {
            namespaces[siResponse.query.namespaces[ns].id] = 'Main';
list += '\n* ' + siResponse.query.namespaces[ns].id + ' =>' + namespaces[siResponse.query.namespaces[ns].id]; //testing line
          }
          else if (siResponse.query.namespaces[ns].id == 4) {
            namespaces[siResponse.query.namespaces[ns].id] = 'Wikipedia';
list += '\n* ' + siResponse.query.namespaces[ns].id + ' =>' + namespaces[siResponse.query.namespaces[ns].id]; //testing line
          }
          else if (siResponse.query.namespaces[ns].id == 5) {
            namespaces[siResponse.query.namespaces[ns].id] = 'Wikipedia talk';
list += '\n* ' + siResponse.query.namespaces[ns].id + ' =>' + namespaces[siResponse.query.namespaces[ns].id]; //testing line
          }
          else {
            namespaces[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns].canonical;
list += '\n* ' + siResponse.query.namespaces[ns].id + ' =>' + namespaces[siResponse.query.namespaces[ns].id]; //testing line
          };
          movelog[siResponse.query.namespaces[ns].id] = 0;
          protectlog[siResponse.query.namespaces[ns].id] = 0;
        };
      };
list += '\n:* There were ' + namespaces.length + " namespaces pulled.";
    }
  });

// Fill move log array
  $(this).html('Counting page<br />move logs...');
  next = '';
  while (next != 'stop') {
    $.ajax({
      url: '/w/api.php?action=query&list=logevents&letype=move&leuser='+mw.config.get('wgUserName')+'&lelimit=max&leprop=title&format=json'+next,
      dataType: 'json',
      async: false,
      success: function(moveResponse) {
        for (var event in moveResponse.query.logevents) {
          movelog[moveResponse.query.logevents[event].ns] += 1;
        };
        if (moveResponse["query-continue"]) {
          next = '&lestart='+moveResponse["query-continue"].logevents.lestart;
        } else {
          next = 'stop';
        };
      }
    });
  };

// Fill protect log array
  $(this).html('Counting page<br />protection logs...');
  next = '';
  while (next != 'stop') {
    $.ajax({
      url: '/w/api.php?action=query&list=logevents&letype=protect&leuser='+mw.config.get('wgUserName')+'&lelimit=max&leprop=title&format=json'+next,
      dataType: 'json',
      async: false,
      success: function(protectResponse) {
        for (var event in protectResponse.query.logevents) {
          protectlog[protectResponse.query.logevents[event].ns] += 1;
        };
        if (protectResponse["query-continue"]) {
          next = '&lestart='+protectResponse["query-continue"].logevents.lestart;
        } else {
          next = 'stop';
        };
      }
    });
  };

// Loop through namespaces counting edits and subtracting page moves and patrols/* No calculating for now
  $(this).html('Calculating<br />edits per<br />namespace...');
  for (var i = 0; i < namespaces.length; i++) {
    next = '';
    edits = 0;
    while (next != 'stop') {
      $.ajax({
        url: '/w/api.php?action=query&list=usercontribs&ucuser='+mw.config.get('wgUserName')+'&uclimit=max&ucdir=newer&format=json&ucnamespace='+i+next,
        dataType: 'json',
        async: false,
        success: function(ucResponse) {
          edits += ucResponse.query.usercontribs.length;
          if (ucResponse["query-continue"]) {
            next = '&ucstart='+ucResponse["query-continue"].usercontribs.ucstart;
          } else {
            next = 'stop';
          };
        }
      });
    };
    edits = edits - movelog[i] - protectlog[i];
    if (edits > 0) {
      table += '\n|-\n| '+namespaces[i]+' || '+brace+brace+'subst:formatnum:'+edits+'}}';
    };
  };
  table += '\n|-\n|colspan="2" align="center"| <small>Last run: '+brace+brace+'subst:#time:F j, Y}}</small>\n|}';
*/
// Get edit token and check if we'll be creating a new page (in the case of non-autoconfirmed users)
  $.ajax({
    url: '/w/api.php?action=query&prop=info|revisions&intoken=edit&format=json&titles=User:'+mw.config.get('wgUserName')+'/'+editPage,
    dataType: 'json',
    async: false,
    success: function(propResponse) {
      for (var page in propResponse.query.pages) {
        edittoken = propResponse.query.pages[page].edittoken.replace(/\+\\$/g,'%2B%5C');
        if (propResponse.query.pages[page]["revisions"]) {
          oldpage = true;
        };
      };
    }
  });

// If the user is not autoconfirmed and we're creating a new page, kick out of the script
  if (!skipcaptcha && !oldpage) {
    alert("Sorry, you're not autoconfirmed yet, so captcha is required to make new pages. Go create the page at: User:"+mw.config.get('wgUserName')+"/"+editPage+", then try again.");
    $(this).html('Error!');
    return;
  };

/*// Edit the target page with the table
  $(this).html('Creating page...');
  $.ajax({
    url: '/w/api.php?action=edit&title=User:'+mw.config.get('wgUserName')+'/'+editPage+'&summary='+editSummary+'&minor=1&recreate=1&bot=1&text='+escape(table)+'&token='+edittoken,
    type: 'POST',
    async: false,
    success: function() { succeeded = true; }
  });
// TESTING SECTION - list of NS*/
  $(this).html('Creating page...');
  $.ajax({
    url: '/w/api.php?action=edit&title=User:'+mw.config.get('wgUserName')+'/'+editPage+'&summary='+editSummary+'&minor=1&recreate=1&bot=1&text='+escape(list)+'&token='+edittoken,
    type: 'POST',
    async: false,
    success: function() { succeeded = true; }
  });
// END TEST

// change edit count link to reflect the status of the page save
  if (succeeded) {
    $(this).html('Done!');
  } else {
    $(this).html('Error!');
  };

  });
});
};