changeset 73:947d53f95ccd

Refactor Sitemap into a separate class. Catch all exceptions in index.php and send a 500 error if nothing else catches it. Check response status before submitting to sitemap.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Fri, 12 Oct 2012 01:06:32 +0200
parents 7bc88fe62101
children 1d5166aba2c5
files Sitemap.inc StatusCodes.inc common-functions.inc index.php sitemap.php
diffstat 5 files changed, 118 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sitemap.inc	Fri Oct 12 01:06:32 2012 +0200
@@ -0,0 +1,97 @@
+<?
+include_once 'ScriptIncludeCache.inc';
+
+/// @cond
+$baseDir = dirname(__FILE__);
+$cache = ScriptIncludeCache::instance(__FILE__);
+$cache->includeOnce('common-functions.inc', $baseDir);
+$cache->includeOnce('Options.inc', dirname(__FILE__));
+/// @endcond
+
+class Sitemap
+{
+  private $master;
+  private $options;
+
+  function __construct($path) {
+    $this->master = new DOMDocument();
+    $this->master->load($path);
+
+    $this->options = new Options($this->master);
+  }
+
+  function genPage() {
+    /// The final output variable
+    $out = '<?xml version="1.0" encoding="UTF-8"?>';
+    $out .= "\n";
+    $out .= '<urlset
+      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
+      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
+            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
+';
+
+    $base = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
+    $base = substr($base, 0, strrpos($base, '/'));
+
+    $acceptedLanguages = $this->options->getAcceptedLanguages();
+
+    foreach($this->options->getAcceptedLanguages() as $lang) {
+      if ($handle = opendir("${lang}")) {
+	while (false !== ($entry = readdir($handle))) {
+	  if (endsWith($entry, '.xml')) {
+	    $fentry = "${lang}/${entry}";
+	    $doc = new DOMDocument();
+
+	    if (file_exists($fentry)) {
+	      $doc->load($fentry);
+
+	      $opts = array();
+	      if (count($acceptedLanguages) > 1) {
+		$opts['lang'] = $lang;
+	      }
+
+	      $toplevel = $doc->getElementsByTagName("toplevel");
+
+	      if($toplevel->length) {
+		$name = substr($entry, 0, -4);
+
+		if ($name != $this->options->getInputDefault('name')) {
+		  $opts['name'] = $name;
+		}
+
+		$optstring = opttostring($opts);
+
+		$location = "${base}/${optstring}";
+		$headers = getHeaders($location);
+
+		$location = htmlentities($location);
+
+		$lastmod = $headers["Last-Modified"];
+
+		$n = StatusCodes::codeFromHeader($headers['']);
+
+		if ($n == StatusCodes::HTTP_OK) {
+		  $lastmod = strtotime($lastmod);
+		  $lastmod = date(DateTime::W3C, $lastmod);
+
+		  $out .= "<url>\n";
+		  $out .= "<loc>${location}</loc>\n";
+		  $out .= "<lastmod>${lastmod}</lastmod>\n";
+		  $out .= "</url>\n";
+		}
+	      }
+	    }
+	  }
+	}
+	closedir($handle);
+      }
+    }
+
+    $out .= '</urlset>';
+
+    header('Content-type: application/xml');
+    print $out;
+  }
+}
+?>
--- a/StatusCodes.inc	Thu Oct 11 23:47:12 2012 +0200
+++ b/StatusCodes.inc	Fri Oct 12 01:06:32 2012 +0200
@@ -292,5 +292,16 @@
       && // and not 304 NOT MODIFIED
       $code != self::HTTP_NOT_MODIFIED;
   }
+
+  /**
+   *
+   */
+  public static function codeFromHeader($header)
+  {
+    $matches = array();
+    preg_match('/HTTP\/\S+\s(\d+)/', $header, $matches);
+    $n = $matches[1];
+    return $n;
+  }
 }
 ?>
