Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplatecachefunction.php
Go to the documentation of this file.
1 <?php
18 class eZTemplateCacheFunction
19 {
20 const DEFAULT_TTL = 7200; // 2 hours = 60*60*2
21
27 public function __construct( $blockName = 'cache-block' )
28 {
29 $this->BlockName = $blockName;
30 }
31
36 function functionList()
37 {
38 return array( $this->BlockName );
39 }
40
41 function functionTemplateHints()
42 {
43 return array( $this->BlockName => array( 'parameters' => true,
44 'static' => false,
45 'transform-children' => true,
46 'tree-transformation' => true,
47 'transform-parameters' => true ) );
48 }
49
50 function templateNodeTransformation( $functionName, &$node,
51 $tpl, $parameters, $privateData )
52 {
53 $ini = eZINI::instance();
54 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
55 if ( $ini->variable( 'TemplateSettings', 'TemplateCache' ) != 'enabled' )
56 {
57 return $children;
58 }
59
60 $functionPlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
61 $placementKeyString = eZTemplateCacheBlock::placementString( $functionPlacement );
62
63 $newNodes = array();
64 $ignoreContentExpiry = false;
65
66 if ( isset( $parameters['expiry'] ) )
67 {
68 if ( eZTemplateNodeTool::isConstantElement( $parameters['expiry'] ) )
69 {
70 $expiryValue = eZTemplateNodeTool::elementConstantValue( $parameters['expiry'] );
71 $ttlCode = $expiryValue > 0 ? eZPHPCreator::variableText( $expiryValue , 0, 0, false ) : 'null';
72 }
73 else
74 {
75 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['expiry'], false, array(), 'localExpiry' );
76 $ttlCode = "( \$localExpiry > 0 ? \$localExpiry : null )";
77 }
78 }
79 else
80 {
81 $ttlCode = eZPHPCreator::variableText( self::DEFAULT_TTL, 0, 0, false );
82 }
83
84 if ( isset( $parameters['ignore_content_expiry'] ) )
85 {
86 $ignoreContentExpiry = eZTemplateNodeTool::elementConstantValue( $parameters['ignore_content_expiry'] );
87 }
88
89 $keysData = false;
90 $hasKeys = false;
91 $subtreeExpiryData = null;
92 $subtreeValue = null;
93 if ( isset( $parameters['keys'] ) )
94 {
95 $keysData = $parameters['keys'];
96 $hasKeys = true;
97 }
98 if ( isset( $parameters['subtree_expiry'] ) )
99 {
100 $subtreeExpiryData = $parameters['subtree_expiry'];
101 if ( !eZTemplateNodeTool::isConstantElement( $subtreeExpiryData ) )
102 $hasKeys = true;
103 else
104 $subtreeValue = eZTemplateNodeTool::elementConstantValue( $subtreeExpiryData );
105
106 $ignoreContentExpiry = true;
107 }
108 $accessName = false;
109 if ( isset( $GLOBALS['eZCurrentAccess']['name'] ) )
110 $accessName = $GLOBALS['eZCurrentAccess']['name'];
111 if ( $hasKeys )
112 {
113 $placementKeyStringText = eZPHPCreator::variableText( $placementKeyString, 0, 0, false );
114 $accessNameText = eZPHPCreator::variableText( $accessName, 0, 0, false );
115 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $keysData, false, array(), 'cacheKeys' );
116 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $subtreeExpiryData, false, array(), 'subtreeExpiry' );
117
118 $code = "\$cacheKeys = array( \$cacheKeys, $placementKeyStringText, $accessNameText );\n";
119 $cachePathText = "\$cachePath";
120 }
121 else
122 {
123 $nodeID = $subtreeValue ? eZTemplateCacheBlock::decodeNodeID( $subtreeValue ) : false;
124 $cachePath = eZTemplateCacheBlock::cachePath( eZTemplateCacheBlock::keyString( array( $placementKeyString, $accessName ) ), $nodeID );
125 $code = "";
126 $cachePathText = eZPHPCreator::variableText( $cachePath, 0, 0, false );
127 }
128
129 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
130
131 $code = '';
132
133 $codePlacementHash = md5( $placementKeyString );
134 if ( $hasKeys )
135 {
136 $code .= "list(\$cacheHandler_{$codePlacementHash}, \$contentData) =\n eZTemplateCacheBlock::retrieve( \$cacheKeys, \$subtreeExpiry, $ttlCode, " . ($ignoreContentExpiry ? "false" : "true") . " );\n";
137 }
138 else
139 {
140 $nodeIDText = var_export( $nodeID, true );
141 $code .= "list(\$cacheHandler_{$codePlacementHash}, \$contentData) =\n eZTemplateCacheBlock::handle( $cachePathText, $nodeIDText, $ttlCode, " . ($ignoreContentExpiry ? "false" : "true") . " );\n";
142 }
143 $code .=
144 "if ( !( \$contentData instanceof eZClusterFileFailure ) )\n" .
145 "{\n";
146
147 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code, array( 'spacing' => 0 ) );
148 $newNodes[] = eZTemplateNodeTool::createWriteToOutputVariableNode( 'contentData', array( 'spacing' => 4 ) );
149 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( " unset( \$contentData );\n" .
150 "}\n" .
151 "else\n" .
152 "{\n" .
153 " unset( \$contentData );" );
154
155 $newNodes[] = eZTemplateNodeTool::createOutputVariableIncreaseNode( array( 'spacing' => 4 ) );
156 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode( 4 );
157 $newNodes = array_merge( $newNodes, $children );
158 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode( 4 );
159 $newNodes[] = eZTemplateNodeTool::createAssignFromOutputVariableNode( 'cachedText', array( 'spacing' => 4 ) );
160
161 $code =
162 "\$cacheHandler_{$codePlacementHash}->storeCache( array( 'scope' => 'template-block', 'binarydata' => \$cachedText ) );\n";
163
164 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code, array( 'spacing' => 4 ) );
165 $newNodes[] = eZTemplateNodeTool::createOutputVariableDecreaseNode( array( 'spacing' => 4 ) );
166 $newNodes[] = eZTemplateNodeTool::createWriteToOutputVariableNode( 'cachedText', array( 'spacing' => 4 ) );
167 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( " unset( \$cachedText, \$cacheHandler_{$codePlacementHash} );\n}\n" );
168
169 return $newNodes;
170 }
171
175 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
176 {
177 switch ( $functionName )
178 {
179 case $this->BlockName:
180 {
181 // Check for disabled cache.
182 $ini = eZINI::instance();
183 if ( $ini->variable( 'TemplateSettings', 'TemplateCache' ) != 'enabled' )
184 {
185 $text = eZTemplateCacheFunction::processUncached( $tpl, $functionChildren,
186 $rootNamespace, $currentNamespace );
187 $textElements[] = $text;
188 return;
189 }
190 else
191 {
192 $text = eZTemplateCacheFunction::processCachedPreprocess( $tpl, $functionChildren,
193 $functionParameters, $functionPlacement,
194 $rootNamespace, $currentNamespace );
195 $textElements[] = $text;
196 }
197 } break;
198 }
199 }
200
201 function processCachedPreprocess( $tpl, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
202 {
203 $keys = null;
204 $subtreeExpiry = null;
205 $expiry = self::DEFAULT_TTL;
206 $ignoreContentExpiry = null;
207 $subtreeExpiry = null;
208
209 if ( isset( $functionParameters["keys"] ) )
210 {
211 $keys = $tpl->elementValue( $functionParameters["keys"], $rootNamespace, $currentNamespace, $functionPlacement );
212 }
213 if ( isset( $functionParameters['subtree_expiry'] ) )
214 {
215 $subtreeExpiry = $tpl->elementValue( $functionParameters["subtree_expiry"], $rootNamespace, $currentNamespace, $functionPlacement );
216 }
217 if ( isset( $functionParameters["expiry"] ) )
218 {
219 $expiry = $tpl->elementValue( $functionParameters["expiry"], $rootNamespace, $currentNamespace, $functionPlacement );
220 }
221 if ( isset( $functionParameters["ignore_content_expiry"] ) )
222 {
223 $ignoreContentExpiry = $tpl->elementValue( $functionParameters["ignore_content_expiry"], $rootNamespace, $currentNamespace, $functionPlacement ) === true;
224 }
225 if ( isset( $functionParameters['subtree_expiry'] ) )
226 {
227 $ignoreContentExpiry = true;
228 }
229
230 $placementString = eZTemplateCacheBlock::placementString( $functionPlacement );
231
232 return eZTemplateCacheFunction::processCached( $tpl, $functionChildren, $rootNamespace, $currentNamespace,
233 $placementString, $keys, $subtreeExpiry, $expiry, $ignoreContentExpiry );
234 }
235
236 function processCached( $tpl, $functionChildren, $rootNamespace, $currentNamespace,
237 $placementString, $keys, $subtreeExpiry, $expiry, $ignoreContentExpiry )
238 {
239 // Fetch the current siteaccess
240 $accessName = false;
241 if ( isset( $GLOBALS['eZCurrentAccess']['name'] ) )
242 $accessName = $GLOBALS['eZCurrentAccess']['name'];
243 if ( $keys === null )
244 {
245 $keyArray = array( $placementString, $accessName );
246 }
247 else
248 {
249 $keyArray = array( $keys, $placementString, $accessName );
250 }
251
252 $nodeID = $subtreeExpiry ? eZTemplateCacheBlock::decodeNodeID( $subtreeExpiry ) : false;
253 $phpPath = eZTemplateCacheBlock::cachePath( eZTemplateCacheBlock::keyString( $keyArray ), $nodeID );
254
255 $ttl = $expiry > 0 ? $expiry : null;
256
257 if ( $subtreeExpiry !== null )
258 {
259 $ignoreContentExpiry = true;
260 }
261 else if ( $ignoreContentExpiry === null )
262 {
263 $ignoreContentExpiry = false;
264 }
265
266 $globalExpiryTime = -1;
267 if ( $ignoreContentExpiry == false )
268 {
269 $globalExpiryTime = eZExpiryHandler::getTimestamp( 'template-block-cache', -1 );
270 }
271 $globalExpiryTime = max( eZExpiryHandler::getTimestamp( 'global-template-block-cache', -1 ), // This expiry value is the true global expiry for cache-blocks
272 $globalExpiryTime );
273
274 // Check if we can restore
275 $cacheFile = eZClusterFileHandler::instance( $phpPath );
276 $args = array( "tpl" => $tpl,
277 "functionChildren" => $functionChildren,
278 "rootNamespace" => $rootNamespace,
279 "currentNamespace" => $currentNamespace );
280 return $cacheFile->processCache( array( 'eZTemplateCacheBlock', 'retrieveContent' ),
281 array( $this, 'generateProcessedContent' ),
282 $ttl,
283 $globalExpiryTime,
284 $args );
285 }
286
287 function generateProcessedContent( $fname, $args )
288 {
289 extract( $args );
290 $content = eZTemplateCacheFunction::processUncached( $tpl, $functionChildren, $rootNamespace, $currentNamespace );
291 return array( 'scope' => 'template-block',
292 'content' => $content,
293 'binarydata' => $content );
294 }
295
301 function processUncached( $tpl, $functionChildren, $rootNamespace, $currentNamespace )
302 {
303 $children = $functionChildren;
304
305 $childTextElements = array();
306 if ( is_array( $children ) )
307 {
308 foreach ( array_keys( $children ) as $childKey )
309 {
310 $child =& $children[$childKey];
311 $tpl->processNode( $child, $childTextElements, $rootNamespace, $currentNamespace );
312 }
313 }
314 $text = implode( '', $childTextElements );
315 return $text;
316 }
317
321 function hasChildren()
322 {
323 return true;
324 }
325
328 public $BlockName;
329 }
330
331 ?>
eZClusterFileHandler\instance
static instance( $filename=false)
Definition: ezclusterfilehandler.php:21
eZTemplateCacheBlock\keyString
static keyString( $keys)
Definition: eztemplatecacheblock.php:157
eZTemplateCacheBlock\decodeNodeID
static decodeNodeID( $subtreeExpiryParameter)
Definition: eztemplatecacheblock.php:205
eZTemplateCacheBlock\placementString
static placementString( $functionPlacement)
Definition: eztemplatecacheblock.php:141
eZTemplateCacheBlock\cachePath
static cachePath( $keyString, $nodeID=false)
Definition: eztemplatecacheblock.php:168
eZTemplateCacheFunction
Advanced cache handling.
Definition: eztemplatecachefunction.php:19
eZTemplateCacheFunction\processUncached
processUncached( $tpl, $functionChildren, $rootNamespace, $currentNamespace)
Definition: eztemplatecachefunction.php:301
eZTemplateCacheFunction\processCached
processCached( $tpl, $functionChildren, $rootNamespace, $currentNamespace, $placementString, $keys, $subtreeExpiry, $expiry, $ignoreContentExpiry)
Definition: eztemplatecachefunction.php:236
eZTemplateCacheFunction\DEFAULT_TTL
const DEFAULT_TTL
Definition: eztemplatecachefunction.php:20
eZTemplateCacheFunction\generateProcessedContent
generateProcessedContent( $fname, $args)
Definition: eztemplatecachefunction.php:287
eZTemplateCacheFunction\$BlockName
$BlockName
Definition: eztemplatecachefunction.php:328
eZTemplateCacheFunction\process
process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplatecachefunction.php:175
eZTemplateCacheFunction\__construct
__construct( $blockName='cache-block')
Definition: eztemplatecachefunction.php:27
eZTemplateCacheFunction\processCachedPreprocess
processCachedPreprocess( $tpl, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplatecachefunction.php:201
eZTemplateCacheFunction\templateNodeTransformation
templateNodeTransformation( $functionName, &$node, $tpl, $parameters, $privateData)
Definition: eztemplatecachefunction.php:50
eZTemplateCacheFunction\hasChildren
hasChildren()
Definition: eztemplatecachefunction.php:321
eZTemplateCacheFunction\functionTemplateHints
functionTemplateHints()
Definition: eztemplatecachefunction.php:41
eZTemplateCacheFunction\functionList
functionList()
Definition: eztemplatecachefunction.php:36
eZTemplateNodeTool\createVariableNode
static createVariableNode( $originalNode=false, $variableData=false, $variablePlacement=false, $parameters=array(), $variableAssignmentName=false, $onlyExisting=false, $overWrite=true, $assignFromVariable=false, $rememberSet=false)
Definition: eztemplatenodetool.php:395
eZTemplateNodeTool\createWriteToOutputVariableNode
static createWriteToOutputVariableNode( $variableName, $parameters=array(), $assignmentType=eZPHPCreator::VARIABLE_APPEND_TEXT)
Definition: eztemplatenodetool.php:475
eZTemplateNodeTool\elementConstantValue
static elementConstantValue( $elements)
Definition: eztemplatenodetool.php:197
eZTemplateNodeTool\createSpacingDecreaseNode
static createSpacingDecreaseNode( $spacing=4, $parameters=array())
Definition: eztemplatenodetool.php:508
eZTemplateNodeTool\createCodePieceNode
static createCodePieceNode( $codePiece, $parameters=array())
Definition: eztemplatenodetool.php:457
eZTemplateNodeTool\createSpacingIncreaseNode
static createSpacingIncreaseNode( $spacing=4, $parameters=array())
Definition: eztemplatenodetool.php:503
eZTemplateNodeTool\createOutputVariableIncreaseNode
static createOutputVariableIncreaseNode( $parameters=array())
Definition: eztemplatenodetool.php:493
eZTemplateNodeTool\extractFunctionNodePlacement
static extractFunctionNodePlacement(&$node)
Definition: eztemplatenodetool.php:660
eZTemplateNodeTool\createOutputVariableDecreaseNode
static createOutputVariableDecreaseNode( $parameters=array())
Definition: eztemplatenodetool.php:498
eZTemplateNodeTool\createAssignFromOutputVariableNode
static createAssignFromOutputVariableNode( $variableName, $parameters=array(), $assignmentType=eZPHPCreator::VARIABLE_ASSIGNMENT)
Definition: eztemplatenodetool.php:488
eZTemplateNodeTool\extractFunctionNodeChildren
static extractFunctionNodeChildren(&$node)
Definition: eztemplatenodetool.php:669
eZTemplateNodeTool\isConstantElement
static isConstantElement( $elements)
Definition: eztemplatenodetool.php:233
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.