Jeroen De Dauw has submitted this change and it was merged. Change subject: Add parsing option to the runners ......................................................................
Add parsing option to the runners Change-Id: I32810796f9740635f225d95274cce04e0dd55008 --- M src/ParserHooks/FunctionRunner.php M src/ParserHooks/HookRegistrant.php M src/ParserHooks/HookRunner.php A src/ParserHooks/Internal/Runner.php 4 files changed, 193 insertions(+), 118 deletions(-) Approvals: Jeroen De Dauw: Verified; Looks good to me, approved diff --git a/src/ParserHooks/FunctionRunner.php b/src/ParserHooks/FunctionRunner.php index 3a839de..443aa7a 100644 --- a/src/ParserHooks/FunctionRunner.php +++ b/src/ParserHooks/FunctionRunner.php @@ -2,8 +2,8 @@ namespace ParserHooks; -use ParamProcessor\Processor; use Parser; +use ParserHooks\Internal\Runner; /** * Class that handles a parser function hook call coming from MediaWiki @@ -15,46 +15,9 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroended...@gmail.com > */ -class FunctionRunner { +class FunctionRunner extends Runner { - /** - * @since 1.0 - * - * @var HookDefinition - */ - protected $definition; - - /** - * @since 1.0 - * - * @var HookHandler - */ - protected $handler; - - /** - * @since 1.0 - * - * @var Processor - */ - protected $paramProcessor; - - /** - * @since 1.0 - * - * @param HookDefinition $definition - * @param HookHandler $handler - * @param Processor|null $paramProcessor - */ - public function __construct( HookDefinition $definition, HookHandler $handler, Processor $paramProcessor = null ) { - $this->definition = $definition; - $this->handler = $handler; - - if ( $paramProcessor === null ) { - $paramProcessor = Processor::newDefault(); - } - - $this->paramProcessor = $paramProcessor; - } + const OPT_DO_PARSE = 'parse'; // Boolean, since 1.1 /** * @since 1.0 @@ -62,41 +25,53 @@ * @param Parser $parser * @param string|string[] $arguments * - * @return mixed + * @return array */ public function run( Parser &$parser, $arguments ) { - if ( is_string( $arguments ) ) { - $arguments = explode( '|', $arguments ); + $resultText = $this->handler->handle( + $parser, + $this->getProcessedParams( $arguments ) + ); + + return $this->getResultStructure( $resultText ); + } + + protected function getProcessedParams( $rawArguments ) { + if ( is_string( $rawArguments ) ) { + $rawArguments = explode( '|', $rawArguments ); } $this->paramProcessor->setFunctionParams( - $arguments, + $rawArguments, $this->definition->getParameters(), $this->definition->getDefaultParameters() ); - return $this->handler->handle( - $parser, - $this->paramProcessor->processParameters() + return $this->paramProcessor->processParameters(); + } + + protected function getResultStructure( $resultText ) { + $result = array( $resultText ); + + if ( !$this->getOption( self::OPT_DO_PARSE ) ) { + $result['noparse'] = true; + $result['isHTML'] = true; + } + + return $result; + } + + /** + * @see Runner::getDefaultOptions + * + * @since 1.1 + * + * @return array + */ + protected function getDefaultOptions() { + return array( + self::OPT_DO_PARSE => true, ); - } - - /** - * @since 1.0 - * - * @return HookHandler - */ - public function getHandler() { - return $this->handler; - } - - /** - * @since 1.0 - * - * @return HookDefinition - */ - public function getDefinition() { - return $this->definition; } } diff --git a/src/ParserHooks/HookRegistrant.php b/src/ParserHooks/HookRegistrant.php index 217a340..c2e7476 100644 --- a/src/ParserHooks/HookRegistrant.php +++ b/src/ParserHooks/HookRegistrant.php @@ -3,6 +3,7 @@ namespace ParserHooks; use Parser; +use PPFrame; /** * Parser hook runner registrant. @@ -40,7 +41,7 @@ foreach ( $runner->getDefinition()->getNames() as $name ) { $this->parser->setFunctionHook( $name, - function( Parser $parser, $frame, $arguments ) use ( $runner ) { + function( Parser $parser, $frame, $arguments ) use ( $runner ) { return $runner->run( $parser, $arguments ); } ); @@ -68,8 +69,8 @@ foreach ( $runner->getDefinition()->getNames() as $name ) { $this->parser->setHook( $name, - function( $text, array $arguments, Parser $parser, $frame ) use ( $runner ) { - return $runner->run( $text, $arguments, $parser ); + function( $text, array $arguments, Parser $parser, PPFrame $frame ) use ( $runner ) { + return $runner->run( $text, $arguments, $parser, $frame ); } ); } diff --git a/src/ParserHooks/HookRunner.php b/src/ParserHooks/HookRunner.php index cc0eb56..7d95a59 100644 --- a/src/ParserHooks/HookRunner.php +++ b/src/ParserHooks/HookRunner.php @@ -4,6 +4,8 @@ use ParamProcessor\Processor; use Parser; +use ParserHooks\Internal\Runner; +use PPFrame; /** * Class that handles a parser hook hook call coming from MediaWiki @@ -15,46 +17,19 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroended...@gmail.com > */ -class HookRunner { +class HookRunner extends Runner { + + const OPT_DO_PARSE = 'parse'; // Boolean, since 1.1 /** - * @since 1.1 - * - * @var HookDefinition + * @var Parser */ - protected $definition; + private $parser; /** - * @since 1.1 - * - * @var HookHandler + * @var PPFrame */ - protected $handler; - - /** - * @since 1.1 - * - * @var Processor - */ - protected $paramProcessor; - - /** - * @since 1.1 - * - * @param HookDefinition $definition - * @param HookHandler $handler - * @param Processor|null $paramProcessor - */ - public function __construct( HookDefinition $definition, HookHandler $handler, Processor $paramProcessor = null ) { - $this->definition = $definition; - $this->handler = $handler; - - if ( $paramProcessor === null ) { - $paramProcessor = Processor::newDefault(); - } - - $this->paramProcessor = $paramProcessor; - } + private $frame; /** * @since 1.1 @@ -62,16 +37,18 @@ * @param string $text * @param string[] $arguments * @param Parser $parser + * @param PPFrame $frame * * @return mixed */ - public function run( $text, array $arguments, Parser &$parser ) { - $arguments = $this->getRawArgsList( $text, $arguments ); + public function run( $text, array $arguments, Parser &$parser, PPFrame $frame ) { + $this->parser = $parser; + $this->frame = $frame; - return $this->handler->handle( - $parser, - $this->getProcessedArgs( $arguments ) - ); + $rawArgs = $this->getRawArgsList( $text, $arguments ); + $resultText = $this->getResultText( $rawArgs ); + + return $this->getProcessedResultText( $resultText ); } protected function getRawArgsList( $text, array $arguments ) { @@ -86,6 +63,13 @@ return $arguments; } + protected function getResultText( array $rawArgs ) { + return $this->handler->handle( + $this->parser, + $this->getProcessedArgs( $rawArgs ) + ); + } + protected function getProcessedArgs( array $rawArgs ) { $this->paramProcessor->setParameters( $rawArgs, @@ -95,22 +79,25 @@ return $this->paramProcessor->processParameters(); } - /** - * @since 1.1 - * - * @return HookHandler - */ - public function getHandler() { - return $this->handler; + protected function getProcessedResultText( $resultText ) { + if ( $this->getOption( self::OPT_DO_PARSE ) ) { + return $this->parser->recursiveTagParse( $resultText, $this->frame ); + } + + return $resultText; } /** + * @see Runner::getDefaultOptions + * * @since 1.1 * - * @return HookDefinition + * @return array */ - public function getDefinition() { - return $this->definition; + protected function getDefaultOptions() { + return array( + self::OPT_DO_PARSE => true, + ); } } diff --git a/src/ParserHooks/Internal/Runner.php b/src/ParserHooks/Internal/Runner.php new file mode 100644 index 0000000..3e42c8a --- /dev/null +++ b/src/ParserHooks/Internal/Runner.php @@ -0,0 +1,112 @@ +<?php + +namespace ParserHooks\Internal; + +use ParamProcessor\Processor; +use Parser; +use ParserHooks\HookDefinition; +use ParserHooks\HookHandler; + +/** + * @since 1.1 + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +abstract class Runner { + + /** + * @since 1.1 + * + * @var HookDefinition + */ + protected $definition; + + /** + * @since 1.1 + * + * @var HookHandler + */ + protected $handler; + + /** + * @since 1.1 + * + * @var array + */ + private $options; + + /** + * @since 1.1 + * + * @var Processor + */ + protected $paramProcessor; + + /** + * @since 1.1 + * + * @param HookDefinition $definition + * @param HookHandler $handler + * @param array $options + * @param Processor|null $paramProcessor + */ + public function __construct( HookDefinition $definition, HookHandler $handler, array $options = array(), Processor $paramProcessor = null ) { + $this->definition = $definition; + $this->handler = $handler; + + $this->setParamProcessor( $paramProcessor ); + $this->setOptions( $options ); + } + + private function setParamProcessor( $paramProcessor ) { + if ( $paramProcessor === null ) { + $paramProcessor = Processor::newDefault(); + } + + $this->paramProcessor = $paramProcessor; + } + + private function setOptions( array $options ) { + $this->options = array_merge( + $this->getDefaultOptions(), + $options + ); + } + + /** + * @since 1.1 + * + * @param string $name + * + * @return mixed + */ + protected function getOption( $name ) { + return $this->options[$name]; + } + + /** + * @since 1.1 + * + * @return array + */ + protected abstract function getDefaultOptions(); + + /** + * @since 1.1 + * + * @return HookHandler + */ + public function getHandler() { + return $this->handler; + } + + /** + * @since 1.1 + * + * @return HookDefinition + */ + public function getDefinition() { + return $this->definition; + } + +} -- To view, visit https://gerrit.wikimedia.org/r/85705 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I32810796f9740635f225d95274cce04e0dd55008 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ParserHooks Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits