Changed handling of PACKAGE_ID and templates
authorAlexander Ebert <ebert@woltlab.com>
Mon, 5 Nov 2012 22:15:50 +0000 (23:15 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 5 Nov 2012 22:15:50 +0000 (23:15 +0100)
wcfsetup/install/files/lib/data/option/OptionEditor.class.php
wcfsetup/install/files/lib/data/package/Package.class.php
wcfsetup/install/files/lib/system/WCF.class.php
wcfsetup/install/files/lib/system/template/TemplateEngine.class.php
wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php

index 2c23413aa71fc7b83e80c3cfaa7ecab437187d13..53334cb41d8e32cb257d37d580eda7d79e3ff5fe 100644 (file)
@@ -114,7 +114,7 @@ class OptionEditor extends DatabaseObjectEditor implements IEditableCachedObject
         * @param       string          $filename
         * @param       integer         $packageID
         */
-       public static function rebuildFile($filename, $packageID = PACKAGE_ID) {
+       public static function rebuildFile($filename, $packageID) {
                $buffer = '';
                
                // file header
index 7c83387aee66242632fc91805fb2e52cb36871dd..20d926c3f61599754e6bef78667d0b600ee7d818 100644 (file)
@@ -600,10 +600,23 @@ class Package extends DatabaseObject {
                        $file->write("\n");
                }
                
+               // get primary application
+               $sql = "SELECT          applications.packageID
+                       FROM            wcf".WCF_N."_application application,
+                                       wcf".WCF_N."_application applications
+                       WHERE           application.packageID = ?
+                                       AND applications.groupID = application.groupID
+                                       AND applications.groupID IS NOT NULL
+                                       AND applications.isPrimary = ?";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute(array($packageID, 1));
+               $row = $statement->fetchArray();
+               $packageID = ($row === false) ? $packageID : $row['packageID'];
+               
                // write general information
                $file->write("// general info\n");
                $file->write("if (!defined('RELATIVE_WCF_DIR')) define('RELATIVE_WCF_DIR', RELATIVE_".$currentPrefix."_DIR.'".FileUtil::getRelativePath($packageDir, WCF_DIR)."');\n");
-               $file->write("if (!defined('PACKAGE_ID')) define('PACKAGE_ID', ".$package->packageID.");\n");
+               $file->write("if (!defined('PACKAGE_ID')) define('PACKAGE_ID', ".$row['packageID'].");\n");
                $file->write("if (!defined('PACKAGE_NAME')) define('PACKAGE_NAME', '".str_replace("'", "\'", $package->getName())."');\n");
                $file->write("if (!defined('PACKAGE_VERSION')) define('PACKAGE_VERSION', '".$package->packageVersion."');\n");
                
index 2ecb05f6612edea654c0df68d33ea2e6337aade6..65a4e6cca3864295fc2c5707e34a6e3300f12079 100644 (file)
@@ -451,6 +451,9 @@ class WCF {
                        
                        // start application if not within ACP
                        if (!class_exists('wcf\system\WCFACP', false)) {
+                               // add template path and abbreviation
+                               $this->getTPL()->addApplication($abbreviation, $application->packageID, $packageDir . 'templates/');
+                               
                                call_user_func(array($className, 'getInstance'));
                        }
                }
@@ -461,7 +464,7 @@ class WCF {
                
                // register template path in ACP
                if (class_exists('wcf\system\WCFACP', false)) {
-                       $this->getTPL()->addTemplatePath($application->packageID, $packageDir . 'acp/templates/');
+                       $this->getTPL()->addApplication($abbreviation, $application->packageID, $packageDir . 'acp/templates/');
                }
                else if (!$isDependentApplication) {
                        // assign base tag
index 669afe7581b7bfef474806b66eb1f9b4b9affa0e..b880045418d850003b29a33de8fb45d888c04bdd 100755 (executable)
@@ -20,6 +20,12 @@ use wcf\util\StringUtil;
  * @category   Community Framework
  */
 class TemplateEngine extends SingletonFactory {
+       /**
+        * list of abbreviations to package id
+        * @var array<integer>
+        */
+       public $abbreviations = array();
+       
        /**
         * directory used to cache previously compiled templates
         * @var string
@@ -102,6 +108,7 @@ class TemplateEngine extends SingletonFactory {
         * @see wcf\system\SingletonFactory::init()
         */
        protected function init() {
+               $this->abbreviations['wcf'] = 1;
                $this->templatePaths = array(1 => WCF_DIR.'templates/');
                $this->pluginNamespace = 'wcf\system\template\plugin\\';
                $this->compileDir = WCF_DIR.'templates/compiled/';
@@ -112,12 +119,14 @@ class TemplateEngine extends SingletonFactory {
        }
        
        /**
-        * Adds a new template path for given package id.
+        * Adds a new application.
         * 
+        * @param       string          $abbreviation
         * @param       integer         $packageID
         * @param       string          $templatePath
         */
-       public function addTemplatePath($packageID, $templatePath) {
+       public function addApplication($abbreviation, $packageID, $templatePath) {
+               $this->abbreviations[$abbreviation] = $packageID;
                $this->templatePaths[$packageID] = $templatePath;
        }
        
@@ -284,10 +293,10 @@ class TemplateEngine extends SingletonFactory {
         * Outputs a template.
         * 
         * @param       string          $templateName
-        * @param       integer         $packageID
+        * @param       string          $application
         * @param       boolean         $sendHeaders
         */
-       public function display($templateName, $packageID = PACKAGE_ID, $sendHeaders = true) {
+       public function display($templateName, $application = 'wcf', $sendHeaders = true) {
                if ($sendHeaders) {
                        HeaderUtil::sendHeaders();
                        
@@ -295,24 +304,24 @@ class TemplateEngine extends SingletonFactory {
                        if (!defined('NO_IMPORTS')) EventHandler::getInstance()->fireAction($this, 'shouldDisplay');
                }
                
-               $tplPackageID = $this->getPackageID($templateName, $packageID);
-               $compiledFilename = $this->getCompiledFilename($templateName, $packageID);
+               $tplPackageID = $this->getPackageID($templateName, $application);
+               $compiledFilename = $this->getCompiledFilename($templateName, $tplPackageID);
                $sourceFilename = $this->getSourceFilename($templateName, $tplPackageID);
-               $metaDataFilename = $this->getMetaDataFilename($templateName, $packageID);
+               $metaDataFilename = $this->getMetaDataFilename($templateName, $tplPackageID);
                $metaData = $this->getMetaData($templateName, $metaDataFilename);
                
                // check if compilation is necessary
-               if (($metaData === null) || !$this->isCompiled($templateName, $sourceFilename, $compiledFilename, $packageID, $metaData)) {
+               if (($metaData === null) || !$this->isCompiled($templateName, $sourceFilename, $compiledFilename, $application, $metaData)) {
                        // compile
                        $this->compileTemplate($templateName, $sourceFilename, $compiledFilename, array(
+                               'application' => $application,
                                'data' => $metaData,
-                               'filename' => $metaDataFilename,
-                               'packageID' => $tplPackageID
+                               'filename' => $metaDataFilename
                        ));
                }
                
                // assign current package id
-               $this->assign('__PACKAGE_ID', $packageID);
+               $this->assign('__APPLICATION', $application);
                
                include($compiledFilename);
                
@@ -326,10 +335,16 @@ class TemplateEngine extends SingletonFactory {
         * Returns path and corresponding file path.
         * 
         * @param       string          $templateName
-        * @param       integer         $packageID
+        * @param       string          $application
         * @return      integer
         */
-       public function getPackageID($templateName, $packageID) {
+       public function getPackageID($templateName, $application = 'wcf') {
+               $packageID = (isset($this->abbreviations[$application])) ? $this->abbreviations[$application] : null;
+               if ($packageID === null) {
+                       throw new SystemException("Unable to find application for abbreviation '".$application."'");
+               }
+               
+               // search within application
                if ($packageID != 1 && isset($this->templatePaths[$packageID])) {
                        $path = $this->getPath($this->templatePaths[$packageID], $templateName);
                        
@@ -338,6 +353,7 @@ class TemplateEngine extends SingletonFactory {
                        }
                }
                
+               // search within WCF
                $path = $this->getPath($this->templatePaths[1], $templateName);
                if (!empty($path)) {
                        return 1;
@@ -415,11 +431,11 @@ class TemplateEngine extends SingletonFactory {
         * @param       string          $templateName
         * @param       string          $sourceFilename
         * @param       string          $compiledFilename
-        * @param       integer         $packageID
+        * @param       string          $application
         * @param       array           $metaData
         * @return      boolean         $isCompiled
         */
-       protected function isCompiled($templateName, $sourceFilename, $compiledFilename, $packageID, array $metaData) {
+       protected function isCompiled($templateName, $sourceFilename, $compiledFilename, $application, array $metaData) {
                if ($this->forceCompile || !file_exists($compiledFilename)) {
                        return false;
                }
@@ -434,7 +450,7 @@ class TemplateEngine extends SingletonFactory {
                                // check for meta data
                                if (!empty($metaData['include'])) {
                                        foreach ($metaData['include'] as $includedTemplate) {
-                                               $tplPackageID = $this->getPackageID($includedTemplate, $packageID);
+                                               $tplPackageID = $this->getPackageID($includedTemplate, $application);
                                                $includedTemplateFilename = $this->getSourceFilename($includedTemplate, $tplPackageID);
                                                $includedMTime = @filemtime($includedTemplateFilename);
                                                
@@ -544,12 +560,12 @@ class TemplateEngine extends SingletonFactory {
         * Returns the output of a template.
         * 
         * @param       string          $templateName
+        * @param       string          $application
         * @param       array           $variables
         * @param       boolean         $sandbox        enables execution in sandbox
-        * @param       integer         $packageID
         * @return      string
         */
-       public function fetch($templateName, array $variables = array(), $sandbox = false, $packageID = PACKAGE_ID) {
+       public function fetch($templateName, $application = 'wcf', array $variables = array(), $sandbox = false) {
                // enable sandbox
                if ($sandbox) {
                        $this->enableSandbox();
@@ -562,7 +578,7 @@ class TemplateEngine extends SingletonFactory {
                
                // get output
                ob_start();
-               $this->display($templateName, $packageID, false);
+               $this->display($templateName, $application, false);
                $output = ob_get_contents();
                ob_end_clean();
                
@@ -690,10 +706,11 @@ class TemplateEngine extends SingletonFactory {
         * Includes a template.
         * 
         * @param       string          $templateName
+        * @param       string          $application
         * @param       array           $variables
         * @param       boolean         $sandbox        enables execution in sandbox
         */
-       protected function includeTemplate($templateName, array $variables = array(), $sandbox = true, $packageID = PACKAGE_ID) {
+       protected function includeTemplate($templateName, $application, array $variables = array(), $sandbox = true) {
                // enable sandbox
                if ($sandbox) {
                        $this->enableSandbox();
@@ -705,7 +722,7 @@ class TemplateEngine extends SingletonFactory {
                }
                
                // display template
-               $this->display($templateName, $packageID, false);
+               $this->display($templateName, $application, false);
                
                // disable sandbox
                if ($sandbox) {
@@ -745,10 +762,11 @@ class TemplateEngine extends SingletonFactory {
         * Returns true if requested template has assigned template listeners.
         * 
         * @param       string          $templateName
+        * @param       string          $application
         * @return      boolean
         */
-       public function hasTemplateListeners($templateName) {
-               if (isset($this->templateListeners[$templateName])) {
+       public function hasTemplateListeners($templateName, $application = 'wcf') {
+               if (isset($this->templateListeners[$application]) && isset($this->templateListeners[$application][$templateName])) {
                        return true;
                }
                
index 2d62963cd47102bce5a1154eb2b81f3802e6c387..246dc4c9cea4756e842bbb977b38597924d2d27a 100644 (file)
@@ -783,14 +783,14 @@ class TemplateScriptingCompiler {
                        }
                        if (!empty($phpCode)) $phpCode = "<?php\n".$phpCode."\n?>";
                        
-                       $tplPackageID = WCF::getTPL()->getPackageID($templateName, $metaData['packageID']);
+                       $tplPackageID = WCF::getTPL()->getPackageID($templateName, $metaData['application']);
                        $sourceFilename = WCF::getTPL()->getSourceFilename($templateName, $tplPackageID);
                        $metaDataFilename = WCF::getTPL()->getMetaDataFilename($templateName, $tplPackageID);
                        
                        $data = $this->compileString($templateName, file_get_contents($sourceFilename), array(
+                               'application' => $metaData['application'],
                                'data' => null,
-                               'filename' => '',
-                               'packageID' => $tplPackageID
+                               'filename' => ''
                        ), true);
                        
                        return $phpCode . $data['template'];
@@ -809,7 +809,7 @@ class TemplateScriptingCompiler {
                        $phpCode .= "ob_start();\n";
                }
                
-               $phpCode .= '$this->includeTemplate('.$file.', array('.$argString.'), ('.$sandbox.' ? 1 : 0), $this->v[\'__PACKAGE_ID\']);'."\n";
+               $phpCode .= '$this->includeTemplate('.$file.', $this->v[\'__APPLICATION\'], array('.$argString.'), ('.$sandbox.' ? 1 : 0));'."\n";
                
                if ($assignVar !== false) {
                        $phpCode .= '$this->'.($append ? 'append' : 'assign').'('.$assignVar.', ob_get_clean());'."\n";