Create HookRunner class and the hook handler interfaces

Bug: T263353
Change-Id: I7ee943a262eba7d2f00fd05925676cc2351b7be9
This commit is contained in:
Umherirrender 2023-06-05 22:47:23 +02:00
parent 3263379318
commit 28c5a67697
7 changed files with 118 additions and 11 deletions

View file

@ -3,6 +3,7 @@
namespace MediaWiki\Extension\Math\HookHandlers;
use FatalError;
use MediaWiki\Extension\Math\Hooks\HookRunner;
use MediaWiki\Extension\Math\MathConfig;
use MediaWiki\Extension\Math\MathMathML;
use MediaWiki\Extension\Math\MathMathMLCli;
@ -39,8 +40,8 @@ class ParserHooksHandler implements
/** @var UserOptionsLookup */
private $userOptionsLookup;
/** @var HookContainer */
private $hookContainer;
/** @var HookRunner */
private $hookRunner;
/**
* @param RendererFactory $rendererFactory
@ -54,7 +55,7 @@ class ParserHooksHandler implements
) {
$this->rendererFactory = $rendererFactory;
$this->userOptionsLookup = $userOptionsLookup;
$this->hookContainer = $hookContainer;
$this->hookRunner = new HookRunner( $hookContainer );
}
/**
@ -138,9 +139,8 @@ class ParserHooksHandler implements
$renderer->addTrackingCategories( $parser );
return $renderer->getLastError();
}
// TODO: Convert to a new style hook system
$this->hookContainer->run( 'MathFormulaPostRender',
[ $parser, $renderer, &$renderedMath ]
$this->hookRunner->onMathFormulaPostRender(
$parser, $renderer, $renderedMath
); // Enables indexing of math formula
// Writes cache if rendering was successful

43
src/Hooks/HookRunner.php Normal file
View file

@ -0,0 +1,43 @@
<?php
namespace MediaWiki\Extension\Math\Hooks;
use MediaWiki\Extension\Math\MathRenderer;
use MediaWiki\HookContainer\HookContainer;
use Parser;
use stdClass;
/**
* This is a hook runner class, see docs/Hooks.md in core.
* @internal
*/
class HookRunner implements
MathFormulaPostRenderHook,
MathRenderingResultRetrievedHook
{
private HookContainer $hookContainer;
public function __construct( HookContainer $hookContainer ) {
$this->hookContainer = $hookContainer;
}
/**
* @inheritDoc
*/
public function onMathFormulaPostRender( Parser $parser, MathRenderer $renderer, string &$renderedMath ) {
return $this->hookContainer->run(
'MathFormulaPostRender',
[ $parser, $renderer, &$renderedMath ]
);
}
/**
* @inheritDoc
*/
public function onMathRenderingResultRetrieved( MathRenderer &$renderer, stdClass &$jsonResult ) {
return $this->hookContainer->run(
'MathRenderingResultRetrieved',
[ &$renderer, &$jsonResult ]
);
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace MediaWiki\Extension\Math\Hooks;
use MediaWiki\Extension\Math\MathRenderer;
use Parser;
/**
* This is a hook handler interface, see docs/Hooks.md in core.
* Use the hook name "MathFormulaPostRender" to register handlers implementing this interface.
*
* @stable to implement
* @ingroup Hooks
*/
interface MathFormulaPostRenderHook {
/**
* @param Parser $parser
* @param MathRenderer $renderer
* @param string &$renderedMath
* @return bool|void True or no return value to continue or false to abort
*/
public function onMathFormulaPostRender( Parser $parser, MathRenderer $renderer, string &$renderedMath );
}

View file

@ -0,0 +1,22 @@
<?php
namespace MediaWiki\Extension\Math\Hooks;
use MediaWiki\Extension\Math\MathRenderer;
use stdClass;
/**
* This is a hook handler interface, see docs/Hooks.md in core.
* Use the hook name "MathRenderingResultRetrieved" to register handlers implementing this interface.
*
* @stable to implement
* @ingroup Hooks
*/
interface MathRenderingResultRetrievedHook {
/**
* @param MathRenderer &$renderer
* @param stdClass &$jsonResult
* @return bool|void True or no return value to continue or false to abort
*/
public function onMathRenderingResultRetrieved( MathRenderer &$renderer, stdClass &$jsonResult );
}

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Extension\Math;
use MediaWiki\Extension\Math\Hooks\HookRunner;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use StatusValue;
@ -117,8 +118,9 @@ class MathLaTeXML extends MathMathML {
$this->setMathml( $jsonResult->result );
// Avoid PHP 7.1 warning from passing $this by reference
$renderer = $this;
MediaWikiServices::getInstance()->getHookContainer()->run( 'MathRenderingResultRetrieved',
[ &$renderer, &$jsonResult ]
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
->onMathRenderingResultRetrieved(
$renderer, $jsonResult
); // Enables debugging of server results
return StatusValue::newGood();
}

View file

@ -9,6 +9,7 @@
namespace MediaWiki\Extension\Math;
use Html;
use MediaWiki\Extension\Math\Hooks\HookRunner;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use Psr\Log\LoggerInterface;
@ -547,8 +548,8 @@ class MathMathML extends MathRenderer {
}
// Avoid PHP 7.1 warning from passing $this by reference
$renderer = $this;
MediaWikiServices::getInstance()->getHookContainer()->run( 'MathRenderingResultRetrieved',
[ &$renderer, &$jsonResult ]
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onMathRenderingResultRetrieved(
$renderer, $jsonResult
); // Enables debugging of server results
return StatusValue::newGood(); // FIXME: empty?
} else {

View file

@ -0,0 +1,16 @@
<?php
namespace MediaWiki\Extension\Math\Tests\Unit;
use MediaWiki\Extension\Math\Hooks\HookRunner;
use MediaWiki\Tests\HookContainer\HookRunnerTestBase;
/**
* @covers \MediaWiki\Extension\Math\Hooks\HookRunner
*/
class HookRunnerTest extends HookRunnerTestBase {
public static function provideHookRunners() {
yield HookRunner::class => [ HookRunner::class ];
}
}