diff --git a/autoload.php b/autoload.php index 68e2e8f9d4b..b31362efab9 100644 --- a/autoload.php +++ b/autoload.php @@ -967,6 +967,7 @@ $wgAutoloadLocalClasses = [ 'MediaWiki\\Hook\\ParserLimitReportFormatHook' => __DIR__ . '/includes/parser/Hook/ParserLimitReportFormatHook.php', 'MediaWiki\\Hook\\ParserLimitReportPrepareHook' => __DIR__ . '/includes/parser/Hook/ParserLimitReportPrepareHook.php', 'MediaWiki\\Hook\\ParserMakeImageParamsHook' => __DIR__ . '/includes/parser/Hook/ParserMakeImageParamsHook.php', + 'MediaWiki\\Hook\\ParserModifyImageHTML' => __DIR__ . '/includes/parser/Hook/ParserModifyImageHTML.php', 'MediaWiki\\Hook\\ParserOptionsRegisterHook' => __DIR__ . '/includes/parser/Hook/ParserOptionsRegisterHook.php', 'MediaWiki\\Hook\\ParserOutputPostCacheTransformHook' => __DIR__ . '/includes/parser/Hook/ParserOutputPostCacheTransformHook.php', 'MediaWiki\\Hook\\ParserPreSaveTransformCompleteHook' => __DIR__ . '/includes/parser/Hook/ParserPreSaveTransformCompleteHook.php', diff --git a/includes/HookContainer/HookRunner.php b/includes/HookContainer/HookRunner.php index fc4646e790b..3dcc0eb5422 100644 --- a/includes/HookContainer/HookRunner.php +++ b/includes/HookContainer/HookRunner.php @@ -4,6 +4,7 @@ namespace MediaWiki\HookContainer; use Article; use Config; +use File; use IContextSource; use ManualLogEntry; use MediaWiki\Linker\LinkRenderer; @@ -12,6 +13,7 @@ use MediaWiki\Page\ProperPageIdentity; use MediaWiki\Permissions\Authority; use MediaWiki\Revision\RevisionRecord; use MediaWiki\User\UserIdentity; +use Parser; use ParserOptions; use ResourceLoaderContext; use Skin; @@ -287,6 +289,7 @@ class HookRunner implements \MediaWiki\Hook\ParserLimitReportFormatHook, \MediaWiki\Hook\ParserLimitReportPrepareHook, \MediaWiki\Hook\ParserMakeImageParamsHook, + \MediaWiki\Hook\ParserModifyImageHTML, \MediaWiki\Hook\ParserOptionsRegisterHook, \MediaWiki\Hook\ParserOutputPostCacheTransformHook, \MediaWiki\Hook\ParserPreSaveTransformCompleteHook, @@ -2915,6 +2918,16 @@ class HookRunner implements ); } + public function onParserModifyImageHTML( Parser $parser, File $file, + array $params, string &$html + ): void { + $this->container->run( + 'ParserModifyImageHTML', + [ $parser, $file, $params, &$html ], + [ 'abortable' => false ] + ); + } + public function onParserOptionsRegister( &$defaults, &$inCacheKey, &$lazyLoad ) { return $this->container->run( 'ParserOptionsRegister', diff --git a/includes/gallery/TraditionalImageGallery.php b/includes/gallery/TraditionalImageGallery.php index 3340963b3e7..b5e155a1efa 100644 --- a/includes/gallery/TraditionalImageGallery.php +++ b/includes/gallery/TraditionalImageGallery.php @@ -197,10 +197,16 @@ class TraditionalImageGallery extends ImageGalleryBase { ], $thumbhtml ); // Call parser transform hook - /** @var MediaHandler $handler */ - $handler = $img->getHandler(); - if ( $resolveFilesViaParser && $handler ) { - $handler->parserTransformHook( $this->mParser, $img ); + if ( $resolveFilesViaParser ) { + /** @var MediaHandler $handler */ + $handler = $img->getHandler(); + if ( $handler ) { + $handler->parserTransformHook( $this->mParser, $img ); + } + if ( $img ) { + $this->mParser->modifyImageHtml( + $img, [ 'handler' => $imageParameters ], $thumbhtml ); + } } } } diff --git a/includes/parser/Hook/ParserModifyImageHTML.php b/includes/parser/Hook/ParserModifyImageHTML.php new file mode 100644 index 00000000000..f51d3d6b52e --- /dev/null +++ b/includes/parser/Hook/ParserModifyImageHTML.php @@ -0,0 +1,30 @@ +parserTransformHook( $this, $file ); } + if ( $file ) { + $this->modifyImageHtml( $file, $params, $ret ); + } return $ret; } @@ -5500,6 +5503,17 @@ class Parser { return [ $type, $target ]; } + /** + * Give hooks a chance to modify image thumbnail HTML + * + * @param File $file + * @param array $params + * @param string &$html + */ + public function modifyImageHtml( File $file, array $params, string &$html ) { + $this->hookRunner->onParserModifyImageHTML( $this, $file, $params, $html ); + } + /** * @param string $caption * @param LinkHolderArray|bool $holders