Use HookHandlers for core hooks

The use of "HookHandlers" attribute in extension.json makes it possible
to inject services into hook handler classes in a future patch.

Bug: T271026
Change-Id: I1583f5075937c4ce71a0d8748700f7012280851c
This commit is contained in:
Umherirrender 2023-08-15 13:33:26 +02:00
parent f61a523ceb
commit 648985c981
3 changed files with 63 additions and 28 deletions

View file

@ -87,19 +87,24 @@
"remoteExtPath": "Scribunto/modules"
},
"Hooks": {
"SoftwareInfo": "MediaWiki\\Extension\\Scribunto\\Hooks::getSoftwareInfo",
"ParserFirstCallInit": "MediaWiki\\Extension\\Scribunto\\Hooks::setupParserHook",
"ParserLimitReportPrepare": "MediaWiki\\Extension\\Scribunto\\Hooks::reportLimitData",
"ParserLimitReportFormat": "MediaWiki\\Extension\\Scribunto\\Hooks::formatLimitData",
"ParserClearState": "MediaWiki\\Extension\\Scribunto\\Hooks::clearState",
"ParserCloned": "MediaWiki\\Extension\\Scribunto\\Hooks::parserCloned",
"SoftwareInfo": "main",
"ParserFirstCallInit": "main",
"ParserLimitReportPrepare": "main",
"ParserLimitReportFormat": "main",
"ParserClearState": "main",
"ParserCloned": "main",
"CodeEditorGetPageLanguage": "MediaWiki\\Extension\\Scribunto\\Hooks::getCodeLanguage",
"EditPage::showStandardInputs:options": "MediaWiki\\Extension\\Scribunto\\Hooks::showStandardInputsOptions",
"EditPage::showReadOnlyForm:initial": "MediaWiki\\Extension\\Scribunto\\Hooks::showReadOnlyFormInitial",
"EditPageBeforeEditButtons": "MediaWiki\\Extension\\Scribunto\\Hooks::beforeEditButtons",
"EditFilterMergedContent": "MediaWiki\\Extension\\Scribunto\\Hooks::validateScript",
"ArticleViewHeader": "MediaWiki\\Extension\\Scribunto\\Hooks::showDocPageHeader",
"ContentHandlerDefaultModelFor": "MediaWiki\\Extension\\Scribunto\\Hooks::contentHandlerDefaultModelFor"
"EditPage::showStandardInputs:options": "main",
"EditPage::showReadOnlyForm:initial": "main",
"EditPageBeforeEditButtons": "main",
"EditFilterMergedContent": "main",
"ArticleViewHeader": "main",
"ContentHandlerDefaultModelFor": "main"
},
"HookHandlers": {
"main": {
"class": "MediaWiki\\Extension\\Scribunto\\Hooks"
}
},
"namespaces": [
{

View file

@ -20,6 +20,8 @@
* http://www.gnu.org/copyleft/gpl.html
*/
// phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
namespace MediaWiki\Extension\Scribunto;
use Article;
@ -28,7 +30,19 @@ use EmptyBagOStuff;
use Html;
use IContextSource;
use MediaWiki\EditPage\EditPage;
use MediaWiki\Hook\EditFilterMergedContentHook;
use MediaWiki\Hook\EditPage__showReadOnlyForm_initialHook;
use MediaWiki\Hook\EditPage__showStandardInputs_optionsHook;
use MediaWiki\Hook\EditPageBeforeEditButtonsHook;
use MediaWiki\Hook\ParserClearStateHook;
use MediaWiki\Hook\ParserClonedHook;
use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\Hook\ParserLimitReportFormatHook;
use MediaWiki\Hook\ParserLimitReportPrepareHook;
use MediaWiki\Hook\SoftwareInfoHook;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\Hook\ArticleViewHeaderHook;
use MediaWiki\Revision\Hook\ContentHandlerDefaultModelForHook;
use MediaWiki\WikiMap\WikiMap;
use ObjectCache;
use OutputPage;
@ -37,6 +51,7 @@ use ParserOutput;
use PPFrame;
use Status;
use Title;
use User;
use UtfNormal\Validator;
use Wikimedia\PSquare;
use Xml;
@ -44,8 +59,20 @@ use Xml;
/**
* Hooks for the Scribunto extension.
*/
class Hooks {
class Hooks implements
SoftwareInfoHook,
ParserFirstCallInitHook,
ParserLimitReportPrepareHook,
ParserLimitReportFormatHook,
ParserClearStateHook,
ParserClonedHook,
EditPage__showStandardInputs_optionsHook,
EditPage__showReadOnlyForm_initialHook,
EditPageBeforeEditButtonsHook,
EditFilterMergedContentHook,
ArticleViewHeaderHook,
ContentHandlerDefaultModelForHook
{
/**
* Define content handler constant upon extension registration
*/
@ -59,7 +86,7 @@ class Hooks {
* @param array &$software
* @return bool
*/
public static function getSoftwareInfo( array &$software ) {
public function onSoftwareInfo( &$software ) {
$engine = Scribunto::newDefaultEngine();
$engine->setTitle( Title::makeTitle( NS_SPECIAL, 'Version' ) );
$engine->getSoftwareInfo( $software );
@ -72,7 +99,7 @@ class Hooks {
* @param Parser $parser
* @return bool
*/
public static function setupParserHook( Parser $parser ) {
public function onParserFirstCallInit( $parser ) {
$parser->setFunctionHook( 'invoke', [ self::class, 'invokeHook' ], Parser::SFH_OBJECT_ARGS );
return true;
}
@ -83,7 +110,7 @@ class Hooks {
* @param Parser $parser
* @return bool
*/
public static function clearState( Parser $parser ) {
public function onParserClearState( $parser ) {
Scribunto::resetParserEngine( $parser );
return true;
}
@ -94,7 +121,7 @@ class Hooks {
* @param Parser $parser
* @return bool
*/
public static function parserCloned( Parser $parser ) {
public function onParserCloned( $parser ) {
$parser->scribunto_engine = null;
return true;
}
@ -273,7 +300,7 @@ class Hooks {
* @param string &$model
* @return bool
*/
public static function contentHandlerDefaultModelFor( Title $title, &$model ) {
public function onContentHandlerDefaultModelFor( $title, &$model ) {
if ( $model === 'sanitized-css' ) {
// Let TemplateStyles override Scribunto
return true;
@ -296,7 +323,7 @@ class Hooks {
* @param ParserOutput $parserOutput
* @return bool
*/
public static function reportLimitData( Parser $parser, ParserOutput $parserOutput ) {
public function onParserLimitReportPrepare( $parser, $parserOutput ) {
if ( Scribunto::isParserEnginePresent( $parser ) ) {
$engine = Scribunto::getParserEngine( $parser );
$engine->reportLimitData( $parserOutput );
@ -314,7 +341,7 @@ class Hooks {
* @param bool $localize
* @return bool
*/
public static function formatLimitData( $key, &$value, &$report, $isHTML, $localize ) {
public function onParserLimitReportFormat( $key, &$value, &$report, $isHTML, $localize ) {
$engine = Scribunto::newDefaultEngine();
return $engine->formatLimitData( $key, $value, $report, $isHTML, $localize );
}
@ -327,7 +354,7 @@ class Hooks {
* @param int &$tab Current tabindex
* @return bool
*/
public static function showStandardInputsOptions( EditPage $editor, OutputPage $output, &$tab ) {
public function onEditPage__showStandardInputs_options( $editor, $output, &$tab ) {
if ( $editor->getTitle()->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
$output->addModules( 'ext.scribunto.edit' );
$editor->editFormTextAfterTools .= '<div id="mw-scribunto-console"></div>';
@ -342,7 +369,7 @@ class Hooks {
* @param OutputPage $output
* @return bool
*/
public static function showReadOnlyFormInitial( EditPage $editor, OutputPage $output ) {
public function onEditPage__showReadOnlyForm_initial( $editor, $output ) {
if ( $editor->getTitle()->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
$output->addModules( 'ext.scribunto.edit' );
$editor->editFormTextAfterContent .= '<div id="mw-scribunto-console"></div>';
@ -358,7 +385,7 @@ class Hooks {
* @param int &$tabindex Current tabindex
* @return bool
*/
public static function beforeEditButtons( EditPage $editor, array &$buttons, &$tabindex ) {
public function onEditPageBeforeEditButtons( $editor, &$buttons, &$tabindex ) {
if ( $editor->getTitle()->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
unset( $buttons['preview'] );
}
@ -369,10 +396,13 @@ class Hooks {
* @param IContextSource $context
* @param Content $content
* @param Status $status
* @param string $summary
* @param User $user
* @param bool $minoredit
* @return bool
*/
public static function validateScript( IContextSource $context, Content $content,
Status $status
public function onEditFilterMergedContent( IContextSource $context, Content $content,
Status $status, $summary, User $user, $minoredit
) {
$title = $context->getTitle();
@ -413,7 +443,7 @@ class Hooks {
* @param bool &$pcache
* @return bool
*/
public static function showDocPageHeader( Article $article, &$outputDone, &$pcache ) {
public function onArticleViewHeader( $article, &$outputDone, &$pcache ) {
$title = $article->getTitle();
if ( Scribunto::isDocPage( $title, $forModule ) ) {
$article->getContext()->getOutput()->addHTML(

View file

@ -32,7 +32,7 @@ class HooksTest extends TestCase {
) {
$title = Title::newFromText( $name );
$model = $before;
$ret = Hooks::contentHandlerDefaultModelFor( $title, $model );
$ret = ( new Hooks )->onContentHandlerDefaultModelFor( $title, $model );
$this->assertSame( $retVal, $ret );
$this->assertSame( $expected, $model );
}