<?php
  //grab class
  include('BasicBot.php5');
  //create bot
  $LivingBot = new phpwikibot("LivingBot", $password, "en", "5", "5");
  $handle = fopen("livingbot_targets.txt", 'r') or die("can't open file");
  $maxnum;
  while (!feof($handle)) {
      $targets[] = trim(fgets($handle));
      if (count($targets) == $maxnum) {
          //No point loading more than we need.
          break;
      }
  }
  fclose($handle);
  for ($i = 0; $i < 25; $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]);
      if (stripos($page, "{{lifetime") !== false) {
          //This should never happen, so avoid if it does.
          continue;
      }
      if (stripos($page, "Category:Living people") !== false) {
          //It's been updated in the mean time
          continue;
      }
      $ontoendof = "[[Category:Year of birth missing (living people)]]";
      $point = stripos($page, $ontoendof);
      if ($point !== false) {
          //Nice easy one
          $point += strlen($ontoendof);
          $newpage = insert($page, "\n[[Category:Living people]]", $point);
          $LivingBot->put_page($targets[$i], $pages, $newpage, "Bot adding category ''Living people''. [[User_talk:LivingBot|Incorrect?]]");
      } else {
          //Probably uses [[Cat: ... | Name]] structure 
          if (preg_match("/Category:Year of birth missing.*\]\]/i", $page, $matches)) {
              $point = stripos($page, $matches[0]) + strlen($matches[0]);
              $newpage = insert($page, "\n[[Category:Living people]]", $point);
              $LivingBot->put_page($targets[$i], $pages, $newpage, "Bot adding category ''Living people''. [[User_talk:LivingBot|Incorrect?]]");
          }
      }
  }
  function insert($page, $text, $point)
  {
      $new = substr($page, 0, $point) . $text . substr($page, $point);
      return $new;
  }
?>