# HG changeset patch # User Tom Fredrik "BFG" Klaussen # Date 1349372771 -7200 # Node ID 7b19be62ea94319b9e2f7fff26ccd5ec7cccf8c0 # Parent 647b72603b7da469d75b8742acba06322eb4f8a0 Remove yet another global, replace by CacheTimeCheck class. diff -r 647b72603b7d -r 7b19be62ea94 Options.inc --- a/Options.inc Sun Sep 30 03:39:32 2012 +0200 +++ b/Options.inc Thu Oct 04 19:46:11 2012 +0200 @@ -6,6 +6,7 @@ private $name; private $acceptedLanguages = array(); private $inputDefaults = array(); + private $cache; function getDefaultLang() { @@ -32,6 +33,16 @@ return $this->name; } + function setCache($cache) + { + $this->cache = $cache; + } + + function getCache() + { + return $this->cache; + } + function getAcceptedLanguages() { return $this->acceptedLanguages; diff -r 647b72603b7d -r 7b19be62ea94 cache_check.inc --- a/cache_check.inc Sun Sep 30 03:39:32 2012 +0200 +++ b/cache_check.inc Thu Oct 04 19:46:11 2012 +0200 @@ -1,35 +1,47 @@ files, $filename); + $this->newest = $minTime; + $this->cache_time(__FILE__); + } - $gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; + function CheckHttpModified() + { + if (DEBUG) + var_dump($_SERVER); + + $HTTP_IF_MODIFIED_SINCE=$_SERVER['HTTP_IF_MODIFIED_SINCE']; + $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); - if ($if_modified_since == $gmdate_mod) { - header("HTTP/1.0 304 Not Modified"); - exit; + $gmdate_mod = gmdate('D, d M Y H:i:s', $this->newest) . ' GMT'; + + if ($if_modified_since == $gmdate_mod) { + header("HTTP/1.0 304 Not Modified"); + exit; + } + header("Last-Modified: $gmdate_mod"); } - header("Last-Modified: $gmdate_mod"); -} -function cache_time($path) -{ - global $newest; - $mtime = filemtime($path); - if ($mtime > $newest) { - $newest = $mtime; + function cache_time($path) + { + array_push($this->files, $path); + $mtime = filemtime($path); + if ($mtime > $this->newest) { + $this->newest = $mtime; + } + } + + function includeOnce($path) + { + $this->cache_time($path); + include_once($path); } } - -function include_with_mtime($path) -{ - cache_time($path); - include_once($path); -} - -cache_time(__FILE__); ?> \ No newline at end of file diff -r 647b72603b7d -r 7b19be62ea94 index.php --- a/index.php Sun Sep 30 03:39:32 2012 +0200 +++ b/index.php Thu Oct 04 19:46:11 2012 +0200 @@ -13,23 +13,24 @@ ini_set("display_errors", 1); } -$SCRIPT_FILENAME=$_SERVER['SCRIPT_FILENAME']; -$newest = filemtime($SCRIPT_FILENAME); $cacheable = true; include_once 'php/cache_check.inc'; -include_with_mtime('php/Options.inc'); -include_with_mtime('php/accept-language.inc'); -include_with_mtime('php/common-functions.inc'); -include_with_mtime('php/filters.inc'); -include_with_mtime('php/inputParser.inc'); -$URL_PARAMS=array('name','lang'); +$cache = new CacheTimeCheck(filemtime(__FILE__)); +$cache->includeOnce('php/Options.inc'); +$cache->includeOnce('php/accept-language.inc'); +$cache->includeOnce('php/common-functions.inc'); +$cache->includeOnce('php/filters.inc'); +$cache->includeOnce('php/inputParser.inc'); + +$URL_PARAMS = array('name', 'lang'); $master = new DOMDocument(); $master->load("master.xml"); $options = new Options($master); +$options->setCache($cache); $lang = $_GET['lang']; if($lang) { @@ -60,7 +61,7 @@ } if (CACHING && $cacheable) - cache_check($newest); + $options->getCache()->CheckHttpModified($newest); print $master->saveXml($master); diff -r 647b72603b7d -r 7b19be62ea94 inputParser.inc --- a/inputParser.inc Sun Sep 30 03:39:32 2012 +0200 +++ b/inputParser.inc Thu Oct 04 19:46:11 2012 +0200 @@ -43,7 +43,7 @@ if (!file_exists($confFile)) { errorPage("Resource not available"); } - cache_time($confFile); + $options->getCache()->cache_time($confFile); $doc = new DOMDocument(); $doc->load($confFile); @@ -65,7 +65,7 @@ $subdoc = new DOMDocument(); $subfile = "${lang}/${src}"; $subdoc->load("$subfile"); - cache_time($subfile); + $options->getCache()->cache_time($subfile); $parent = $include->parentNode; $xml = getElementByTagName($subdoc,"xml"); foreach($xml->childNodes as $child) { @@ -142,7 +142,7 @@ else { $src = $file->getAttribute("src"); $fname = "${lang}/${src}"; - cache_time($fname); + $options->getCache()->cache_time($fname); $file_content = loadFile($fname); } if(floatval($file_content)<0) {