<?php
  //grab class
  include('Wikibot.php5');

  //get my password
  $fhnext = fopen("password2.txt", 'r') or die("can't open file");
  $password = fgets($fhnext);
  fclose($fhnext);

  //create bot
  $LivingBot = new Wikibot("LivingBot", $password, "en", "5", "5");
  
  //open file to pick up where we left off
  $fhnext = fopen("next.txt", 'r') or die("can't open file");
  $start = fgets($fhnext);
  fclose($fhnext);

  //grab list of pages in category
  $pages = $LivingBot->category("Year of birth missing (living people)", 100, "all", "", "$start");
  //the next file is tacked onto the end: get it off, store write to file
 $newstart = array_pop($pages);
 if(strlen($newstart) < 50){
  $fhnewnext = fopen("next.txt", 'w') or die("can't open file");
  fwrite($fhnewnext, $newstart);
  fclose($fhnewnext);
}

  $dead = "";
  $log = "";

  $none =  true;
  for ($i = 0; $i < count($pages); $i++) {
      if (strpos($pages[$i], " and ") === false && strrpos($pages[$i], "The ") !== 0 && strrpos($pages[$i], "brothers") === false && strpos($pages[$i], "sisters") === false) {
          $score = 0;
          $log .= "\nReading $pages[$i]:";
          $page = $LivingBot->get_page($pages[$i]);
          if (preg_match('/\[\[Category:([12][0-9]{3}) [dD]eaths\]\]/i', $page)) {
              $log .= "Matched deaths category. \n";
              $score += 2;
          }
      if(strpos($page, "'''") !== false){
        $page =  substr($page, (strpos($page, "'''") + 3));
          }
          if ((strpos($page, "(") - 20)  > strpos($page, "'''")) {
              $brackets = false;
          }
          $brackets = true;
          if (strpos($page, " or ")  > strpos($page, "(") && strpos($page, " or ") < strpos($page, ")")) {
              $brackets = false;
          }
          if (strpos($page, "[") !== false && strpos($page, "[") < strpos($page, "(")) {
              $brackets = false;
          }
      if (strpos($page, "==") !== false && strpos($page, "==") < strpos($page, ")")) {
              $brackets = false;
          }
      if($brackets){
          $firstbit = substr($page, 0, (strpos($page, ")") + 2));
          if (preg_match('/[(][^()<{]*([12][0-9]{3})[^0-9][^()<{\/&]*([12][0-9]{3})[^0-9]?[)]/i', $firstbit,$matches)) {
              if($matches[1] != $matches[2]){
               if((intval($matches[2]) - 5)  > intval($matches[1])){
                 $log .= "Found bracketed birth and death:\n";
                 $log .= $matches[1] . " and " . $matches[2] . " respectively.\n";
                 $score += 8;              
             }
            }
          } else {
              if (preg_match('/\([^()<{]*[12][0-9]{3}[^()0-9<{]*\)/i', $firstbit)) {
                  $log .= "Found only bracketed birth.\n";
                  $score -= 7;
              }
          }
          } else if(strpos($page, "(") !== false){
        $log .= "Brackets found but discarded.\n";
          }
           
          if (preg_match('/death_date[ ]*=[^|}]*([12][0-9]{3})/i', $page)) {
              $log .= "Found untemplated death date.\n";
              $score += 10;
          } else {
              if (stripos($page, "{{infobox") !== false && stripos($page, "death_date") === false) {
                  $log .= "Infobox missing death date.\n";
                  $score -= 9;
              }
          }
          
          if ($score > 0) {
              $dead .= "<a href=\"http://en.wikipedia.org/wiki/$pages[$i]\">$pages[$i]</a>\n";
          }
      }
  }

  //Ammend list of possibilities
  $fhdead = fopen("dead.html", 'a') or die("can't open file");
  if($dead == ""){
    $dead = "<!--None found, continuing to $newstart-->\n";
  }
  fwrite($fhdead, $dead);
  fclose($fhdead);

  //Ammend log
  $fhdead = fopen("log.txt", 'a') or die("can't open file");
  fwrite($fhdead, $log);
  fclose($fhdead);   

  header('location:http://www.foundation-flash.com/wikibot/LivingBot.php5')
?>