From: Gergő Tisza Date: Sun, 10 Sep 2017 03:08:43 +0000 (-0700) Subject: Enable recursive partials in TemplateParser X-Git-Tag: 1.31.0-rc.0~2135^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_aide%28?a=commitdiff_plain;h=49350108bf5f7b4beface0a873cbacadec5f01a2;p=lhc%2Fweb%2Fwiklou.git Enable recursive partials in TemplateParser Recursive partials are the only way to handle tree-like structures such as nested lists. Allow setting FLAG_RUNTIMEPARTIAL in LightnCandy so they can be used. Since this has a slight performance impact (makes partial invocations evaluation-time functions calls instead of compilation-time transclusions) make it optional. Change-Id: Ie37105a9f1ff92e1a79bfcd9f8578965e3d347f0 --- diff --git a/includes/TemplateParser.php b/includes/TemplateParser.php index 2759ff9baa..2293dabbd9 100644 --- a/includes/TemplateParser.php +++ b/includes/TemplateParser.php @@ -38,6 +38,13 @@ class TemplateParser { */ protected $forceRecompile = false; + /** + * @var int Compilation flags passed to LightnCandy + */ + // Do not add more flags here without discussion. + // If you do add more flags, be sure to update unit tests as well. + protected $compileFlags = LightnCandy::FLAG_ERROR_EXCEPTION; + /** * @param string $templateDir * @param bool $forceRecompile @@ -47,6 +54,18 @@ class TemplateParser { $this->forceRecompile = $forceRecompile; } + /** + * Enable/disable the use of recursive partials. + * @param bool $enable + */ + public function enableRecursivePartials( $enable ) { + if ( $enable ) { + $this->compileFlags = $this->compileFlags | LightnCandy::FLAG_RUNTIMEPARTIAL; + } else { + $this->compileFlags = $this->compileFlags & ~LightnCandy::FLAG_RUNTIMEPARTIAL; + } + } + /** * Constructs the location of the the source Mustache template * @param string $templateName The name of the template @@ -73,11 +92,13 @@ class TemplateParser { * @throws RuntimeException */ protected function getTemplate( $templateName ) { + $templateKey = $templateName . '|' . $this->compileFlags; + // If a renderer has already been defined for this template, reuse it - if ( isset( $this->renderers[$templateName] ) && - is_callable( $this->renderers[$templateName] ) + if ( isset( $this->renderers[$templateKey] ) && + is_callable( $this->renderers[$templateKey] ) ) { - return $this->renderers[$templateName]; + return $this->renderers[$templateKey]; } $filename = $this->getTemplateFilename( $templateName ); @@ -90,7 +111,7 @@ class TemplateParser { $fileContents = file_get_contents( $filename ); // Generate a quick hash for cache invalidation - $fastHash = md5( $fileContents ); + $fastHash = md5( $this->compileFlags . '|' . $fileContents ); // Fetch a secret key for building a keyed hash of the PHP code $config = MediaWikiServices::getInstance()->getMainConfig(); @@ -127,7 +148,7 @@ class TemplateParser { if ( !is_callable( $renderer ) ) { throw new RuntimeException( "Requested template, {$templateName}, is not callable" ); } - $this->renderers[$templateName] = $renderer; + $this->renderers[$templateKey] = $renderer; return $renderer; } @@ -168,9 +189,7 @@ class TemplateParser { return LightnCandy::compile( $code, [ - // Do not add more flags here without discussion. - // If you do add more flags, be sure to update unit tests as well. - 'flags' => LightnCandy::FLAG_ERROR_EXCEPTION, + 'flags' => $this->compileFlags, 'basedir' => $this->templateDir, 'fileext' => '.mustache', ] diff --git a/tests/phpunit/data/templates/recurse.mustache b/tests/phpunit/data/templates/recurse.mustache new file mode 100644 index 0000000000..391f227e3b --- /dev/null +++ b/tests/phpunit/data/templates/recurse.mustache @@ -0,0 +1 @@ +r{{#r}}{{>recurse}}{{/r}} \ No newline at end of file diff --git a/tests/phpunit/includes/TemplateParserTest.php b/tests/phpunit/includes/TemplateParserTest.php index 2bd9086d60..c161f85306 100644 --- a/tests/phpunit/includes/TemplateParserTest.php +++ b/tests/phpunit/includes/TemplateParserTest.php @@ -109,4 +109,17 @@ class TemplateParserTest extends MediaWikiTestCase { ], ]; } + + public function testEnableRecursivePartials() { + $tp = new TemplateParser( $this->templateDir ); + $data = [ 'r' => [ 'r' => [ 'r' => [] ] ] ]; + + $tp->enableRecursivePartials( true ); + $this->assertEquals( 'rrr', $tp->processTemplate( 'recurse', $data ) ); + + $tp->enableRecursivePartials( false ); + $this->setExpectedException( 'Exception' ); + $tp->processTemplate( 'recurse', $data ); + } + }