changeset 62:b7efe2ecbc11

Wrapped everything in inputParser in a class.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 11 Oct 2012 18:21:59 +0200
parents 13d899b748b7
children 92c3e52c12d4
files Options.inc index.php inputParser.inc sitemap.php
diffstat 4 files changed, 240 insertions(+), 211 deletions(-) [+]
line wrap: on
line diff
--- a/Options.inc	Thu Oct 11 17:19:13 2012 +0200
+++ b/Options.inc	Thu Oct 11 18:21:59 2012 +0200
@@ -16,6 +16,7 @@
   private $basePath;
   private $flagUrl;
   private $baseUrl;
+  private $cacheable;
 
   /**
    * Gets the default language
@@ -37,6 +38,16 @@
     return $this->lang;
   }
 
+  function setCacheable($cacheable)
+  {
+    $this->cacheable = $cacheable;
+  }
+
+  function getCacheable()
+  {
+    return $this->cacheable;
+  }
+
   /**
    * Get the base url, or if non set, extracts it from _SERVER
    *
--- a/index.php	Thu Oct 11 17:19:13 2012 +0200
+++ b/index.php	Thu Oct 11 18:21:59 2012 +0200
@@ -23,48 +23,9 @@
   var_dump($_SERVER);
 }
 
-$master = new DOMDocument();
 $masterName = basePath() . "/master.xml";
-$cache->cache_time($masterName);
-$master->load($masterName);
-
-$options = new Options($master);
-$options->setCache($cache);
-$options->setBasePath(basePath());
-
-$options->setUrlParams(array('name', 'lang'));
-
-if(array_key_exists('lang', $_GET) && $_GET['lang']) {
-  $options->setLang($_GET['lang']);
-}
-else {
-  $options->setLang($options->getDefaultLang());
-}
-
-if(array_key_exists('name', $_GET) && $_GET['name']) {
-  $options->setName($_GET['name']);
-}
 
-$params = $master->getElementsByTagName("param");
-
-foreach ($params as $param) {
-  if ($param->getAttribute("type") == "input") {
-    $doc = getInput($master, $param, $options);
+$input = new InputParser($masterName, $cache);
 
-    $parent = $param->parentNode;
-    foreach ($doc->firstChild->childNodes as $child) {
-      $clonedChild = $master->importNode($child, true);
-      $parent->insertBefore($clonedChild, $param);
-    }
-    $parent->removeChild($param);
-  }
-}
-$master = getFiles($master, $options);
-
-if (CACHING && $cacheable)
-  $options->getCache()->CheckHttpModified();
-
-print $master->saveXml($master);
-//print_r($cache->cacheSet(1));
-
+$input->genPage();
 ?>
\ No newline at end of file
--- a/inputParser.inc	Thu Oct 11 17:19:13 2012 +0200
+++ b/inputParser.inc	Thu Oct 11 18:21:59 2012 +0200
@@ -3,202 +3,258 @@
  * @file
  * Functionality for translating an XML document into a webpage
  */
