| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Html; |
| 4 |
|
| 5 |
use FileContentsHasher; |
| 6 |
use LightnCandy\LightnCandy; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\MediaWikiServices; |
| 9 |
use RuntimeException; |
| 10 |
use UnexpectedValueException; |
| 11 |
use Wikimedia\ObjectCache\BagOStuff; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class TemplateParser { |
| 35 |
|
| 36 |
private const CACHE_VERSION = '2.2.0'; |
| 37 |
private const CACHE_TTL = BagOStuff::TTL_WEEK; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private $cache; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
protected $templateDir; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
protected $renderers; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
protected $compileFlags; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function __construct( $templateDir = null, ?BagOStuff $cache = null ) { |
| 64 |
$this->templateDir = $templateDir ?: __DIR__ . '/../templates'; |
| 65 |
$this->cache = $cache ?: MediaWikiServices::getInstance()->getObjectCacheFactory() |
| 66 |
->getLocalServerInstance( CACHE_ANYTHING ); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
$this->compileFlags = LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_MUSTACHELOOKUP; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function enableRecursivePartials( $enable ) { |
| 78 |
if ( $enable ) { |
| 79 |
$this->compileFlags |= LightnCandy::FLAG_RUNTIMEPARTIAL; |
| 80 |
} else { |
| 81 |
$this->compileFlags &= ~LightnCandy::FLAG_RUNTIMEPARTIAL; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected function getTemplateFilename( $templateName ) { |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if ( strcspn( $templateName, ":/\\\000&<>'\"%" ) !== strlen( $templateName ) ) { |
| 96 |
throw new UnexpectedValueException( "Malformed \$templateName: $templateName" ); |
| 97 |
} |
| 98 |
|
| 99 |
return "{$this->templateDir}/{$templateName}.mustache"; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
protected function getTemplate( $templateName ) { |
| 111 |
$templateKey = $templateName . '|' . $this->compileFlags; |
| 112 |
|
| 113 |
|
| 114 |
if ( isset( $this->renderers[$templateKey] ) && |
| 115 |
is_callable( $this->renderers[$templateKey] ) |
| 116 |
) { |
| 117 |
return $this->renderers[$templateKey]; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
$secretKey = MediaWikiServices::hasInstance() |
| 123 |
? MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SecretKey ) |
| 124 |
: null; |
| 125 |
|
| 126 |
if ( $secretKey ) { |
| 127 |
|
| 128 |
$key = $this->cache->makeKey( |
| 129 |
'lightncandy-compiled', |
| 130 |
self::CACHE_VERSION, |
| 131 |
$this->compileFlags, |
| 132 |
$this->templateDir, |
| 133 |
$templateName |
| 134 |
); |
| 135 |
$compiledTemplate = $this->cache->get( $key ); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
if ( $compiledTemplate ) { |
| 140 |
$filesHash = FileContentsHasher::getFileContentsHash( $compiledTemplate['files'] ); |
| 141 |
|
| 142 |
if ( $filesHash !== $compiledTemplate['filesHash'] ) { |
| 143 |
$compiledTemplate = null; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if ( $compiledTemplate ) { |
| 150 |
$integrityHash = hash_hmac( 'sha256', $compiledTemplate['phpCode'], $secretKey ); |
| 151 |
|
| 152 |
if ( $integrityHash !== $compiledTemplate['integrityHash'] ) { |
| 153 |
$compiledTemplate = null; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if ( !$compiledTemplate ) { |
| 160 |
$compiledTemplate = $this->compile( $templateName ); |
| 161 |
|
| 162 |
$compiledTemplate['integrityHash'] = hash_hmac( |
| 163 |
'sha256', |
| 164 |
$compiledTemplate['phpCode'], |
| 165 |
$secretKey |
| 166 |
); |
| 167 |
|
| 168 |
$this->cache->set( $key, $compiledTemplate, self::CACHE_TTL ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
} else { |
| 173 |
$compiledTemplate = $this->compile( $templateName ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
$renderer = eval( $compiledTemplate['phpCode'] ); |
| 178 |
if ( !is_callable( $renderer ) ) { |
| 179 |
throw new RuntimeException( "Compiled template `{$templateName}` is not callable" ); |
| 180 |
} |
| 181 |
$this->renderers[$templateKey] = $renderer; |
| 182 |
return $renderer; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
protected function compile( $templateName ) { |
| 217 |
$filename = $this->getTemplateFilename( $templateName ); |
| 218 |
|
| 219 |
if ( !file_exists( $filename ) ) { |
| 220 |
throw new RuntimeException( "Could not find template `{$templateName}` at {$filename}" ); |
| 221 |
} |
| 222 |
|
| 223 |
$files = [ $filename ]; |
| 224 |
$contents = file_get_contents( $filename ); |
| 225 |
$compiled = LightnCandy::compile( |
| 226 |
$contents, |
| 227 |
[ |
| 228 |
'flags' => $this->compileFlags, |
| 229 |
'basedir' => $this->templateDir, |
| 230 |
'fileext' => '.mustache', |
| 231 |
'partialresolver' => function ( $cx, $partialName ) use ( $templateName, &$files ) { |
| 232 |
$filename = "{$this->templateDir}/{$partialName}.mustache"; |
| 233 |
if ( !file_exists( $filename ) ) { |
| 234 |
throw new RuntimeException( sprintf( |
| 235 |
'Could not compile template `%s`: Could not find partial `%s` at %s', |
| 236 |
$templateName, |
| 237 |
$partialName, |
| 238 |
$filename |
| 239 |
) ); |
| 240 |
} |
| 241 |
|
| 242 |
$fileContents = file_get_contents( $filename ); |
| 243 |
|
| 244 |
if ( $fileContents === false ) { |
| 245 |
throw new RuntimeException( sprintf( |
| 246 |
'Could not compile template `%s`: Could not find partial `%s` at %s', |
| 247 |
$templateName, |
| 248 |
$partialName, |
| 249 |
$filename |
| 250 |
) ); |
| 251 |
} |
| 252 |
|
| 253 |
$files[] = $filename; |
| 254 |
|
| 255 |
return $fileContents; |
| 256 |
} |
| 257 |
] |
| 258 |
); |
| 259 |
if ( !$compiled ) { |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
throw new RuntimeException( "Could not compile template `{$filename}`" ); |
| 264 |
} |
| 265 |
|
| 266 |
$files = array_values( array_unique( $files ) ); |
| 267 |
|
| 268 |
return [ |
| 269 |
'phpCode' => $compiled, |
| 270 |
'files' => $files, |
| 271 |
'filesHash' => FileContentsHasher::getFileContentsHash( $files ), |
| 272 |
]; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public function processTemplate( $templateName, $args, array $scopes = [] ) { |
| 296 |
$template = $this->getTemplate( $templateName ); |
| 297 |
return $template( $args, $scopes ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
class_alias( TemplateParser::class, 'TemplateParser' ); |