Jump to content

User talk:PleaseStand (bot)/Deprods

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Source code

[edit]

This is a script for the Luasóg bot framework.

var settings = {  username: "PSBot",
  password: prompt("Please enter PSBot's password", ""),
  page: "User:PSBot/Deprods"
};

// for non-Firefox browsers
if(!Array.prototype.map) {
  Array.prototype.map = function(fun /*, thisp*/) {
    var len = this.length >>> 0;
    if (typeof fun != "function") {
      throw new TypeError();
    }        
    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
      if (i in this) {
        res[i] = fun.call(thisp, this[i], i, this);
      }
    }
    return res;
  };
}

// Wrap callback functions to work around a framework limitation, allowing passage of extra arguments
// The limitation should be corrected in the next version of the framework.
function arrayize(o) {
  var a = [];
  for(var x = 0; x < o.length; x++) {
    a.push(o[x]);
  }
  return a;
}

function cbWrap(f) {
  var extraArgs = arrayize(arguments).slice(1);
  return function() {
    return f.apply(null, arrayize(arguments).concat(extraArgs));
  };
}

var API = new Luasog("http://en.wikipedia.org/w/api.php");
API.assert = "user";
API.byeline = " (bot edit)";

var oldList = [];
var lastUpdate = new Date(0);
var lastTouched = new Date();
var currentLines = [];

function updateList() {
  var now = new Date();
  if(lastTouched.getTime() > lastUpdate.getTime()) {
    trace(">> Updating list");
    deproddedPages = deproddedPages.slice(-100);
    API.post({page: settings.page, content: "{{" + settings.page + "/Header|~~~~~}}\n" + deproddedPages.join("\n"), summary: "Updating list of deprodded articles"},function(success, result){
      if(!success) {
        error(result.info);
      } else { trace(">> Edit succeeded");}});
    lastUpdate = now;
  }
}

function existenceCheck(success, result, page, last){
  if(!success) {
    error(result.info);
  } else {
    if(typeof result.content == "string") {
      trace(page + " was deprodded");
      deproddedPages.push("* {{ln2|1=" + page + "}}");
      lastTouched = new Date();
    } else {
      trace(page + " was deleted");
    }
  }
  if(last) {
    updateList();
  }
}

function trackDeprods(newList) {
  var queue = [];
  trace(">> " + oldList.length + " noms -> " + newList.length + " noms");
  for(var x = 0; x < oldList.length; x++) {
    if(newList.indexOf(oldList[x]) < 0) {
      trace("Queuing " + oldList[x]);
      queue.push(x);
    }
  }
  if(queue.length) {
    queue.slice(0, -1).map(function(x) {
      API.get({page: oldList[x]}, cbWrap(existenceCheck, oldList[x], false));
    });
    API.get({page: oldList[queue[queue.length - 1]]}, cbWrap(existenceCheck, oldList[queue[queue.length - 1]], true));
  }
  setTimeout(function(){
    API.getcategory({category: "Category:All articles proposed for deletion"}, function(success, result){
      oldList = newList;
      if(!success) {
        error(result.info);
        trackDeprods(oldList);
      } else {
        trackDeprods(result.members.map(function(a){return a.title;}));
      }
    });
  }, (newList.length < 1 && oldList.length < 1) ? 0 : 1800000);
}

trace(">> Authenticating to the wiki");
API.login({username: settings.username, password: settings.password}, function(success, result) {
  if(!success) {
    error(result.info);
  } else {
    trace(">> Getting last revision of list");
    API.get({page: settings.page}, function(success, result) {
      if(!success) {
        error(result.info);
      } else {
        trace(">> Splitting into lines");
        if(typeof result.content == "string") {
          deproddedPages = result.content.split("\n").slice(1);
        }
        trace(">> Now tracking additions/removals in the category");
        trackDeprods(oldList);
      }
    });
  }
});

PleaseStand (talk) 21:56, 25 June 2010 (UTC)[reply]