\ No newline at end of file
--- a/common-functions.inc	Thu Oct 11 23:47:12 2012 +0200
+++ b/common-functions.inc	Fri Oct 12 01:06:32 2012 +0200
@@ -198,7 +198,9 @@
 {
   $response = http_head($url, array("timeout" => 1), $info);
   $headers = array();
+  $response = trim($response);
   $str = explode("\n", $response);
+  $headers[''] = trim($str[0]);
   foreach($str as $kv) {
     $p = strpos($kv, ":");
     if ($p) {
--- a/index.php	Thu Oct 11 23:47:12 2012 +0200
+++ b/index.php	Fri Oct 12 01:06:32 2012 +0200
@@ -22,9 +22,14 @@
   var_dump($_SERVER);
 }
 
+try {
 $input = new InputParser(basePath() . "/master.xml", $cache);
 
 $input->genPage();
+}
+catch (Exception $e) {
+  errorPage($e, StatusCodes::HTTP_INTERNAL_SERVER_ERROR);
+}
 /// @endcond
 
 ?>
\ No newline at end of file
--- a/sitemap.php	Thu Oct 11 23:47:12 2012 +0200
+++ b/sitemap.php	Fri Oct 12 01:06:32 2012 +0200
@@ -2,86 +2,16 @@
 /**
  * @file
  * Generates a sitemap
- * @todo reuse functionality from InputParser
  */
 
-/// The final output variable
-$out = '<?xml version="1.0" encoding="UTF-8"?>';
-$out .= "\n";
-$out .= '<urlset
-      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
-      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
-            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
-';
 include_once 'ScriptIncludeCache.inc';
 
 /// @cond
 $baseDir = dirname(__FILE__);
 $cache = ScriptIncludeCache::instance(__FILE__);
-$cache->includeOnce('common-functions.inc', $baseDir);
-$cache->includeOnce('Options.inc', dirname(__FILE__));
+$cache->includeOnce('Sitemap.inc', dirname(__FILE__));
 /// @endcond
 
-$master = new DOMDocument();
-$master->load("master.xml");
-
-$options = new Options($master);
-
-$base = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
-$base = substr($base, 0, strrpos($base, '/'));
-
-$acceptedLanguages = $options->getAcceptedLanguages();
-
-foreach($options->getAcceptedLanguages() as $lang) {
-  if ($handle = opendir("${lang}")) {
-    while (false !== ($entry = readdir($handle))) {
-      if (endsWith($entry, '.xml')) {
-	$fentry = "${lang}/${entry}";
-	$doc = new DOMDocument();
-
-	if (file_exists($fentry)) {
-	  $doc->load($fentry);
-
-	  $opts = array();
-	  if (count($acceptedLanguages) > 1) {
-	    $opts['lang'] = $lang;
-	  }
-
-	  $toplevel = $doc->getElementsByTagName("toplevel");
-
-	  if($toplevel->length) {
-	    $name = substr($entry, 0, -4);
-
-	    if ($name != $options->getInputDefault('name')) {
-	      $opts['name'] = $name;
-	    }
-
-	    $optstring = opttostring($opts);
-
-	    $location = "${base}/${optstring}";
-	    $headers = getHeaders($location);
-
-	    $location = htmlentities($location);
-
-	    $lastmod = $headers["Last-Modified"];
-	    $lastmod = strtotime($lastmod);
-	    $lastmod = date(DateTime::W3C, $lastmod);
-
-	    $out .= "<url>\n";
-	    $out .= "<loc>${location}</loc>\n";
-	    $out .= "<lastmod>${lastmod}</lastmod>\n";
-	    $out .= "</url>\n";
-	  }
-	}
-      }
-    }
-    closedir($handle);
-  }
-}
-
-$out .= '</urlset>';
-
-header('Content-type: application/xml');
-print $out;
+$sitemap = new Sitemap("master.xml");
+$sitemap->genPage();
 ?>