From 9afddc19d02012c1b743ac11511e6f0b0775e197 Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sun, 8 Dec 2024 13:07:59 +0000 Subject: [PATCH] Inject services into Hooks Change-Id: I1b91c256b8b864efb29d6dbd41e89884804e001c --- extension.json | 6 +++++- includes/Hooks.php | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/extension.json b/extension.json index 3fa12d3f..24360bb0 100644 --- a/extension.json +++ b/extension.json @@ -84,7 +84,11 @@ }, "HookHandlers": { "main": { - "class": "MediaWiki\\SyntaxHighlight\\Hooks" + "class": "MediaWiki\\SyntaxHighlight\\Hooks", + "services": [ + "MainConfig", + "ParserFactory" + ] } }, "ParsoidModules": [ diff --git a/includes/Hooks.php b/includes/Hooks.php index e9758c6f..05783a1c 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -3,6 +3,7 @@ namespace MediaWiki\SyntaxHighlight; use MediaWiki\Api\Hook\ApiFormatHighlightHook; +use MediaWiki\Config\Config; use MediaWiki\Content\Content; use MediaWiki\Content\Hook\ContentGetParserOutputHook; use MediaWiki\Content\TextContent; @@ -10,8 +11,8 @@ use MediaWiki\Context\IContextSource; use MediaWiki\Hook\ParserFirstCallInitHook; use MediaWiki\Hook\SoftwareInfoHook; use MediaWiki\MainConfigNames; -use MediaWiki\MediaWikiServices; use MediaWiki\Parser\Parser; +use MediaWiki\Parser\ParserFactory; use MediaWiki\Parser\ParserOptions; use MediaWiki\Parser\ParserOutput; use MediaWiki\Parser\Sanitizer; @@ -34,6 +35,17 @@ class Hooks implements 'text/xml' => 'xml', ]; + private Config $config; + private ParserFactory $parserFactory; + + public function __construct( + Config $config, + ParserFactory $parserFactory + ) { + $this->config = $config; + $this->parserFactory = $parserFactory; + } + /** * Register parser hook * @@ -87,11 +99,10 @@ class Hooks implements $lexer = $models[$model]; $text = $content->getText(); - $config = MediaWikiServices::getInstance()->getMainConfig(); // Parse using the standard parser to get links etc. into the database, HTML is replaced below. // We could do this using $content->fillParserOutput(), but alas it is 'protected'. - if ( in_array( $model, $config->get( MainConfigNames::TextModelsToParse ), true ) ) { - $parserOutput = MediaWikiServices::getInstance()->getParser() + if ( in_array( $model, $this->config->get( MainConfigNames::TextModelsToParse ), true ) ) { + $parserOutput = $this->parserFactory->getInstance() ->parse( $text, $title, $options, true, true, $revId ); }