2015-01-30 19:31:44 +00:00
|
|
|
<?php
|
2019-05-28 14:04:23 +00:00
|
|
|
|
2017-04-04 20:31:42 +00:00
|
|
|
use LightnCandy\LightnCandy;
|
2016-11-22 23:39:22 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2015-01-30 19:31:44 +00:00
|
|
|
/**
|
|
|
|
|
* Handles compiling Mustache templates into PHP rendering functions
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
2015-02-20 18:50:17 +00:00
|
|
|
* @since 1.25
|
2015-01-30 19:31:44 +00:00
|
|
|
*/
|
|
|
|
|
class TemplateParser {
|
2020-03-18 23:01:07 +00:00
|
|
|
|
|
|
|
|
private const CACHE_VERSION = '2.2.0';
|
|
|
|
|
private const CACHE_TTL = BagOStuff::TTL_WEEK;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var BagOStuff
|
|
|
|
|
*/
|
|
|
|
|
private $cache;
|
|
|
|
|
|
2015-01-30 19:31:44 +00:00
|
|
|
/**
|
|
|
|
|
* @var string The path to the Mustache templates
|
|
|
|
|
*/
|
|
|
|
|
protected $templateDir;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var callable[] Array of cached rendering functions
|
|
|
|
|
*/
|
|
|
|
|
protected $renderers;
|
|
|
|
|
|
2017-09-10 03:08:43 +00:00
|
|
|
/**
|
|
|
|
|
* @var int Compilation flags passed to LightnCandy
|
|
|
|
|
*/
|
2018-08-31 03:18:44 +00:00
|
|
|
protected $compileFlags;
|
2017-09-10 03:08:43 +00:00
|
|
|
|
2015-01-30 19:31:44 +00:00
|
|
|
/**
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $templateDir
|
2020-03-18 23:01:07 +00:00
|
|
|
* @param BagOStuff|null|true $cache Read-write cache
|
|
|
|
|
* If set to true, caching is disabled (deprecated since 1.35).
|
2015-01-30 19:31:44 +00:00
|
|
|
*/
|
2020-03-18 23:01:07 +00:00
|
|
|
public function __construct( $templateDir = null, $cache = null ) {
|
2015-10-21 23:55:10 +00:00
|
|
|
$this->templateDir = $templateDir ?: __DIR__ . '/templates';
|
2020-03-18 23:01:07 +00:00
|
|
|
if ( $cache === true ) {
|
|
|
|
|
wfDeprecated( __CLASS__ . ' with $forceRecompile', '1.35' );
|
|
|
|
|
$cache = new EmptyBagOStuff();
|
|
|
|
|
}
|
|
|
|
|
$this->cache = $cache ?: ObjectCache::getLocalServerInstance( CACHE_ANYTHING );
|
2018-08-31 03:18:44 +00:00
|
|
|
|
|
|
|
|
// Do not add more flags here without discussion.
|
|
|
|
|
// If you do add more flags, be sure to update unit tests as well.
|
|
|
|
|
$this->compileFlags = LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_MUSTACHELOOKUP;
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-10 03:08:43 +00:00
|
|
|
/**
|
|
|
|
|
* Enable/disable the use of recursive partials.
|
|
|
|
|
* @param bool $enable
|
|
|
|
|
*/
|
|
|
|
|
public function enableRecursivePartials( $enable ) {
|
|
|
|
|
if ( $enable ) {
|
2019-04-11 20:48:07 +00:00
|
|
|
$this->compileFlags |= LightnCandy::FLAG_RUNTIMEPARTIAL;
|
2017-09-10 03:08:43 +00:00
|
|
|
} else {
|
2019-04-11 20:48:07 +00:00
|
|
|
$this->compileFlags &= ~LightnCandy::FLAG_RUNTIMEPARTIAL;
|
2017-09-10 03:08:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 19:31:44 +00:00
|
|
|
/**
|
2018-08-09 08:21:56 +00:00
|
|
|
* Constructs the location of the source Mustache template
|
2015-01-30 19:31:44 +00:00
|
|
|
* @param string $templateName The name of the template
|
|
|
|
|
* @return string
|
2015-10-21 23:55:10 +00:00
|
|
|
* @throws UnexpectedValueException If $templateName attempts upwards directory traversal
|
2015-01-30 19:31:44 +00:00
|
|
|
*/
|
2015-03-23 03:06:28 +00:00
|
|
|
protected function getTemplateFilename( $templateName ) {
|
2017-03-14 04:01:09 +00:00
|
|
|
// Prevent path traversal. Based on Language::isValidCode().
|
|
|
|
|
// This is for paranoia. The $templateName should never come from
|
|
|
|
|
// untrusted input.
|
2015-01-30 19:31:44 +00:00
|
|
|
if (
|
2017-03-14 04:01:09 +00:00
|
|
|
strcspn( $templateName, ":/\\\000&<>'\"%" ) !== strlen( $templateName )
|
2015-01-30 19:31:44 +00:00
|
|
|
) {
|
2015-03-12 21:31:32 +00:00
|
|
|
throw new UnexpectedValueException( "Malformed \$templateName: $templateName" );
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "{$this->templateDir}/{$templateName}.mustache";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a given template function if found, otherwise throws an exception.
|
|
|
|
|
* @param string $templateName The name of the template (without file suffix)
|
2015-03-22 22:01:52 +00:00
|
|
|
* @return callable
|
2020-02-26 14:54:55 +00:00
|
|
|
* @throws RuntimeException When the template file cannot be found
|
|
|
|
|
* @throws RuntimeException When the compiled template isn't callable. This is indicative of a
|
|
|
|
|
* bug in LightnCandy
|
2015-01-30 19:31:44 +00:00
|
|
|
*/
|
2015-03-23 03:06:28 +00:00
|
|
|
protected function getTemplate( $templateName ) {
|
2017-09-10 03:08:43 +00:00
|
|
|
$templateKey = $templateName . '|' . $this->compileFlags;
|
|
|
|
|
|
2015-01-30 19:31:44 +00:00
|
|
|
// If a renderer has already been defined for this template, reuse it
|
2017-09-10 03:08:43 +00:00
|
|
|
if ( isset( $this->renderers[$templateKey] ) &&
|
|
|
|
|
is_callable( $this->renderers[$templateKey] )
|
2015-09-27 08:10:34 +00:00
|
|
|
) {
|
2017-09-10 03:08:43 +00:00
|
|
|
return $this->renderers[$templateKey];
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-20 18:50:17 +00:00
|
|
|
// Fetch a secret key for building a keyed hash of the PHP code
|
2016-11-22 23:39:22 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig();
|
2015-03-03 22:19:35 +00:00
|
|
|
$secretKey = $config->get( 'SecretKey' );
|
|
|
|
|
|
|
|
|
|
if ( $secretKey ) {
|
2020-02-26 15:52:46 +00:00
|
|
|
// See if the compiled PHP code is stored in the server-local cache.
|
2020-03-18 23:01:07 +00:00
|
|
|
$key = $this->cache->makeKey(
|
2020-02-26 15:52:46 +00:00
|
|
|
'lightncandy-compiled',
|
2020-03-18 23:01:07 +00:00
|
|
|
self::CACHE_VERSION,
|
2020-02-26 15:52:46 +00:00
|
|
|
$this->compileFlags,
|
2020-03-18 21:57:34 +00:00
|
|
|
$this->templateDir,
|
2020-02-26 15:52:46 +00:00
|
|
|
$templateName
|
|
|
|
|
);
|
2020-03-18 23:01:07 +00:00
|
|
|
$compiledTemplate = $this->cache->get( $key );
|
2020-02-26 15:52:46 +00:00
|
|
|
|
|
|
|
|
// 1. Has the template changed since the compiled template was cached? If so, don't use
|
|
|
|
|
// the cached code.
|
|
|
|
|
if ( $compiledTemplate ) {
|
2020-02-27 12:22:32 +00:00
|
|
|
$filesHash = FileContentsHasher::getFileContentsHash( $compiledTemplate['files'] );
|
2020-02-26 15:52:46 +00:00
|
|
|
|
2020-02-27 12:22:32 +00:00
|
|
|
if ( $filesHash !== $compiledTemplate['filesHash'] ) {
|
2020-02-26 15:52:46 +00:00
|
|
|
$compiledTemplate = null;
|
2015-03-03 22:19:35 +00:00
|
|
|
}
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|
2017-05-16 22:40:08 +00:00
|
|
|
|
2020-02-26 15:52:46 +00:00
|
|
|
// 2. Is the integrity of the cached PHP code compromised? If so, don't use the cached
|
|
|
|
|
// code.
|
|
|
|
|
if ( $compiledTemplate ) {
|
|
|
|
|
$integrityHash = hash_hmac( 'sha256', $compiledTemplate['phpCode'], $secretKey );
|
|
|
|
|
|
|
|
|
|
if ( $integrityHash !== $compiledTemplate['integrityHash'] ) {
|
|
|
|
|
$compiledTemplate = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We're not using the cached code for whathever reason. Recompile the template and
|
|
|
|
|
// cache it.
|
|
|
|
|
if ( !$compiledTemplate ) {
|
|
|
|
|
$compiledTemplate = $this->compile( $templateName );
|
|
|
|
|
|
|
|
|
|
$compiledTemplate['integrityHash'] = hash_hmac(
|
|
|
|
|
'sha256',
|
|
|
|
|
$compiledTemplate['phpCode'],
|
|
|
|
|
$secretKey
|
|
|
|
|
);
|
|
|
|
|
|
2020-03-18 23:01:07 +00:00
|
|
|
$this->cache->set( $key, $compiledTemplate, self::CACHE_TTL );
|
2017-05-16 22:40:08 +00:00
|
|
|
}
|
2020-02-26 15:52:46 +00:00
|
|
|
|
2015-03-03 22:19:35 +00:00
|
|
|
// If there is no secret key available, don't use cache
|
|
|
|
|
} else {
|
2020-02-26 15:52:46 +00:00
|
|
|
$compiledTemplate = $this->compile( $templateName );
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-26 15:52:46 +00:00
|
|
|
$renderer = eval( $compiledTemplate['phpCode'] );
|
2015-03-20 00:09:13 +00:00
|
|
|
if ( !is_callable( $renderer ) ) {
|
2020-02-26 14:54:55 +00:00
|
|
|
throw new RuntimeException( "Compiled template `{$templateName}` is not callable" );
|
2015-03-20 00:09:13 +00:00
|
|
|
}
|
2017-09-10 03:08:43 +00:00
|
|
|
$this->renderers[$templateKey] = $renderer;
|
2015-06-17 20:12:47 +00:00
|
|
|
return $renderer;
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-26 15:52:46 +00:00
|
|
|
* Compile the Mustache template into PHP code using LightnCandy.
|
|
|
|
|
*
|
|
|
|
|
* The compilation step generates both PHP code and metadata, which is also returned in the
|
|
|
|
|
* result. An example result looks as follows:
|
|
|
|
|
*
|
|
|
|
|
* ```php
|
|
|
|
|
* [
|
|
|
|
|
* 'phpCode' => '...',
|
2020-02-27 12:22:32 +00:00
|
|
|
* 'files' => [
|
|
|
|
|
* '/path/to/template.mustache',
|
|
|
|
|
* '/path/to/partial1.mustache',
|
|
|
|
|
* '/path/to/partial2.mustache',
|
|
|
|
|
* 'filesHash' => '...'
|
2020-02-26 15:52:46 +00:00
|
|
|
* ]
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2020-02-27 12:22:32 +00:00
|
|
|
* The `files` entry is a list of the files read during the compilation of the template. Each
|
|
|
|
|
* entry is the fully-qualified filename, i.e. it includes path information.
|
2020-02-26 15:52:46 +00:00
|
|
|
*
|
2020-02-27 12:22:32 +00:00
|
|
|
* The `filesHash` entry can be used to determine whether the template has changed since it was
|
|
|
|
|
* last compiled without compiling the template again. Currently, the `filesHash` entry is
|
2020-02-26 15:52:46 +00:00
|
|
|
* generated with FileContentsHasher::getFileContentsHash.
|
|
|
|
|
*
|
|
|
|
|
* @param string $templateName The name of the template
|
|
|
|
|
* @return array An associative array containing the PHP code and metadata about its compilation
|
2020-02-26 14:54:55 +00:00
|
|
|
* @throws Exception Thrown by LightnCandy if it could not compile the Mustache code
|
|
|
|
|
* @throws RuntimeException If LightnCandy could not compile the Mustache code but did not throw
|
|
|
|
|
* an exception. This exception is indicative of a bug in LightnCandy
|
2017-04-04 20:31:42 +00:00
|
|
|
* @suppress PhanTypeMismatchArgument
|
2015-01-30 19:31:44 +00:00
|
|
|
*/
|
2020-02-26 15:52:46 +00:00
|
|
|
protected function compile( $templateName ) {
|
|
|
|
|
$filename = $this->getTemplateFilename( $templateName );
|
|
|
|
|
|
|
|
|
|
if ( !file_exists( $filename ) ) {
|
|
|
|
|
throw new RuntimeException( "Could not find template `{$templateName}` at {$filename}" );
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 12:22:32 +00:00
|
|
|
$files = [ $filename ];
|
2020-02-26 15:52:46 +00:00
|
|
|
$contents = file_get_contents( $filename );
|
2020-01-15 23:57:44 +00:00
|
|
|
$compiled = LightnCandy::compile(
|
2020-02-26 15:52:46 +00:00
|
|
|
$contents,
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2017-09-10 03:08:43 +00:00
|
|
|
'flags' => $this->compileFlags,
|
2015-07-07 01:32:21 +00:00
|
|
|
'basedir' => $this->templateDir,
|
|
|
|
|
'fileext' => '.mustache',
|
2020-02-27 12:22:32 +00:00
|
|
|
'partialresolver' => function ( $cx, $partialName ) use ( $templateName, &$files ) {
|
2020-02-26 15:52:46 +00:00
|
|
|
$filename = "{$this->templateDir}/{$partialName}.mustache";
|
|
|
|
|
if ( !file_exists( $filename ) ) {
|
|
|
|
|
throw new RuntimeException( sprintf(
|
|
|
|
|
'Could not compile template `%s`: Could not find partial `%s` at %s',
|
|
|
|
|
$templateName,
|
|
|
|
|
$partialName,
|
|
|
|
|
$filename
|
|
|
|
|
) );
|
2017-04-04 20:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-26 15:52:46 +00:00
|
|
|
$fileContents = file_get_contents( $filename );
|
2017-04-04 20:31:42 +00:00
|
|
|
|
|
|
|
|
if ( $fileContents === false ) {
|
2020-02-26 15:52:46 +00:00
|
|
|
throw new RuntimeException( sprintf(
|
|
|
|
|
'Could not compile template `%s`: Could not find partial `%s` at %s',
|
|
|
|
|
$templateName,
|
|
|
|
|
$partialName,
|
|
|
|
|
$filename
|
|
|
|
|
) );
|
2017-04-04 20:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-27 12:22:32 +00:00
|
|
|
$files[] = $filename;
|
|
|
|
|
|
2017-04-04 20:31:42 +00:00
|
|
|
return $fileContents;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
2015-01-30 19:31:44 +00:00
|
|
|
);
|
2020-01-15 23:57:44 +00:00
|
|
|
if ( !$compiled ) {
|
|
|
|
|
// This shouldn't happen because LightnCandy::FLAG_ERROR_EXCEPTION is set
|
|
|
|
|
// Errors should throw exceptions instead of returning false
|
|
|
|
|
// Check anyway for paranoia
|
2020-02-26 14:54:55 +00:00
|
|
|
throw new RuntimeException( "Could not compile template `{$filename}`" );
|
2020-01-15 23:57:44 +00:00
|
|
|
}
|
2020-02-26 15:52:46 +00:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'phpCode' => $compiled,
|
2020-02-27 12:22:32 +00:00
|
|
|
'files' => $files,
|
|
|
|
|
'filesHash' => FileContentsHasher::getFileContentsHash( $files ),
|
2020-02-26 15:52:46 +00:00
|
|
|
];
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns HTML for a given template by calling the template function with the given args
|
2015-02-20 19:09:43 +00:00
|
|
|
*
|
|
|
|
|
* @code
|
|
|
|
|
* echo $templateParser->processTemplate(
|
|
|
|
|
* 'ExampleTemplate',
|
2016-09-27 03:15:08 +00:00
|
|
|
* [
|
2015-02-20 19:09:43 +00:00
|
|
|
* 'username' => $user->getName(),
|
|
|
|
|
* 'message' => 'Hello!'
|
2016-09-27 03:15:08 +00:00
|
|
|
* ]
|
2015-02-20 19:09:43 +00:00
|
|
|
* );
|
|
|
|
|
* @endcode
|
2015-01-30 19:31:44 +00:00
|
|
|
* @param string $templateName The name of the template
|
2018-07-12 02:48:34 +00:00
|
|
|
* @param-taint $templateName exec_misc
|
2015-01-30 19:31:44 +00:00
|
|
|
* @param mixed $args
|
2018-07-12 02:48:34 +00:00
|
|
|
* @param-taint $args none
|
2015-01-30 19:31:44 +00:00
|
|
|
* @param array $scopes
|
2018-07-12 02:48:34 +00:00
|
|
|
* @param-taint $scopes none
|
2015-01-30 19:31:44 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function processTemplate( $templateName, $args, array $scopes = [] ) {
|
2015-01-30 19:31:44 +00:00
|
|
|
$template = $this->getTemplate( $templateName );
|
2019-03-08 03:25:40 +00:00
|
|
|
return $template( $args, $scopes );
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
}
|