function templateNodeTransformation($functionName, &$node, $tpl, $parameters, $privateData)
 {
     $ini = eZINI::instance();
     $children = eZTemplateNodeTool::extractFunctionNodeChildren($node);
     if ($ini->variable('TemplateSettings', 'TemplateCache') != 'enabled') {
         return $children;
     }
     $functionPlacement = eZTemplateNodeTool::extractFunctionNodePlacement($node);
     $placementKeyString = eZTemplateCacheBlock::placementString($functionPlacement);
     $newNodes = array();
     $ignoreContentExpiry = false;
     if (isset($parameters['expiry'])) {
         if (eZTemplateNodeTool::isConstantElement($parameters['expiry'])) {
             $expiryValue = eZTemplateNodeTool::elementConstantValue($parameters['expiry']);
             $ttlCode = $expiryValue > 0 ? eZPHPCreator::variableText($expiryValue, 0, 0, false) : 'null';
         } else {
             $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['expiry'], false, array(), 'localExpiry');
             $ttlCode = "( \$localExpiry > 0 ? \$localExpiry : null )";
         }
     } else {
         $ttlCode = eZPHPCreator::variableText(self::DEFAULT_TTL, 0, 0, false);
     }
     if (isset($parameters['ignore_content_expiry'])) {
         $ignoreContentExpiry = eZTemplateNodeTool::elementConstantValue($parameters['ignore_content_expiry']);
     }
     $keysData = false;
     $hasKeys = false;
     $subtreeExpiryData = null;
     $subtreeValue = null;
     if (isset($parameters['keys'])) {
         $keysData = $parameters['keys'];
         $hasKeys = true;
     }
     if (isset($parameters['subtree_expiry'])) {
         $subtreeExpiryData = $parameters['subtree_expiry'];
         if (!eZTemplateNodeTool::isConstantElement($subtreeExpiryData)) {
             $hasKeys = true;
         } else {
             $subtreeValue = eZTemplateNodeTool::elementConstantValue($subtreeExpiryData);
         }
         $ignoreContentExpiry = true;
     }
     $accessName = false;
     if (isset($GLOBALS['eZCurrentAccess']['name'])) {
         $accessName = $GLOBALS['eZCurrentAccess']['name'];
     }
     if ($hasKeys) {
         $placementKeyStringText = eZPHPCreator::variableText($placementKeyString, 0, 0, false);
         $accessNameText = eZPHPCreator::variableText($accessName, 0, 0, false);
         $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $keysData, false, array(), 'cacheKeys');
         $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $subtreeExpiryData, false, array(), 'subtreeExpiry');
         $code = "\$cacheKeys = array( \$cacheKeys, {$placementKeyStringText}, {$accessNameText} );\n";
         $cachePathText = "\$cachePath";
     } else {
         $nodeID = $subtreeValue ? eZTemplateCacheBlock::decodeNodeID($subtreeValue) : false;
         $cachePath = eZTemplateCacheBlock::cachePath(eZTemplateCacheBlock::keyString(array($placementKeyString, $accessName)), $nodeID);
         $code = "";
         $cachePathText = eZPHPCreator::variableText($cachePath, 0, 0, false);
     }
     $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code);
     $code = '';
     $codePlacementHash = md5($placementKeyString);
     if ($hasKeys) {
         $code .= "list(\$cacheHandler_{$codePlacementHash}, \$contentData) =\n  eZTemplateCacheBlock::retrieve( \$cacheKeys, \$subtreeExpiry, {$ttlCode}, " . ($ignoreContentExpiry ? "false" : "true") . " );\n";
     } else {
         $nodeIDText = var_export($nodeID, true);
         $code .= "list(\$cacheHandler_{$codePlacementHash}, \$contentData) =\n  eZTemplateCacheBlock::handle( {$cachePathText}, {$nodeIDText}, {$ttlCode}, " . ($ignoreContentExpiry ? "false" : "true") . " );\n";
     }
     $code .= "if ( !( \$contentData instanceof eZClusterFileFailure ) )\n" . "{\n";
     $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code, array('spacing' => 0));
     $newNodes[] = eZTemplateNodeTool::createWriteToOutputVariableNode('contentData', array('spacing' => 4));
     $newNodes[] = eZTemplateNodeTool::createCodePieceNode("    unset( \$contentData );\n" . "}\n" . "else\n" . "{\n" . "    unset( \$contentData );");
     $newNodes[] = eZTemplateNodeTool::createOutputVariableIncreaseNode(array('spacing' => 4));
     $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(4);
     $newNodes = array_merge($newNodes, $children);
     $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(4);
     $newNodes[] = eZTemplateNodeTool::createAssignFromOutputVariableNode('cachedText', array('spacing' => 4));
     $code = "\$cacheHandler_{$codePlacementHash}->storeCache( array( 'scope' => 'template-block', 'binarydata' => \$cachedText ) );\n";
     $newNodes[] = eZTemplateNodeTool::createCodePieceNode($code, array('spacing' => 4));
     $newNodes[] = eZTemplateNodeTool::createOutputVariableDecreaseNode(array('spacing' => 4));
     $newNodes[] = eZTemplateNodeTool::createWriteToOutputVariableNode('cachedText', array('spacing' => 4));
     $newNodes[] = eZTemplateNodeTool::createCodePieceNode("    unset( \$cachedText, \$cacheHandler_{$codePlacementHash} );\n}\n");
     return $newNodes;
 }
