jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/370940 )
Change subject: Renders Index: pages without relying on their Wikitext serialization ...................................................................... Renders Index: pages without relying on their Wikitext serialization Change-Id: I3f3ffcae202a08acec55061dc07a20f033fa8d69 --- M extension.json M includes/index/IndexContent.php A includes/index/ParserHelper.php A tests/phpunit/index/ParserHelperTest.php 4 files changed, 131 insertions(+), 5 deletions(-) Approvals: Daniel Kinzler: Looks good to me, but someone else must approve jenkins-bot: Verified Samwilson: Looks good to me, but someone else must approve Tpt: Looks good to me, approved diff --git a/extension.json b/extension.json index 258a420..0d39a69 100644 --- a/extension.json +++ b/extension.json @@ -56,6 +56,7 @@ "ProofreadPage\\Index\\IndexDifferenceEngine": "includes/index/IndexDifferenceEngine.php", "ProofreadPage\\Index\\IndexEditAction": "includes/index/IndexEditAction.php", "ProofreadPage\\Index\\IndexSubmitAction": "includes/index/IndexSubmitAction.php", + "ProofreadPage\\Index\\ParserHelper": "includes/index/ParserHelper.php", "ProofreadPage\\Index\\EditIndexPage": "includes/index/EditIndexPage.php", "ProofreadIndexDbConnector": "includes/index/ProofreadIndexDbConnector.php", "ProofreadPage\\Pagination\\PaginationFactory": "includes/Pagination/PaginationFactory.php", diff --git a/includes/index/IndexContent.php b/includes/index/IndexContent.php index 0e113e4..11bc9b9 100644 --- a/includes/index/IndexContent.php +++ b/includes/index/IndexContent.php @@ -5,10 +5,12 @@ use Content; use MagicWord; use MalformedTitleException; +use Parser; use ParserOptions; use ProofreadPage\Link; use ProofreadPage\Pagination\PageList; use Sanitizer; +use ParserOutput; use TextContent; use Title; use User; @@ -170,13 +172,34 @@ } /** - * @see Content::getParserOutput + * @see AbstractContent::fillParserOutput */ - public function getParserOutput( - Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true + protected function fillParserOutput( Title $title, $revId, + ParserOptions $options, $generateHtml, ParserOutput &$output ) { - $wikitextContent = new WikitextContent( $this->serialize( CONTENT_FORMAT_WIKITEXT ) ); - return $wikitextContent->getParserOutput( $title, $revId, $options, $generateHtml ); + /** @var Parser $wgParser */ + global $wgParser; + $parserHelper = new ParserHelper( $title, $options ); + + // We retrieve the view template + list( $templateText, $templateTitle ) = $parserHelper->fetchTemplateTextAndTitle( + Title::makeTitle( NS_MEDIAWIKI, 'Proofreadpage index template' ) + ); + + // We replace the arguments calls by their values + $text = $parserHelper->expandTemplateArgs( + $templateText, + array_map( function ( Content $content ) { + return $content->serialize( CONTENT_FORMAT_WIKITEXT ); + }, $this->fields ) + ); + + // We do the final rendering + $output = $wgParser->parse( $text, $title, $options, true, true, $revId ); + $output->addTemplate( $templateTitle, + $templateTitle->getArticleID(), + $templateTitle->getLatestRevID() + ); } /** diff --git a/includes/index/ParserHelper.php b/includes/index/ParserHelper.php new file mode 100644 index 0000000..80c3af8 --- /dev/null +++ b/includes/index/ParserHelper.php @@ -0,0 +1,46 @@ +<?php + +namespace ProofreadPage\Index; + +use Parser; +use ParserOptions; +use Title; + +/** + * @licence GNU GPL v2+ + * + * Content of a Index: page + */ +class ParserHelper { + + /** + * @var Parser + */ + private $parser; + + public function __construct( Title $title = null, ParserOptions $options ) { + $this->parser = new Parser(); + $this->parser->startExternalParse( $title, $options, Parser::OT_PLAIN ); + } + + /** + * @param string $text the wikitext that should see its template arguments expanded + * @param string[] $args the arguments to use during the expansion + * @return string + */ + public function expandTemplateArgs( $text, array $args ) { + // We build a frame with the arguments for the template + $frame = $this->parser->getPreprocessor()->newCustomFrame( $args ); + + // We replace the arguments calls by their values + $dom = $this->parser->preprocessToDom( $text, Parser::PTD_FOR_INCLUSION ); + $text = $frame->expand( $dom ); + + // We take care of removing the tags placeholders + return $this->parser->mStripState->unstripBoth( $text ); + } + + public function fetchTemplateTextAndTitle( Title $title ) { + return $this->parser->fetchTemplateAndTitle( $title ); + } +} diff --git a/tests/phpunit/index/ParserHelperTest.php b/tests/phpunit/index/ParserHelperTest.php new file mode 100644 index 0000000..2f8000f --- /dev/null +++ b/tests/phpunit/index/ParserHelperTest.php @@ -0,0 +1,56 @@ +<?php + +namespace ProofreadPage\Index; + +use Language; +use ParserOptions; +use ProofreadPageTestCase; +use User; + +/** + * @group ProofreadPage + * @covers \ProofreadPage\Index\ParserHelper + */ +class ParserHelperTest extends ProofreadPageTestCase { + + public function expandTemplateArgsProvider() { + return [ + [ + '{{{foo}}}', + [ 'bar' => 'baz' ], + '{{{foo}}}' + ], + [ + '{{{foo|}}}', + [ 'bar' => 'baz' ], + '{{{foo|}}}' + ], + [ + '{{{bar|}}}', + [ 'bar' => 'baz' ], + 'baz' + ], + [ + '<indicator name="foo"/>{{{bar|}}}', + [ 'bar' => 'baz' ], + '<indicator name="foo"/>baz' + ], + [ + '{{{bar|}}}', + [ 'bar' => '<indicator name="foo"/>baz' ], + '<indicator name="foo"/>baz' + ], + ]; + } + + /** + * @dataProvider expandTemplateArgsProvider + */ + public function testExpandTemplateArgsProvider( $inputText, $args, $outputText ) { + $parserHelper = new ParserHelper( + null, + ParserOptions::newCanonical( new User(), Language::factory( 'en' ) ) + ); + $this->assertEquals( $outputText, $parserHelper->expandTemplateArgs( $inputText, $args ) ); + } +} -- To view, visit https://gerrit.wikimedia.org/r/370940 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I3f3ffcae202a08acec55061dc07a20f033fa8d69 Gerrit-PatchSet: 8 Gerrit-Project: mediawiki/extensions/ProofreadPage Gerrit-Branch: master Gerrit-Owner: Tpt <thoma...@hotmail.fr> Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org> Gerrit-Reviewer: C. Scott Ananian <canan...@wikimedia.org> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Samwilson <s...@samwilson.id.au> Gerrit-Reviewer: Subramanya Sastry <ssas...@wikimedia.org> Gerrit-Reviewer: Tpt <thoma...@hotmail.fr> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits