MediaWiki REL1_30
TemplateParser.php
Go to the documentation of this file.
1<?php
3
25class TemplateParser {
29 protected $templateDir;
30
34 protected $renderers;
35
39 protected $forceRecompile = false;
40
44 // Do not add more flags here without discussion.
45 // If you do add more flags, be sure to update unit tests as well.
46 protected $compileFlags = LightnCandy::FLAG_ERROR_EXCEPTION;
47
52 public function __construct( $templateDir = null, $forceRecompile = false ) {
53 $this->templateDir = $templateDir ?: __DIR__ . '/templates';
54 $this->forceRecompile = $forceRecompile;
55 }
56
61 public function enableRecursivePartials( $enable ) {
62 if ( $enable ) {
63 $this->compileFlags = $this->compileFlags | LightnCandy::FLAG_RUNTIMEPARTIAL;
64 } else {
65 $this->compileFlags = $this->compileFlags & ~LightnCandy::FLAG_RUNTIMEPARTIAL;
66 }
67 }
68
75 protected function getTemplateFilename( $templateName ) {
76 // Prevent path traversal. Based on Language::isValidCode().
77 // This is for paranoia. The $templateName should never come from
78 // untrusted input.
79 if (
80 strcspn( $templateName, ":/\\\000&<>'\"%" ) !== strlen( $templateName )
81 ) {
82 throw new UnexpectedValueException( "Malformed \$templateName: $templateName" );
83 }
84
85 return "{$this->templateDir}/{$templateName}.mustache";
86 }
87
94 protected function getTemplate( $templateName ) {
95 $templateKey = $templateName . '|' . $this->compileFlags;
96
97 // If a renderer has already been defined for this template, reuse it
98 if ( isset( $this->renderers[$templateKey] ) &&
99 is_callable( $this->renderers[$templateKey] )
100 ) {
101 return $this->renderers[$templateKey];
102 }
103
104 $filename = $this->getTemplateFilename( $templateName );
105
106 if ( !file_exists( $filename ) ) {
107 throw new RuntimeException( "Could not locate template: {$filename}" );
108 }
109
110 // Read the template file
111 $fileContents = file_get_contents( $filename );
112
113 // Generate a quick hash for cache invalidation
114 $fastHash = md5( $this->compileFlags . '|' . $fileContents );
115
116 // Fetch a secret key for building a keyed hash of the PHP code
117 $config = MediaWikiServices::getInstance()->getMainConfig();
118 $secretKey = $config->get( 'SecretKey' );
119
120 if ( $secretKey ) {
121 // See if the compiled PHP code is stored in cache.
123 $key = $cache->makeKey( 'template', $templateName, $fastHash );
124 $code = $this->forceRecompile ? null : $cache->get( $key );
125
126 if ( $code ) {
127 // Verify the integrity of the cached PHP code
128 $keyedHash = substr( $code, 0, 64 );
129 $code = substr( $code, 64 );
130 if ( $keyedHash !== hash_hmac( 'sha256', $code, $secretKey ) ) {
131 // If the integrity check fails, don't use the cached code
132 // We'll update the invalid cache below
133 $code = null;
134 }
135 }
136 if ( !$code ) {
137 $code = $this->compileForEval( $fileContents, $filename );
138
139 // Prefix the cached code with a keyed hash (64 hex chars) as an integrity check
140 $cache->set( $key, hash_hmac( 'sha256', $code, $secretKey ) . $code );
141 }
142 // If there is no secret key available, don't use cache
143 } else {
144 $code = $this->compileForEval( $fileContents, $filename );
145 }
146
147 $renderer = eval( $code );
148 if ( !is_callable( $renderer ) ) {
149 throw new RuntimeException( "Requested template, {$templateName}, is not callable" );
150 }
151 $this->renderers[$templateKey] = $renderer;
152 return $renderer;
153 }
154
163 protected function compileForEval( $fileContents, $filename ) {
164 // Compile the template into PHP code
165 $code = $this->compile( $fileContents );
166
167 if ( !$code ) {
168 throw new RuntimeException( "Could not compile template: {$filename}" );
169 }
170
171 // Strip the "<?php" added by lightncandy so that it can be eval()ed
172 if ( substr( $code, 0, 5 ) === '<?php' ) {
173 $code = substr( $code, 5 );
174 }
175
176 return $code;
177 }
178
185 protected function compile( $code ) {
186 if ( !class_exists( 'LightnCandy' ) ) {
187 throw new RuntimeException( 'LightnCandy class not defined' );
188 }
189 return LightnCandy::compile(
190 $code,
191 [
192 'flags' => $this->compileFlags,
193 'basedir' => $this->templateDir,
194 'fileext' => '.mustache',
195 ]
196 );
197 }
198
216 public function processTemplate( $templateName, $args, array $scopes = [] ) {
217 $template = $this->getTemplate( $templateName );
218 return call_user_func( $template, $args, $scopes );
219 }
220}
if( $line===false) $args
Definition cdb.php:63
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getLocalServerInstance( $fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping $template
Definition hooks.txt:829
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:863
const CACHE_ANYTHING
Definition Defines.php:102
$cache
Definition mcc.php:33