From f77fab01efe31007b0165ed226c1daf404cc8f28 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 4 Apr 2007 02:40:20 +0000 Subject: [PATCH] Removing BSTemplate database-driven system in place of BSTemplateFs (which is now called BSTemplate) because we never use database-driven templates any more, but rather file templates with database caching --- Template.php | 124 +++++++++++++++++++----------- TemplateFs.php | 201 ------------------------------------------------- 2 files changed, 80 insertions(+), 245 deletions(-) delete mode 100644 TemplateFs.php diff --git a/Template.php b/Template.php index 315218c..8dda1d1 100644 --- a/Template.php +++ b/Template.php @@ -25,11 +25,18 @@ * @package ISSO */ +require_once('ISSO/Functions.php'); + /** -* Database-Driven Template System +* File-Based Template System +* +* This framework merely replaces the template loading functions with +* file-system based ones. It has an optional caching system in which +* template data will remain stored in the database as long as the filesystem +* file is modified. To do this, pass a table name to setDatabaseCache() and make sure +* there's a DB module that has access to a table with this schema: * -* This framework is a backend to the database template engine and -* contains all the parsing algorithms. +* CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL); * * @author Blue Static * @copyright Copyright ©2002 - [#]year[#], Blue Static @@ -40,34 +47,22 @@ class BSTemplate { /** - * The database object to use - * @var object - */ - private $db; - - /** - * Name of the database table templates are in - * @var string - */ - private $tableName = ''; - - /** - * Name of the table column template names are in + * The path, from the path of the application, where templates are stored * @var string */ - private $nameColumn = ''; + private $templateDir = ''; /** - * Name of the table column templates are in + * The extension all the template files have * @var string */ - private $dataColumn = ''; + private $extension = 'tpl'; /** - * Additional WHERE clauses for the template fetch SQL - * @var string + * The database table name for the template cache + * @var string */ - private $extraWhere = ''; + private $dbCacheTable = null; /** * The name of the function phrases are fetched with @@ -113,37 +108,35 @@ class BSTemplate // ################################################################### /** - * Constructor: check required modules + * Sets the template directory name + * + * @param string Template directory name */ - public function __construct() + public function setTemplateDirectory($dir) { - BSRegister::RequiredModules(array('Db')); + $this->templateDir = BSFunctions::FetchSourcePath($dir); } // ################################################################### /** - * Sets the database object to use + * Sets the file extension for the templates * - * @param object Database object + * @param string File extension */ - public function setDatabase($db) + public function setExtension($ext) { - $this->db = $db; + $this->extension = $ext; } // ################################################################### /** - * Sets the table name and column information + * Sets the name of the table to access for the datbase cache * - * @param string The database table name - * @param string Name of the column that stores the name of the template - * @param string Name of the column that stores template data + * @param string DB table name */ - public function setDatabaseTable($tableName, $nameColumn, $dataColumn) + public function setDatabaseCache($table) { - $this->tableName = $tableName; - $this->nameColumn = $nameColumn; - $this->dataColumn = $dataColumn; + $this->dbCacheTable = $table; } // ################################################################### @@ -173,11 +166,45 @@ class BSTemplate } else { - $templates = $this->db->query("SELECT * FROM " . $this->tableName . " WHERE " . $this->nameColumn . " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ? ' ' . $this->extrawhere : '')); - while ($template = $this->db->fetch_array($templates)) + $dbCache = array(); + if ($this->dbCacheTable) { - $this->cache[ $template[ $this->nameColumn ] ] = $this->_parse($template[ $this->dataColumn ]); - $this->usage[ $template[ $this->nameColumn ] ] = 0; + $db =& BSRegister::GetType('Db'); + $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')"); + while ($tpl = $db->fetchArray($cache)) + { + $time = filemtime(BSRegister::GetAppPath() . $this->templateDir . $tpl['filename'] . '.' . $this->extension); + $template = $tpl['template']; + if ($time > $tpl['timestamp']) + { + $template = $this->_parseTemplate($this->_loadTemplate($tpl['filename'])); + $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escapeString($template) . "', timestamp = " . TIMENOW . " WHERE filename = '" . $tpl['filename'] . "'"); + $tpl['template'] = $template; + } + $dbCache["$tpl[filename]"] = $template; + } + } + foreach ($namearray AS $name) + { + if ($this->dbCacheTable) + { + if (isset($dbCache["$name"])) + { + $template = $dbCache["$name"]; + } + else + { + $template = $this->_parseTemplate($this->_loadTemplate($name)); + $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escapeString($template) . "', " . TIMENOW . ")"); + } + } + else + { + $template = $this->_parseTemplate($this->_loadTemplate($name)); + } + + $this->cache["$name"] = $template; + $this->usage["$name"] = 0; } } } @@ -307,13 +334,22 @@ class BSTemplate */ protected function _loadTemplate($name) { - if ($template = $this->db->queryFirst("SELECT * FROM " . $this->tableName . " WHERE " . $this->nameColumn . " = '$name'" . ($this->extrawhere ? ' ' . $this->extrawhere : ''))) + $path = BSRegister::GetAppPath() . $this->templateDir . $name . '.' . $this->extension; + if (is_file($path)) { - return $template[ $this->dataColumn ]; + if (($template = @file_get_contents($path)) !== false) + { + return $template; + } + else + { + trigger_error("Could not load the template '$path'"); + exit; + } } else { - trigger_error("The template '$name' could not be loaded"); + trigger_error("Could not load the template '$path'"); exit; } } diff --git a/TemplateFs.php b/TemplateFs.php deleted file mode 100644 index 1d80941..0000000 --- a/TemplateFs.php +++ /dev/null @@ -1,201 +0,0 @@ -templateDir = BSFunctions::FetchSourcePath($dir); - } - - // ################################################################### - /** - * Sets the file extension for the templates - * - * @param string File extension - */ - public function setExtension($ext) - { - $this->extension = $ext; - } - - // ################################################################### - /** - * Sets the name of the table to access for the datbase cache - * - * @param string DB table name - */ - public function setDatabaseCache($table) - { - $this->dbCacheTable = $table; - } - - // ################################################################### - /** - * Takes an array of template names, loads them, and then stores a - * parsed version for optimum speed. - * - * @param array List of template names to be cached - */ - public function cache($namearray) - { - if (sizeof($this->cache) > 0) - { - trigger_error('You cannot cache templates more than once per initialization'); - } - else - { - $dbCache = array(); - if ($this->dbCacheTable) - { - $db =& BSRegister::GetType('Db'); - $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')"); - while ($tpl = $db->fetchArray($cache)) - { - $time = filemtime(BSRegister::GetAppPath() . $this->templateDir . $tpl['filename'] . '.' . $this->extension); - $template = $tpl['template']; - if ($time > $tpl['timestamp']) - { - $template = $this->_parseTemplate($this->_loadTemplate($tpl['filename'])); - $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escapeString($template) . "', timestamp = " . TIMENOW . " WHERE filename = '" . $tpl['filename'] . "'"); - $tpl['template'] = $template; - } - $dbCache["$tpl[filename]"] = $template; - } - } - foreach ($namearray AS $name) - { - if ($this->dbCacheTable) - { - if (isset($dbCache["$name"])) - { - $template = $dbCache["$name"]; - } - else - { - $template = $this->_parseTemplate($this->_loadTemplate($name)); - $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escapeString($template) . "', " . TIMENOW . ")"); - } - } - else - { - $template = $this->_parseTemplate($this->_loadTemplate($name)); - } - - $this->cache["$name"] = $template; - $this->usage["$name"] = 0; - } - } - } - - // ################################################################### - /** - * Loads a template from the file system from the specified - * $templatedir with the file extension $extension - * - * @param string The name of the template call - */ - protected function _loadTemplate($name) - { - $path = BSRegister::GetAppPath() . $this->templateDir . $name . '.' . $this->extension; - if (is_file($path)) - { - if (($template = @file_get_contents($path)) !== false) - { - return $template; - } - else - { - trigger_error("Could not load the template '$path'"); - exit; - } - } - else - { - trigger_error("Could not load the template '$path'"); - exit; - } - } -} - -/*=====================================================================*\ -|| ################################################################### -|| # $HeadURL$ -|| # $Id$ -|| ################################################################### -\*=====================================================================*/ -?> \ No newline at end of file -- 2.43.5