Merge "Inject services into Hooks"

This commit is contained in:
jenkins-bot 2024-12-09 10:36:08 +00:00 committed by Gerrit Code Review
commit dac1e0002d
2 changed files with 20 additions and 5 deletions

View file

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

View file

@ -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 );
}