Add a ParserModifyImageHTML hook for PageImages

PageImages is expensively loading and reparsing the lead section during
LinksUpdate because the parser's image link hooks do not give enough
context to tell whether the image is in the lead section.

So, add a hook which allows PageImages to add a marker to image links.
The marker can be associated with the section number in ParserAfterTidy.

Bug: T296895
Bug: T176520
Change-Id: I24528381e8d24ca8d138bceadb9397c83fd31356
This commit is contained in:
Tim Starling 2021-12-02 16:42:04 +11:00
parent 93f79a9122
commit d7cb90dc5e
5 changed files with 68 additions and 4 deletions

View file

@ -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',

View file

@ -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',

View file

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

View file

@ -0,0 +1,30 @@
<?php
namespace MediaWiki\Hook;
use File;
use Parser;
/**
* This is a hook handler interface, see docs/Hooks.md.
* Use the hook name "ParserModifyImageHTML" to register handlers implementing this interface.
*
* @stable to implement
* @ingroup Hooks
*/
interface ParserModifyImageHTML {
/**
* This hook is called for each image added to parser output, with its
* associated HTML as returned from Linker::makeImageLink().
*
* @param Parser $parser
* @param File $file
* @param array $params An associative array of options that were used to generate the HTML.
* Like the one sent to onParserMakeImageParams. The 'handler' element contains handler
* options. The 'frame' element contains frame options. In the image gallery case, "frame"
* will be missing.
* @param string &$html The HTML of the image or image wrapper
*/
public function onParserModifyImageHTML( Parser $parser, File $file,
array $params, string &$html ): void;
}

View file

@ -5453,6 +5453,9 @@ class Parser {
if ( $handler ) {
$handler->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