Output any image candidate markers generated by PageImages (#105)

* Output any image candidate markers generated by PageImages

wikimedia/mediawiki-extensions-PageImages#768464d changed how lead images are identified within PageImages, and since then an HTML comment in the form of `MW-PAGEIMAGES-CANDIDATE-$ID` is output by`doParserModifyImageHTML`, where the ID is an index in the extension output data's list of image metadata. These comments should find a way into the ParserOutput as the extension looks for those comments in a chosen document fragment when identifying qualifying candidates out of all images gathered through `onParserModifyImageHTML`.

This change does not affect compatibility with older PageImages (only the "new" hook's being called already).

* Default htmlAfter to null and update tests to expect it

Follow-up to my previous PageImages commit.
This commit is contained in:
alex4401 2023-07-24 19:13:33 +02:00 committed by GitHub
parent a105effa60
commit eb55d89e77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 18 deletions

View file

@ -7,5 +7,5 @@ interface ExternalParser {
public function replaceVariables( $text ); public function replaceVariables( $text );
public function addImage( $title ); public function addImage( $title ): ?string;
} }

View file

@ -83,8 +83,9 @@ class MediaWikiParserService implements ExternalParser {
* Add image to parser output for later usage * Add image to parser output for later usage
* *
* @param Title $title * @param Title $title
* @return ?string PageImages markers, if any.
*/ */
public function addImage( $title ) { public function addImage( $title ): ?string {
$services = MediaWikiServices::getInstance(); $services = MediaWikiServices::getInstance();
$repoGroup = $services->getRepoGroup(); $repoGroup = $services->getRepoGroup();
@ -94,7 +95,8 @@ class MediaWikiParserService implements ExternalParser {
$sha1 = $file ? $file->getSha1() : null; $sha1 = $file ? $file->getSha1() : null;
$this->parser->getOutput()->addImage( $title->getDBkey(), $tmstmp, $sha1 ); $this->parser->getOutput()->addImage( $title->getDBkey(), $tmstmp, $sha1 );
// Pass PI images to PageImages extension if available (Popups and og:image) // Pass PI images to PageImages extension if available (Popups and og:image). Since 1.38, this produces an HTML
// comment that must be present in the rendered HTML for the image to qualify for selection.
if ( method_exists( if ( method_exists(
ParserFileProcessingHookHandlers::class, 'onParserModifyImageHTML' ParserFileProcessingHookHandlers::class, 'onParserModifyImageHTML'
) ) { ) ) {
@ -113,6 +115,10 @@ class MediaWikiParserService implements ExternalParser {
$handler->onParserModifyImageHTML( $handler->onParserModifyImageHTML(
$this->parser, $file, $params, $html $this->parser, $file, $params, $html
); );
return $html;
} }
return null;
} }
} }

View file

@ -150,10 +150,6 @@ class NodeMedia extends Node {
return []; return [];
} }
if ( $titleObj instanceof Title ) {
$this->getExternalParser()->addImage( $titleObj );
}
$mediatype = $fileObj->getMediaType(); $mediatype = $fileObj->getMediaType();
$image = [ $image = [
'url' => $this->resolveImageUrl( $fileObj ), 'url' => $this->resolveImageUrl( $fileObj ),
@ -164,9 +160,15 @@ class NodeMedia extends Node {
'isVideo' => $mediatype === MEDIATYPE_VIDEO, 'isVideo' => $mediatype === MEDIATYPE_VIDEO,
'isAudio' => $mediatype === MEDIATYPE_AUDIO, 'isAudio' => $mediatype === MEDIATYPE_AUDIO,
'source' => $this->getPrimarySource(), 'source' => $this->getPrimarySource(),
'item-name' => $this->getItemName() 'item-name' => $this->getItemName(),
'htmlAfter' => null,
]; ];
if ( $titleObj instanceof Title ) {
// This call may produce extra HTML from, for example, the PageImages integration
$image['htmlAfter'] = $this->getExternalParser()->addImage( $titleObj );
}
if ( $image['isImage'] ) { if ( $image['isImage'] ) {
$image = array_merge( $image, $helper->extendImageData( $image = array_merge( $image, $helper->extendImageData(
$fileObj, $fileObj,

View file

@ -11,7 +11,8 @@ class SimpleParser implements ExternalParser {
return $text; return $text;
} }
public function addImage( $title ) { public function addImage( $title ): ?string {
// do nothing // do nothing
return null;
} }
} }

View file

@ -3,7 +3,7 @@
{{#if isImage}}<img src="{{thumbnail}}" srcset="{{thumbnail}} 1x, {{thumbnail2x}} 2x" class="pi-image-thumbnail" alt="{{alt}}" width="{{{width}}}" height="{{{height}}}"/> {{#if isImage}}<img src="{{thumbnail}}" srcset="{{thumbnail}} 1x, {{thumbnail2x}} 2x" class="pi-image-thumbnail" alt="{{alt}}" width="{{{width}}}" height="{{{height}}}"/>
{{else}}{{#if isVideo}}<video src="{{url}}" class="pi-video-player" controls="true" controlsList="nodownload" preload="metadata">{{alt}}</video> {{else}}{{#if isVideo}}<video src="{{url}}" class="pi-video-player" controls="true" controlsList="nodownload" preload="metadata">{{alt}}</video>
{{else}}{{#if isAudio}}<audio src="{{url}}" class="pi-audio-player" controls="true" controlsList="nodownload">{{alt}}</audio> {{else}}{{#if isAudio}}<audio src="{{url}}" class="pi-audio-player" controls="true" controlsList="nodownload">{{alt}}</audio>
{{else}}{{alt}}{{/if}}{{/if}}{{/if}} {{else}}{{alt}}{{/if}}{{/if}}{{/if}}{{{htmlAfter}}}
</a> </a>
{{#if caption}}<figcaption class="pi-item-spacing pi-caption">{{{caption}}}</figcaption>{{/if}} {{#if caption}}<figcaption class="pi-item-spacing pi-caption">{{{caption}}}</figcaption>{{/if}}
</figure> </figure>

View file

@ -9,7 +9,7 @@
{{#if isImage}}<img src="{{thumbnail}}" srcset="{{thumbnail}} 1x, {{thumbnail2x}} 2x" class="pi-image-thumbnail" alt="{{alt}}" width="{{{width}}}" height="{{{height}}}"/> {{#if isImage}}<img src="{{thumbnail}}" srcset="{{thumbnail}} 1x, {{thumbnail2x}} 2x" class="pi-image-thumbnail" alt="{{alt}}" width="{{{width}}}" height="{{{height}}}"/>
{{else}}{{#if isVideo}}<video src="{{url}}" class="pi-video-player" controls="true" controlsList="nodownload" preload="metadata">{{alt}}</video> {{else}}{{#if isVideo}}<video src="{{url}}" class="pi-video-player" controls="true" controlsList="nodownload" preload="metadata">{{alt}}</video>
{{else}}{{#if isAudio}}<audio src="{{url}}" class="pi-audio-player" controls="true" controlsList="nodownload">{{alt}}</audio> {{else}}{{#if isAudio}}<audio src="{{url}}" class="pi-audio-player" controls="true" controlsList="nodownload">{{alt}}</audio>
{{else}}{{alt}}{{/if}}{{/if}}{{/if}} {{else}}{{alt}}{{/if}}{{/if}}{{/if}}{{{htmlAfter}}}
</a> </a>
</figure> </figure>
</div> </div>

View file

@ -185,7 +185,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase {
'isVideo' => false, 'isVideo' => false,
'isAudio' => false, 'isAudio' => false,
'source' => 'img', 'source' => 'img',
'item-name' => null 'item-name' => null,
'htmlAfter' => null,
] ] ] ]
], ],
[ [
@ -201,7 +202,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase {
'isVideo' => false, 'isVideo' => false,
'isAudio' => false, 'isAudio' => false,
'source' => 'img', 'source' => 'img',
'item-name' => null 'item-name' => null,
'htmlAfter' => null,
] ] ] ]
], ],
[ [
@ -217,7 +219,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase {
'isVideo' => false, 'isVideo' => false,
'isAudio' => false, 'isAudio' => false,
'source' => 'img', 'source' => 'img',
'item-name' => null 'item-name' => null,
'htmlAfter' => null,
] ] ] ]
], ],
[ [
@ -233,7 +236,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase {
'isVideo' => false, 'isVideo' => false,
'isAudio' => false, 'isAudio' => false,
'source' => 'img', 'source' => 'img',
'item-name' => null 'item-name' => null,
'htmlAfter' => null,
] ] ] ]
], ],
[ [
@ -249,7 +253,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase {
'isVideo' => false, 'isVideo' => false,
'isAudio' => false, 'isAudio' => false,
'source' => 'img', 'source' => 'img',
'item-name' => 'img' 'item-name' => 'img',
'htmlAfter' => null,
] ] ] ]
], ],
[ [
@ -265,7 +270,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase {
'isVideo' => true, 'isVideo' => true,
'isAudio' => false, 'isAudio' => false,
'source' => 'media', 'source' => 'media',
'item-name' => null 'item-name' => null,
'htmlAfter' => null,
] ] ] ]
], ],
[ [
@ -287,7 +293,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase {
'isVideo' => false, 'isVideo' => false,
'isAudio' => true, 'isAudio' => true,
'source' => 'media', 'source' => 'media',
'item-name' => null 'item-name' => null,
'htmlAfter' => null,
] ] ] ]
], ],
[ [