Inject services into Hooks

Change-Id: I1b91c256b8b864efb29d6dbd41e89884804e001c
This commit is contained in:
Fomafix 2024-12-08 13:07:59 +00:00
parent 64bd35d95f
commit 9afddc19d0
2 changed files with 20 additions and 5 deletions

View file

@ -84,7 +84,11 @@
}, },
"HookHandlers": { "HookHandlers": {
"main": { "main": {
"class": "MediaWiki\\SyntaxHighlight\\Hooks" "class": "MediaWiki\\SyntaxHighlight\\Hooks",
"services": [
"MainConfig",
"ParserFactory"
]
} }
}, },
"ParsoidModules": [ "ParsoidModules": [

View file

@ -3,6 +3,7 @@
namespace MediaWiki\SyntaxHighlight; namespace MediaWiki\SyntaxHighlight;
use MediaWiki\Api\Hook\ApiFormatHighlightHook; use MediaWiki\Api\Hook\ApiFormatHighlightHook;
use MediaWiki\Config\Config;
use MediaWiki\Content\Content; use MediaWiki\Content\Content;
use MediaWiki\Content\Hook\ContentGetParserOutputHook; use MediaWiki\Content\Hook\ContentGetParserOutputHook;
use MediaWiki\Content\TextContent; use MediaWiki\Content\TextContent;
@ -10,8 +11,8 @@ use MediaWiki\Context\IContextSource;
use MediaWiki\Hook\ParserFirstCallInitHook; use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\Hook\SoftwareInfoHook; use MediaWiki\Hook\SoftwareInfoHook;
use MediaWiki\MainConfigNames; use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Parser\Parser; use MediaWiki\Parser\Parser;
use MediaWiki\Parser\ParserFactory;
use MediaWiki\Parser\ParserOptions; use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\ParserOutput; use MediaWiki\Parser\ParserOutput;
use MediaWiki\Parser\Sanitizer; use MediaWiki\Parser\Sanitizer;
@ -34,6 +35,17 @@ class Hooks implements
'text/xml' => 'xml', '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 * Register parser hook
* *
@ -87,11 +99,10 @@ class Hooks implements
$lexer = $models[$model]; $lexer = $models[$model];
$text = $content->getText(); $text = $content->getText();
$config = MediaWikiServices::getInstance()->getMainConfig();
// Parse using the standard parser to get links etc. into the database, HTML is replaced below. // 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'. // We could do this using $content->fillParserOutput(), but alas it is 'protected'.
if ( in_array( $model, $config->get( MainConfigNames::TextModelsToParse ), true ) ) { if ( in_array( $model, $this->config->get( MainConfigNames::TextModelsToParse ), true ) ) {
$parserOutput = MediaWikiServices::getInstance()->getParser() $parserOutput = $this->parserFactory->getInstance()
->parse( $text, $title, $options, true, true, $revId ); ->parse( $text, $title, $options, true, true, $revId );
} }