Exemple #2
0
 function templateNodeTransformation($functionName, &$node, $tpl, $parameters, $privateData)
 {
     if ($functionName == $this->BlockName or $functionName == $this->AppendBlockName) {
         if (!isset($parameters['variable'])) {
             return false;
         }
         $scope = eZTemplate::NAMESPACE_SCOPE_RELATIVE;
         if (isset($parameters['scope'])) {
             if (!eZTemplateNodeTool::isConstantElement($parameters['scope'])) {
                 return false;
             }
             $scopeText = eZTemplateNodeTool::elementConstantValue($parameters['scope']);
             if ($scopeText == 'relative') {
                 $scope = eZTemplate::NAMESPACE_SCOPE_RELATIVE;
             } else {
                 if ($scopeText == 'root') {
                     $scope = eZTemplate::NAMESPACE_SCOPE_LOCAL;
                 } else {
                     if ($scopeText == 'global') {
                         $scope = eZTemplate::NAMESPACE_SCOPE_GLOBAL;
                     }
                 }
             }
         }
         $name = '';
         if (isset($parameters['name'])) {
             if (!eZTemplateNodeTool::isConstantElement($parameters['name'])) {
                 return false;
             }
             $name = eZTemplateNodeTool::elementConstantValue($parameters['name']);
         }
         $variableName = eZTemplateNodeTool::elementConstantValue($parameters['variable']);
         $newNodes = array();
         $children = eZTemplateNodeTool::extractFunctionNodeChildren($node);
         $newNodes[] = eZTemplateNodeTool::createOutputVariableIncreaseNode();
         $newNodes = array_merge($newNodes, $children);
         $newNodes[] = eZTemplateNodeTool::createAssignFromOutputVariableNode('blockText');
         if ($functionName == $this->AppendBlockName) {
             $data = array(eZTemplateNodeTool::createVariableElement($variableName, $name, $scope));
             $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $data, false, array(), 'blockData');
             // This block checks whether the append-block variable is an array or not.
             // TODO: This is a temporary solution and should also check whether the template variable exists.
             // This new solution requires probably writing the createVariableElement and createVariableNode your self.
             $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( is_null ( \$blockData ) ) \$blockData = array();");
             $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( is_array ( \$blockData ) ) \$blockData[] = \$blockText;");
             $newNodes[] = eZTemplateNodeTool::createCodePieceNode("else eZDebug::writeError( \"Variable '{$variableName}' is already in use.\" );");
             $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'blockData', false, array(), array($name, $scope, $variableName), false, true, true);
             $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode('blockData');
         } else {
             $newNodes[] = eZTemplateNodeTool::createVariableNode(false, 'blockText', false, array(), array($name, $scope, $variableName), false, true, true);
         }
         $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode('blockText');
         $newNodes[] = eZTemplateNodeTool::createOutputVariableDecreaseNode();
         return $newNodes;
     } else {
         if ($functionName == $this->OnceName) {
             $functionPlacement = eZTemplateNodeTool::extractFunctionNodePlacement($node);
             $key = $this->placementKey($functionPlacement);
             $newNodes = array();
             if ($key !== false) {
                 $keyText = eZPHPCreator::variableText($key, 0, 0, false);
                 $placementText = eZPHPCreator::variableText($functionPlacement, 0, 0, false);
                 $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( !isset( \$GLOBALS['eZTemplateRunOnceKeys'][{$keyText}] ) )\n" . "{\n" . "    \$GLOBALS['eZTemplateRunOnceKeys'][{$keyText}] = {$placementText};");
                 $children = eZTemplateNodeTool::extractFunctionNodeChildren($node);
                 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(4);
                 $newNodes = array_merge($newNodes, $children);
                 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(4);
                 $newNodes[] = eZTemplateNodeTool::createCodePieceNode("}");
             }
             return $newNodes;
         }
     }
     return false;
 }