-function getParam($param)
-{
-  $param_type=$param->getAttribute("type");
-  $param_value;
-  if (!$param_type)
-    $param_type="scalar";
+class InputParser {
+  private $options;
+  private $master;
+
+  function __construct($name, $masterCache) {
+    $this->master = new DOMDocument();
+    $cache = $masterCache;
+    $cache->cache_time($name);
+    $this->master->load($name);
 
-  if($param_type == "scalar") {
-    $param_subst=$param->getAttribute("subst");
-    $param_value=$param->getAttribute("value");
-    if ($param_subst) {
-      /*
-      $param_value=preg_replace("/name/", $name, $param_subst);
-      $param_value=preg_replace('/lang/', $lang, $param_value);
-      */
+    $this->options = new Options($this->master);
+    $this->options->setCache($cache);
+    $this->options->setBasePath(basePath());
+    $this->options->setCacheable(true);
+
+    $this->options->setUrlParams(array('name', 'lang'));
+
+    if(array_key_exists('lang', $_GET) && $_GET['lang']) {
+      $this->options->setLang($_GET['lang']);
     }
-  }
-  elseif($param_type == "array") {
-	  $params=$param->getElementsByTagName("param");
-	  $param_value=array();
-	  foreach ($param->childNodes as $param) {
-	    if ($param->nodeType == XML_ELEMENT_NODE)
-	    {
-	      array_push($param_value, getParam($param));
-	    }
-	  }
-  }
-  return $param_value;
-}
+    else {
+      $this->options->setLang($this->options->getDefaultLang());
+    }
+
+    if(array_key_exists('name', $_GET) && $_GET['name']) {
+      $this->options->setName($_GET['name']);
+    }
+
+    $params = $this->master->getElementsByTagName("param");
 
-function getFiles($doc, $options) {
-  $lang = $options->getLang();
-  $conf = $options->getName();
+    foreach ($params as $param) {
+      if ($param->getAttribute("type") == "input") {
+	$doc = self::getInput($this->master, $param, $this->options);
 
-  $toRemove = array();
+	$parent = $param->parentNode;
+	foreach ($doc->firstChild->childNodes as $child) {
+	  $clonedChild = $this->master->importNode($child, true);
+	  $parent->insertBefore($clonedChild, $param);
+	}
+	$parent->removeChild($param);
+      }
+    }
+    $this->master = self::getFiles($this->master, $this->options);
 
-  $topLevelTags = $doc->getElementsByTagName("toplevel");
-  foreach ($topLevelTags as $topLevel) {
-    $topLevel->parentNode->removeChild($topLevel);
   }
 
-  $valueDict = array();
-  $fragments = array();
-  $setters = $doc->getElementsByTagName("set");
-  foreach ($setters as $setTag) {
-    $key = $setTag->getAttribute("id");
-    $type = $setTag->getAttribute("type");
-    if ($type == "fragment") {
-      $fragments[$key] = $setTag;
-    }
-    else {
-      $value = $setTag->getAttribute("value");
-      if ($key && $value) {
-	$valueDict[$key] = $value;
-      }
-    }
-    //We need to iterate in the opposite direction when removing,
-    //so best shifting.
-    array_unshift($toRemove, $setTag);
+  function genPage()
+  {
+    if (CACHING && $this->options->getCacheable())
+      $this->options->getCache()->CheckHttpModified();
+
+    print $this->master->saveXml($this->master);
+    //print_r($cache->cacheSet(1));
   }
 
-  $params = $doc->getElementsByTagName("param");
-  foreach ($params as $param) {
-    if ($param->getAttribute("type")=="input_config") {
-      $id = $param->getAttribute("id");
-      if (array_key_exists($id, $valueDict)) {
-	$value = $valueDict[$id];
-	$tmp = new DOMDocument();
+
+  function getParam($param)
+  {
+    $param_type=$param->getAttribute("type");
+    $param_value;
+    if (!$param_type)
+      $param_type="scalar";
 
-	$tmp->loadXml("<xml>${value}</xml>");
-	$parent = $param->parentNode;
-	$parent->insertBefore(new DOMText($tmp->textContent), $param);
-	//We need to iterate in the opposite direction when removing,
-	//so best shifting.
-	array_unshift($toRemove, $param);
-      }
-      elseif (array_key_exists($id, $fragments)) {
-	$fragment = $fragments[$id];
-
-	$cloneFragment = $fragment->cloneNode(true);
-	$insNode = $param;
-	for ($i = $cloneFragment->childNodes->length - 1; $i >= 0; $i--) {
-	  $child = $cloneFragment->childNodes->item($i);
-	  $child = $child->parentNode->removeChild($child);
-	  $insNode = $insNode->parentNode->insertBefore($child, $insNode);
-	}
-
-	//We need to iterate in the opposite direction when removing,
-	//so best shifting.
-	array_unshift($toRemove, $param);
+    if($param_type == "scalar") {
+      $param_subst=$param->getAttribute("subst");
+      $param_value=$param->getAttribute("value");
+      if ($param_subst) {
+	/*
+	  $param_value=preg_replace("/name/", $name, $param_subst);
+	  $param_value=preg_replace('/lang/', $lang, $param_value);
+	*/
       }
     }
+    elseif($param_type == "array") {
+      $params=$param->getElementsByTagName("param");
+      $param_value=array();
+      foreach ($param->childNodes as $param) {
+	if ($param->nodeType == XML_ELEMENT_NODE)
+	  {
+	    array_push($param_value, self::getParam($param));
+	  }
+      }
+    }
+    return $param_value;
   }
 
-  foreach($toRemove as $param) {
-    $parent = $param->parentNode;
-    $parent->removeChild($param);
-  }
+  function getFiles($doc, $options) {
+    $lang = $options->getLang();
+    $conf = $options->getName();
 
-  $body = getElementByTagName($doc,"body");
-  $files = $body->getElementsByTagName("file");
+    $toRemove = array();
+
+    $topLevelTags = $doc->getElementsByTagName("toplevel");
+    foreach ($topLevelTags as $topLevel) {
+      $topLevel->parentNode->removeChild($topLevel);
+    }
 
-  $toRemove = array();
-
-  foreach ($files as $file) {
-    $script=$file->getAttribute("script");
-    if ($script) {
-      $cacheable = false;
-      $src="";
-      $cwd = getcwd();
+    $valueDict = array();
+    $fragments = array();
+    $setters = $doc->getElementsByTagName("set");
+    foreach ($setters as $setTag) {
+      $key = $setTag->getAttribute("id");
+      $type = $setTag->getAttribute("type");
+      if ($type == "fragment") {
+	$fragments[$key] = $setTag;
+      }
+      else {
+	$value = $setTag->getAttribute("value");
+	if ($key && $value) {
+	  $valueDict[$key] = $value;
+	}
+      }
+      //We need to iterate in the opposite direction when removing,
+      //so best shifting.
+      array_unshift($toRemove, $setTag);
+    }
 
-      $matches=array();
-      preg_match('/(.*\/)/', $script, $matches);
-      $dirname=$matches[0];
-      preg_match('/([^\/]*)$/', $script, $matches);
-      $filename=$matches[0];
-      chdir("${lang}/${dirname}");
-      $pipe=popen("php ${filename}","r");
-      $file_content = stream_get_contents($pipe);
-      chdir("${cwd}");
+    $params = $doc->getElementsByTagName("param");
+    foreach ($params as $param) {
+      if ($param->getAttribute("type")=="input_config") {
+	$id = $param->getAttribute("id");
+	if (array_key_exists($id, $valueDict)) {
+	  $value = $valueDict[$id];
+	  $tmp = new DOMDocument();
+
+	  $tmp->loadXml("<xml>${value}</xml>");
+	  $parent = $param->parentNode;
+	  $parent->insertBefore(new DOMText($tmp->textContent), $param);
+	  //We need to iterate in the opposite direction when removing,
+	  //so best shifting.
+	  array_unshift($toRemove, $param);
+	}
+	elseif (array_key_exists($id, $fragments)) {
+	  $fragment = $fragments[$id];
+
+	  $cloneFragment = $fragment->cloneNode(true);
+	  $insNode = $param;
+	  for ($i = $cloneFragment->childNodes->length - 1; $i >= 0; $i--) {
+	    $child = $cloneFragment->childNodes->item($i);
+	    $child = $child->parentNode->removeChild($child);
+	    $insNode = $insNode->parentNode->insertBefore($child, $insNode);
+	  }
+
+	  //We need to iterate in the opposite direction when removing,
+	  //so best shifting.
+	  array_unshift($toRemove, $param);
+	}
+      }
     }
-    else {
-      $src = $file->getAttribute("src");
-      $fname = $options->getBasePath() . "/${lang}/${src}";
-      $file_content = $options->getCache()->loadFile($fname);
-    }
-    if(floatval($file_content)<0) {
-      errorPage("Resource not found '${lang}/${src}'");
+
+    foreach($toRemove as $param) {
+      $parent = $param->parentNode;
+      $parent->removeChild($param);
     }
 
-    $filters = $file->getElementsByTagName("filter");
-    foreach($filters as $filter) {
-      $func = $filter->getAttribute("function");
-      $params = $filter->getElementsByTagName("param");
-      $callString = "\$file_content = ${func}(\$file_content, \$options";
-      $param_values = array();
-      $i = 0;
-      foreach ($filter->childNodes as $param) {
-	if ($param->nodeType == XML_ELEMENT_NODE)
-	  {
-	    $param_value[$i] = getParam($param);
-	    $callString .= ",\$param_value[$i]";
-	    $i++;
-	  }
+    $body = getElementByTagName($doc,"body");
+    $files = $body->getElementsByTagName("file");
+
+    $toRemove = array();
+
+    foreach ($files as $file) {
+      $script = $file->getAttribute("script");
+      if ($script) {
+	$options->setCacheable(false);
+	$src="";
+	$cwd = getcwd();
+
+	$matches=array();
+	preg_match('/(.*\/)/', $script, $matches);
+	$dirname=$matches[0];
+	preg_match('/([^\/]*)$/', $script, $matches);
+	$filename=$matches[0];
+	chdir("${lang}/${dirname}");
+	$pipe=popen("php ${filename}","r");
+	$file_content = stream_get_contents($pipe);
+	chdir("${cwd}");
+      }
+      else {
+	$src = $file->getAttribute("src");
+	$fname = $options->getBasePath() . "/${lang}/${src}";
+	$file_content = $options->getCache()->loadFile($fname);
+      }
+      if(floatval($file_content)<0) {
+	errorPage("Resource not found '${lang}/${src}'");
       }
-      $callString .= ");";
-      eval($callString);
-    }
-    $ndoc = new DOMDocument();
-
-    $ndoc->loadXml("<xml>${file_content}</xml>");
 
-    $parent = $file->parentNode;
-    foreach ($ndoc->firstChild->childNodes as $child) {
-      $clonedChild = $doc->importNode($child, true);
-      $parent->insertBefore($clonedChild, $file);
+      $filters = $file->getElementsByTagName("filter");
+      foreach($filters as $filter) {
+	$func = $filter->getAttribute("function");
+	$params = $filter->getElementsByTagName("param");
+	$callString = "\$file_content = ${func}(\$file_content, \$options";
+	$param_values = array();
+	$i = 0;
+	foreach ($filter->childNodes as $param) {
+	  if ($param->nodeType == XML_ELEMENT_NODE)
+	    {
+	      $param_value[$i] = self::getParam($param);
+	      $callString .= ",\$param_value[$i]";
+	      $i++;
+	    }
+	}
+	$callString .= ");";
+	eval($callString);
+      }
+      $ndoc = new DOMDocument();
+
+      $ndoc->loadXml("<xml>${file_content}</xml>");
+
+      $parent = $file->parentNode;
+      foreach ($ndoc->firstChild->childNodes as $child) {
+	$clonedChild = $doc->importNode($child, true);
+	$parent->insertBefore($clonedChild, $file);
+      }
+      //We need to iterate in the opposite direction when removing,
+      //so best shifting.
+      array_unshift($toRemove, $file);
     }
-    //We need to iterate in the opposite direction when removing,
-    //so best shifting.
-    array_unshift($toRemove, $file);
-  }
-  foreach($toRemove as $param) {
-    $parent = $param->parentNode;
-    $parent->removeChild($param);
+    foreach($toRemove as $param) {
+      $parent = $param->parentNode;
+      $parent->removeChild($param);
+    }
+
+    return $doc;
   }
 
-  return $doc;
-}
+  function getInput($master, $param, $options)
+  {
+    $lang = $options->getLang();
+    $name = $param->getAttribute("id");
+    $conf = $options->getName();
+    if (!$conf)
+      $conf = $param->getAttribute("default");
 
-function getInput($master, $param, $options)
-{
-  $lang = $options->getLang();
-  $name = $param->getAttribute("id");
-  $conf = $options->getName();
-  if (!$conf)
-    $conf = $param->getAttribute("default");
+    $confFile = $options->getBasePath() . "/${lang}/${conf}.xml";
+    $options->getCache()->cache_time($confFile);
+    $doc = new DOMDocument();
+    $doc->load($confFile);
 
-  $confFile = $options->getBasePath() . "/${lang}/${conf}.xml";
-  $options->getCache()->cache_time($confFile);
-  $doc = new DOMDocument();
-  $doc->load($confFile);
+    $toplevel = $doc->getElementsByTagName("toplevel");
 
-  $toplevel = $doc->getElementsByTagName("toplevel");
+    if(! $toplevel->length) {
+      errorPage("Resource '${conf}' is not available", 500);
+    }
 
-  if(! $toplevel->length) {
-    errorPage("Resource '${conf}' is not available", 500);
-  }
+    $includes = $doc->getElementsByTagName("include");
+    $recurse = 0;
 
-  $includes = $doc->getElementsByTagName("include");
-  $recurse = 0;
-
-  while($includes->length > 0) {
-    if(++$recurse > MAX_RECURSE) {
-      errorPage('Recursion limit exceeded', 500);
-    }
-    foreach ($includes as $include) {
+    while($includes->length > 0) {
+      if(++$recurse > MAX_RECURSE) {
+	errorPage('Recursion limit exceeded', 500);
+      }
+      foreach ($includes as $include) {
 	$src = $include->getAttribute("src");
 	$subdoc = new DOMDocument();
 	$subfile = $options->getBasePath() . "/${lang}/${src}";
@@ -212,10 +268,11 @@
 	  $parent->insertBefore($clonedChild,$include);
 	}
 	$parent->removeChild($include);
+      }
+      $includes = $doc->getElementsByTagName("include");
     }
-    $includes = $doc->getElementsByTagName("include");
+
+    return $doc;
   }
-
-  return $doc;
 }
 ?>
\ No newline at end of file
--- a/sitemap.php	Thu Oct 11 17:19:13 2012 +0200
+++ b/sitemap.php	Thu Oct 11 18:21:59 2012 +0200
@@ -82,4 +82,4 @@
 
 header('Content-type: application/xml');
 print $out;
-?>
\ No newline at end of file
+?>