Jump to content

User:Luasóg bot/scripts/ipn

From Wikipedia, the free encyclopedia
var my_bot = new Luasog("http://wiki.riteme.site/w/api.php");my_bot.speed = 6000;
my_bot.byeline = " (bot: [[User:Luasóg|Luasóg]])";

var listofpages = new Array();
function readcats(success, result){
  if (!success) { kill(result.info); return false; }
  
  var listofcats = result.bynamespace["14"];
  
  trace(listofcats.length + " categories found. Adding pages.");
  
  var preppages = function(success, result){
    if (!success) { kill(result.info); return false; }
    
    for (var category in result.bynamespace["0"])
      listofpages.push(result.bynamespace["0"][category].title);
    
    trace(listofpages.length + " pages added. " + listofcats.length + " categories to go.");
    
    if (listofcats[0] !== undefined)
      my_bot.getcategory({category:listofcats.shift().title}, preppages);
    else
      shiftapage();
  };
  
  my_bot.getcategory({category:listofcats.shift().title}, preppages);      
}

var currentpage = "";
var numberdone = 0;
function shiftapage(){
  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;
  
  // replace first instance of derive template with IPN template
  if (content == result.content)
    content = content.replace(/\{\{derive\|Irish\|/i, "{{Irish place name|");
  // replace first instance of lang-ga template with IPN template
  if (content == result.content)
    content = content.replace(/\{\{lang-ga\|/i, "{{Irish place name|");
  // replace first instance of lang-gle template with IPN template
  if (content == result.content)
    content = content.replace(/\{\{lang-gle\|/i, "{{Irish place name|");
  
  // replace first instance of ([[Irish language|Irish]: XXX) template with IPN template
  var match = content.match(/\(\[\[Irish language[^\]]*\]\]:? '+[\w\sáéóíú]+'+/i);
  if (match !== null) {
    match = match[0];
    var replace = match.replace(/\(\[\[Irish language[^\]]*\]\]:? '+/i, "({{Irish place name|");
    replace = replace.replace(/'+/i, "}}");
    if (content == result.content)
      content = content.replace(match, replace);
  }
  
  // match first instance of (XXX in [[Irish language|Irish]]) with IPN template
  match = content.match(/\('*[\w\sáéóíú]+'* in \[\[Irish[^\]]*\]\]*/i);
  if (match !== null) {
    match = match[0];
    var replace = match.replace(/\('*/i, "");
    replace = replace.replace(/'* in \[\[Irish[^\]]*\]\]/i, "");
    replace = "({{Irish place name|"+replace+"}}";
    if (content == result.content)
      content = content.replace(match, replace);
  }
  
  // catch "meaning XXX" constructs preceeded by IPN template
  match = content.match(/\(\{\{Irish place name\|[\w\sáéóíú]+\}\}[, ]+meaning ["']*[^"')]+["']*\)/i);
  if (match !== null) {
    match = match[0];
    var replace = match.replace(/\}\}[, ]+meaning ["']*/i, "|");
    replace = replace.replace(/["']*\)/i, "}})");
    if (content == result.content)
      content = content.replace(match, replace);
  }
  
  var summary = "updating place name templates";
  
  // if no changes were made then add to the holding category (if not already in it)
  if (!/(\{\{Irish place name)|(\{\{lga)|(\{\{Irish language place name)|(\{\{Gaeltacht place name)|(\[\[Category:Articles on towns and villages in Ireland possibly missing Irish place names)/i.test(content)) {
    summary = "adding page to temporary category";
    content = content + "\n[[Category:Articles on towns and villages in Ireland possibly missing Irish place names]]";
  }
  
  if (!allowBots(result.content, "Luasóg")){
    listofpages.shift();
    numberdone++;
    trace("Bot not allowed on " + currentpage + " ("+numberdone+")");
    shiftapage();
  } else if (content == result.content) {
    listofpages.shift();
    numberdone++;
    trace("No change for " + currentpage + " ("+numberdone+")");
    shiftapage();
  } else {
    my_bot.post({page:currentpage, 
                 content:content, 
                 summary:summary,
                 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 {
                     numberdone++;
                     listofpages.shift();
                     trace("Saved " + currentpage + " ("+numberdone+")");
                   }
                   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 categories.");
  my_bot.getcategory({category:"Category:Towns and villages in the Republic of Ireland by county"}, readcats);
});