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, ] ] ], [