<?php
  //grab class
  include('Wikibot.php5');
  //get my password
  $fhpw = fopen("password2.txt", 'r') or die("can't open file");
  $password = fgets($fhpw);
  fclose($fhpw);
  //create bot
  $LivingBot = new Wikibot("LivingBot", $password, "en", "5", "5");
  $handle = fopen("livingbot_targets_books.txt", 'r') or die("can't open file");
  $count =0;
  $editcount = 0;
  while (!feof($handle)) {
      $targets[] = trim(fgets($handle));
      if($count++ == 300){
       break;
      }
  }
  fclose($handle);
  for ($i = 0; $i < count($targets); $i++) {
      if (stripos($LivingBot->get_page("User:LivingBot/shutoff"), "<!--Emergency shutoff-->") === false) {
          echo "Bot shut down.<br />";
          break;
      }
      echo "Editing page [[$targets[$i]]].<br />";
      $page = $LivingBot->get_page($targets[$i]);
      preg_match('/\{\{Infobox Book[^}]*\|(([ ]*)isbn([ ]*)=([ ]*)"?ISBN *([0-9]+)"?( *(\([^)]+\) *&? *)?"?ISBN *([0-9]+)"?)?)/i', $page,$matches);
      if(!isset($matches[0])){
      continue;
      }
      $new = $matches[2]."isbn".$matches[3]."=".$matches[4].$matches[5];
      if(isset($matches[6])){
           $new .= $matches[7]."<br />".$matches[8];
      }
      //Replace old with new
      $newpage = str_ireplace($matches[1], $new, $page);
      $newpage = str_ireplace("infobox book", "Infobox Book", $newpage);
      echo "<pre>".substr($newpage,0,strpos($newpage,"'''"))."</pre>";
      //Edit
      echo "Editing ".$trialcount;
      $LivingBot->edit_page($targets[$i], $newpage, "Bot correcting isbn parameter of {{Infobox Book}}. [[User_talk:LivingBot|Incorrect?]]",true,true);
  }
?>