From eb55d89e77ac683492a7eec1872058e17e65771e Mon Sep 17 00:00:00 2001 From: alex4401 <33527044+alex4401@users.noreply.github.com> Date: Mon, 24 Jul 2023 19:13:33 +0200 Subject: [PATCH] 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. --- includes/services/Parser/ExternalParser.php | 2 +- .../Parser/MediaWikiParserService.php | 10 +++++++-- includes/services/Parser/Nodes/NodeMedia.php | 12 ++++++----- includes/services/Parser/SimpleParser.php | 3 ++- templates/PortableInfoboxItemMedia.hbs | 2 +- .../PortableInfoboxItemMediaCollection.hbs | 2 +- tests/phpunit/nodes/NodeMediaTest.php | 21 ++++++++++++------- 7 files changed, 34 insertions(+), 18 deletions(-) diff --git a/includes/services/Parser/ExternalParser.php b/includes/services/Parser/ExternalParser.php index ae25935..63eb149 100644 --- a/includes/services/Parser/ExternalParser.php +++ b/includes/services/Parser/ExternalParser.php @@ -7,5 +7,5 @@ interface ExternalParser { public function replaceVariables( $text ); - public function addImage( $title ); + public function addImage( $title ): ?string; } diff --git a/includes/services/Parser/MediaWikiParserService.php b/includes/services/Parser/MediaWikiParserService.php index c9e95d0..0e2e328 100644 --- a/includes/services/Parser/MediaWikiParserService.php +++ b/includes/services/Parser/MediaWikiParserService.php @@ -83,8 +83,9 @@ class MediaWikiParserService implements ExternalParser { * Add image to parser output for later usage * * @param Title $title + * @return ?string PageImages markers, if any. */ - public function addImage( $title ) { + public function addImage( $title ): ?string { $services = MediaWikiServices::getInstance(); $repoGroup = $services->getRepoGroup(); @@ -94,7 +95,8 @@ class MediaWikiParserService implements ExternalParser { $sha1 = $file ? $file->getSha1() : null; $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( ParserFileProcessingHookHandlers::class, 'onParserModifyImageHTML' ) ) { @@ -113,6 +115,10 @@ class MediaWikiParserService implements ExternalParser { $handler->onParserModifyImageHTML( $this->parser, $file, $params, $html ); + + return $html; } + + return null; } } diff --git a/includes/services/Parser/Nodes/NodeMedia.php b/includes/services/Parser/Nodes/NodeMedia.php index e347913..196484c 100644 --- a/includes/services/Parser/Nodes/NodeMedia.php +++ b/includes/services/Parser/Nodes/NodeMedia.php @@ -150,10 +150,6 @@ class NodeMedia extends Node { return []; } - if ( $titleObj instanceof Title ) { - $this->getExternalParser()->addImage( $titleObj ); - } - $mediatype = $fileObj->getMediaType(); $image = [ 'url' => $this->resolveImageUrl( $fileObj ), @@ -164,9 +160,15 @@ class NodeMedia extends Node { 'isVideo' => $mediatype === MEDIATYPE_VIDEO, 'isAudio' => $mediatype === MEDIATYPE_AUDIO, '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'] ) { $image = array_merge( $image, $helper->extendImageData( $fileObj, diff --git a/includes/services/Parser/SimpleParser.php b/includes/services/Parser/SimpleParser.php index 3a8bbf7..3d7c427 100644 --- a/includes/services/Parser/SimpleParser.php +++ b/includes/services/Parser/SimpleParser.php @@ -11,7 +11,8 @@ class SimpleParser implements ExternalParser { return $text; } - public function addImage( $title ) { + public function addImage( $title ): ?string { // do nothing + return null; } } diff --git a/templates/PortableInfoboxItemMedia.hbs b/templates/PortableInfoboxItemMedia.hbs index aa08cde..461a0b8 100644 --- a/templates/PortableInfoboxItemMedia.hbs +++ b/templates/PortableInfoboxItemMedia.hbs @@ -3,7 +3,7 @@ {{#if isImage}}{{alt}} {{else}}{{#if isVideo}} {{else}}{{#if isAudio}} - {{else}}{{alt}}{{/if}}{{/if}}{{/if}} + {{else}}{{alt}}{{/if}}{{/if}}{{/if}}{{{htmlAfter}}} {{#if caption}}
{{{caption}}}
{{/if}} \ No newline at end of file diff --git a/templates/PortableInfoboxItemMediaCollection.hbs b/templates/PortableInfoboxItemMediaCollection.hbs index e638c57..9cefc21 100644 --- a/templates/PortableInfoboxItemMediaCollection.hbs +++ b/templates/PortableInfoboxItemMediaCollection.hbs @@ -9,7 +9,7 @@ {{#if isImage}}{{alt}} {{else}}{{#if isVideo}} {{else}}{{#if isAudio}} - {{else}}{{alt}}{{/if}}{{/if}}{{/if}} + {{else}}{{alt}}{{/if}}{{/if}}{{/if}}{{{htmlAfter}}} diff --git a/tests/phpunit/nodes/NodeMediaTest.php b/tests/phpunit/nodes/NodeMediaTest.php index 731dc6a..c9917a9 100644 --- a/tests/phpunit/nodes/NodeMediaTest.php +++ b/tests/phpunit/nodes/NodeMediaTest.php @@ -185,7 +185,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase { 'isVideo' => false, 'isAudio' => false, 'source' => 'img', - 'item-name' => null + 'item-name' => null, + 'htmlAfter' => null, ] ] ], [ @@ -201,7 +202,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase { 'isVideo' => false, 'isAudio' => false, 'source' => 'img', - 'item-name' => null + 'item-name' => null, + 'htmlAfter' => null, ] ] ], [ @@ -217,7 +219,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase { 'isVideo' => false, 'isAudio' => false, 'source' => 'img', - 'item-name' => null + 'item-name' => null, + 'htmlAfter' => null, ] ] ], [ @@ -233,7 +236,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase { 'isVideo' => false, 'isAudio' => false, 'source' => 'img', - 'item-name' => null + 'item-name' => null, + 'htmlAfter' => null, ] ] ], [ @@ -249,7 +253,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase { 'isVideo' => false, 'isAudio' => false, 'source' => 'img', - 'item-name' => 'img' + 'item-name' => 'img', + 'htmlAfter' => null, ] ] ], [ @@ -265,7 +270,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase { 'isVideo' => true, 'isAudio' => false, 'source' => 'media', - 'item-name' => null + 'item-name' => null, + 'htmlAfter' => null, ] ] ], [ @@ -287,7 +293,8 @@ class NodeMediaTest extends MediaWikiIntegrationTestCase { 'isVideo' => false, 'isAudio' => true, 'source' => 'media', - 'item-name' => null + 'item-name' => null, + 'htmlAfter' => null, ] ] ], [