changeset 59:0e157721bbad

Change the way we parse input files, preparing for more reasonable caching strategy.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 11 Oct 2012 16:40:03 +0200
parents ba5afd9ff24e
children 63ea1cfd387d
files index.php inputParser.inc
diffstat 2 files changed, 119 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Thu Oct 11 13:04:53 2012 +0200
+++ b/index.php	Thu Oct 11 16:40:03 2012 +0200
@@ -59,6 +59,7 @@
     $parent->removeChild($param);
   }
 }
+$master = getFiles($master, $options);
 
 if (CACHING && $cacheable)
   $options->getCache()->CheckHttpModified();
--- a/inputParser.inc	Thu Oct 11 13:04:53 2012 +0200
+++ b/inputParser.inc	Thu Oct 11 16:40:03 2012 +0200
@@ -33,95 +33,79 @@
   return $param_value;
 }
 
-function getInput($master, $param, $options)
-{
-  $out = '';
-
+function getFiles($doc, $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);
+  $toRemove = array();
 
-  $toplevel = $doc->getElementsByTagName("toplevel");
-
-  if(! $toplevel->length) {
-    errorPage("Resource '${conf}' is not available", 500);
+  $topLevelTags = $doc->getElementsByTagName("toplevel");
+  foreach ($topLevelTags as $topLevel) {
+    $topLevel->parentNode->removeChild($topLevel);
   }
 
-  $includes = $doc->getElementsByTagName("include");
-  $recurse = 0;
-
-  while($includes->length>0) {
-    if(++$recurse > MAX_RECURSE) {
-      errorPage('Recursion limit exceeded', 500);
+  $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;
     }
-    foreach ($includes as $include) {
-	$src = $include->getAttribute("src");
-	$subdoc = new DOMDocument();
-	$subfile = $options->getBasePath() . "/${lang}/${src}";
-	$subdoc->load("$subfile");
-	$options->getCache()->cache_time($subfile);
-	$parent = $include->parentNode;
-	$xml = getElementByTagName($subdoc,"xml");
-	foreach($xml->childNodes as $child) {
-	  $text = $subdoc->saveXml($child);
-	  $clonedChild = $doc->importNode($child,true);
-	  $parent->insertBefore($clonedChild,$include);
-	}
-	$parent->removeChild($include);
+    else {
+      $value = $setTag->getAttribute("value");
+      if ($key && $value) {
+	$valueDict[$key] = $value;
+      }
     }
-    $includes = $doc->getElementsByTagName("include");
+    //We need to iterate in the opposite direction when removing,
+    //so best shifting.
+    array_unshift($toRemove, $setTag);
   }
 
-  $head = getElementByTagName($doc, "head");
-  $title = $head->getAttribute("title");
+  $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();
 
-  if($title) {
-    $values=$master->getElementsByTagName("param");
-    foreach ($values as $value) {
-      if ($value->getAttribute("type")=="input_config") {
-        if ($value->getAttribute("id")=="title") {
-          $tmp = new DOMDocument();
-          $tmp->loadXml("<xml>${title}</xml>");
-	  $parent=$value->parentNode;
-	  $parent->removeChild($value);
-	  $parent->appendChild(new DOMText($tmp->textContent));
-        }
+	$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);
       }
     }
   }
 
-  $css = getElementByTagName($head,"css");
-  $css = $doc->saveXML($css);
-  $css = preg_replace('/\s*<\/?\s*css\s*>\s*/s', '', $css);
-
-  if($css) {
-    $values = $master->getElementsByTagName("param");
-    foreach ($values as $value) {
-      if ($value->getAttribute("type") == "input_config") {
-        if ($value->getAttribute("id") == "css") {
-          $tmp = new DOMDocument();
-          $tmp->loadXml("<xml>${css}</xml>");
-	  $parent=$value->parentNode;
-	  foreach($tmp->firstChild->childNodes as $node) {
-		$clonedChild=$master->importNode($node,true);
-		$parent->insertBefore($clonedChild,$value);
-          }
-	  $parent->removeChild($value);
-        }
-      }
-    }
+  foreach($toRemove as $param) {
+    $parent = $param->parentNode;
+    $parent->removeChild($param);
   }
 
+  $body = getElementByTagName($doc,"body");
+  $files = $body->getElementsByTagName("file");
 
-  $body=getElementByTagName($doc,"body");
-  $files=$body->getElementsByTagName("file");
+  $toRemove = array();
 
   foreach ($files as $file) {
     $script=$file->getAttribute("script");
@@ -149,7 +133,7 @@
       errorPage("Resource not found '${lang}/${src}'");
     }
 
-    $filters=$file->getElementsByTagName("filter");
+    $filters = $file->getElementsByTagName("filter");
     foreach($filters as $filter) {
       $func = $filter->getAttribute("function");
       $params = $filter->getElementsByTagName("param");
@@ -167,13 +151,71 @@
       $callString .= ");";
       eval($callString);
     }
-    $out.= $file_content;
-  }
+    $ndoc = new DOMDocument();
+
+    $ndoc->loadXml("<xml>${file_content}</xml>");
 
-  $doc = new DOMDocument();
-  $doc->loadXml("<xml>${out}</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);
+  }
+  foreach($toRemove as $param) {
+    $parent = $param->parentNode;
+    $parent->removeChild($param);
+  }
 
   return $doc;
 }
 
+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);
+
+  $toplevel = $doc->getElementsByTagName("toplevel");
+
+  if(! $toplevel->length) {
+    errorPage("Resource '${conf}' is not available", 500);
+  }
+
+  $includes = $doc->getElementsByTagName("include");
+  $recurse = 0;
+
+  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}";
+	$subdoc->load("$subfile");
+	$options->getCache()->cache_time($subfile);
+	$parent = $include->parentNode;
+	$xml = getElementByTagName($subdoc,"xml");
+	foreach($xml->childNodes as $child) {
+	  $text = $subdoc->saveXml($child);
+	  $clonedChild = $doc->importNode($child,true);
+	  $parent->insertBefore($clonedChild,$include);
+	}
+	$parent->removeChild($include);
+    }
+    $includes = $doc->getElementsByTagName("include");
+  }
+
+  return $doc;
+}
 ?>
\ No newline at end of file