User:Luasóg bot/scripts/mainbelt

var my_bot = new Luasog("http://en.wikipedia.org/w/api.php");my_bot.speed = 6000;
my_bot.byeline = " (bot: [[User:Luasóg_bot|Luasóg]])";

var listofpages = new Array();
var currentpage = undefined;
function shiftapage(){
  trace(listofpages.length+" pages to go.");
  if (listofpages.length > 0){
    currentpage = listofpages[0];
    my_bot.get({page:currentpage}, editpage);
  } else {
    trace("All done.");
    stop();
  }
}

function editpage(success, result){
  if (!success) { kill(result.info); return false; }
  
  var content = result.content;
  
  var match = content.match(/\w\s\[\[Main-belt Asteroid\]\]/i);
  if (match !== null) {
    match = match[0];
    var replace = match.replace(/\[\[Main-belt Asteroid\]\]/, "[[Asteroid belt|main-belt]] [[asteroid]]");
    content = content.replace(match, replace);
  }

  if (!allowBots(result.content, "Luasóg")){
    trace("Bots not allowed on " + currentpage);
    listofpages.shift();
    shiftapage();
  } else if (content == result.content) {
    trace("No change for " + currentpage);
    listofpages.shift();
    shiftapage();
  } else {
    content = content.replace(/\{\{MinorPlanets Navigator\}\}/gi, "{{Minor planets navigator}}");
    content = content.replace(/\{\{MinorPlanets Footer\}\}/gi, "{{Small Solar System bodies}}");
    my_bot.post({page:currentpage, 
                 content:content, 
                 summary:"automatically fixing a typo: \"[[Main-belt Asteroid]]\" -> \"[[Asteroid belt|main-belt]] [[asteroid]]\"",
                 timestamp:result.timestamp}, function(success, result){
                   if (!success && "editconflict" != result.code) { kill(result.info); return false; }
                   if ("editconflict" == result.code) {
                     trace("Edit conflict on " + currentpage);
                   } else {
                     trace("Saved " + currentpage);
                     listofpages.shift();
                   }
                   shiftapage();
                 });
  }
}

function allowBots(text, user){
  if (!new RegExp("\\{\\{\\s*(nobots|bots[^}]*)\\s*\\}\\}", "i").test(text)) return true;
  return (new RegExp("\\{\\{\\s*bots\\s*\\|\\s*deny\\s*=\\s*([^}]*,\\s*)*"+user+"\\s*(?=[,\\}])[^}]*\\s*\\}\\}", "i").test(text)) ? false : new RegExp("\\{\\{\\s*((?!nobots)|bots(\\s*\\|\\s*allow\\s*=\\s*((?!none)|([^}]*,\\s*)*"+user+"\\s*(?=[,\\}])[^}]*|all))?|bots\\s*\\|\\s*deny\\s*=\\s*(?!all)[^}]*|bots\\s*\\|\\s*optout=(?!all)[^}]*)\\s*\\}\\}", "i").test(text);
}

trace("Loggin in..");
my_bot.login({username:"luasóg_bot", password:"****"}, function(success, result){
  if (!success) { kill(result.info); return false; }
  trace("Getting list of backlinks...");
  my_bot.getbacklinks({page:"Main-belt Asteroid"}, function(success, result){
    if (!success) { kill(result.info); return false; }
    
    trace("Got pages.");
    for (var link in result.bynamespace[0]) listofpages.push(result.bynamespace[0][link]);
    
    shiftapage();
  });
});