2019-03-30 11:16:51 +00:00
|
|
|
<?php
|
2022-01-06 18:44:56 +00:00
|
|
|
|
2019-03-30 11:16:51 +00:00
|
|
|
/**
|
|
|
|
|
* Base class for the output of file transformation methods.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
|
|
|
|
|
2023-05-06 20:01:10 +00:00
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2023-02-16 19:27:21 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2022-01-06 18:44:56 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2024-05-16 10:52:03 +00:00
|
|
|
use MediaWiki\Xml\Xml;
|
2022-01-06 18:44:56 +00:00
|
|
|
|
2019-03-30 11:16:51 +00:00
|
|
|
/**
|
|
|
|
|
* Media transform output for images
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
|
|
|
|
class ThumbnailImage extends MediaTransformOutput {
|
|
|
|
|
/**
|
|
|
|
|
* Get a thumbnail object from a file and parameters.
|
|
|
|
|
* If $path is set to null, the output file is treated as a source copy.
|
|
|
|
|
* If $path is set to false, no output file will be created.
|
|
|
|
|
* $parameters should include, as a minimum, (file) 'width' and 'height'.
|
|
|
|
|
* It may also include a 'page' parameter for multipage files.
|
|
|
|
|
*
|
|
|
|
|
* @param File $file
|
|
|
|
|
* @param string $url URL path to the thumb
|
2022-03-08 22:57:00 +00:00
|
|
|
* @param string|null|false $path Filesystem path to the thumb
|
2019-03-30 11:16:51 +00:00
|
|
|
* @param array $parameters Associative array of parameters
|
|
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function __construct( $file, $url, $path = false, $parameters = [] ) {
|
2023-03-08 20:44:20 +00:00
|
|
|
// Previous parameters:
|
|
|
|
|
// $file, $url, $width, $height, $path = false, $page = false
|
2019-03-30 11:16:51 +00:00
|
|
|
|
|
|
|
|
$defaults = [
|
|
|
|
|
'page' => false,
|
|
|
|
|
'lang' => false
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ( is_array( $parameters ) ) {
|
|
|
|
|
$actualParams = $parameters + $defaults;
|
|
|
|
|
} else {
|
2023-03-08 20:44:20 +00:00
|
|
|
// Using old format, should convert. Later a warning could be added here.
|
2019-03-30 11:16:51 +00:00
|
|
|
$numArgs = func_num_args();
|
|
|
|
|
$actualParams = [
|
|
|
|
|
'width' => $path,
|
|
|
|
|
'height' => $parameters,
|
|
|
|
|
'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
|
|
|
|
|
] + $defaults;
|
|
|
|
|
$path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->file = $file;
|
|
|
|
|
$this->url = $url;
|
|
|
|
|
$this->path = $path;
|
|
|
|
|
|
2023-03-08 20:44:20 +00:00
|
|
|
// These should be integers when they get here.
|
|
|
|
|
// If not, there's a bug somewhere. But let's at
|
|
|
|
|
// least produce valid HTML code regardless.
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal Confused by old signature
|
2022-02-26 16:28:48 +00:00
|
|
|
$this->width = (int)round( $actualParams['width'] );
|
|
|
|
|
$this->height = (int)round( $actualParams['height'] );
|
2019-03-30 11:16:51 +00:00
|
|
|
|
|
|
|
|
$this->page = $actualParams['page'];
|
|
|
|
|
$this->lang = $actualParams['lang'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return HTML <img ... /> tag for the thumbnail, will include
|
|
|
|
|
* width and height attributes and a blank alt text (as required).
|
|
|
|
|
*
|
|
|
|
|
* @param array $options Associative array of options. Boolean options
|
|
|
|
|
* should be indicated with a value of true for true, and false or
|
|
|
|
|
* absent for false.
|
|
|
|
|
*
|
|
|
|
|
* alt HTML alt attribute
|
|
|
|
|
* title HTML title attribute
|
|
|
|
|
* desc-link Boolean, show a description link
|
|
|
|
|
* file-link Boolean, show a file download link
|
|
|
|
|
* valign vertical-align property, if the output is an inline element
|
|
|
|
|
* img-class Class applied to the \<img\> tag, if there is such a tag
|
2023-01-25 13:11:10 +00:00
|
|
|
* loading Specify an explicit browser loading strategy for images and iframes.
|
2019-03-30 11:16:51 +00:00
|
|
|
* desc-query String, description link query params
|
|
|
|
|
* override-width Override width attribute. Should generally not set
|
|
|
|
|
* override-height Override height attribute. Should generally not set
|
|
|
|
|
* no-dimensions Boolean, skip width and height attributes (useful if
|
|
|
|
|
* set in CSS)
|
|
|
|
|
* custom-url-link Custom URL to link to
|
|
|
|
|
* custom-title-link Custom Title object to link to
|
2022-02-18 20:17:18 +00:00
|
|
|
* custom-title-link-query Querystring parameters array, for custom-title-link
|
2022-02-17 22:38:17 +00:00
|
|
|
* custom-target-link Value of the target attribute, for custom-url-link
|
2019-03-30 11:16:51 +00:00
|
|
|
* parser-extlink-* Attributes added by parser for external links:
|
|
|
|
|
* parser-extlink-rel: add rel="nofollow"
|
|
|
|
|
* parser-extlink-target: link target, but overridden by custom-target-link
|
Add magnify links on thumbs when not linking to file desc page
This is all in the context of !$enableLegacyMediaDOM where explicit
magnify links aren't added to the html.
If we're linking to the file description page, magnify links are already
added via css. However, if we're not, we're missing both the url for
the media and something to click on, which can't be added with css.
Here we add the necessary url as a resource attribute on the
mw-file-element. Parsoid already does this unconditionally so there's
precedent for it. And, in the common case, there isn't any redundancy
since it will be omitted where mw-file-description links are present.
A script is provided to add the magnify links where needed. These
aren't added directly to the html since they are considered purely
presentational. Though they're styled the same as mw-file-description
links, they are given a different class, mw-file-magnify, to avoid any
confusion about their purpose and the expectation that the media element
would be found inside them.
As above, since the legacy output always includes the magnify links in
the html, it's a significant reduction in size to only be including the
resource attribute where necessary and adding the links via css.
Bug: T329413
Depends-On: Ia22eeef770980fd555fdeb83049fc0ff700be884
Change-Id: Id46d1b2ab1af3baebff13e10f1485f3cfd9a4b37
2023-05-12 02:16:25 +00:00
|
|
|
* magnify-resource To set the HTML resource attribute, when necessary
|
2019-03-30 11:16:51 +00:00
|
|
|
*
|
|
|
|
|
* For images, desc-link and file-link are implemented as a click-through. For
|
|
|
|
|
* sounds and videos, they may be displayed in other ways.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function toHtml( $options = [] ) {
|
2023-05-06 20:01:10 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$mainConfig = $services->getMainConfig();
|
2022-04-26 15:48:03 +00:00
|
|
|
$nativeImageLazyLoading = $mainConfig->get( MainConfigNames::NativeImageLazyLoading );
|
2022-05-09 20:35:04 +00:00
|
|
|
$enableLegacyMediaDOM = $mainConfig->get( MainConfigNames::ParserEnableLegacyMediaDOM );
|
2019-03-30 11:16:51 +00:00
|
|
|
|
2023-03-08 20:44:20 +00:00
|
|
|
if ( func_num_args() === 2 ) {
|
2024-01-20 20:07:28 +00:00
|
|
|
throw new InvalidArgumentException( __METHOD__ . ' called in the old style' );
|
2019-03-30 11:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = $options['desc-query'] ?? '';
|
|
|
|
|
|
2023-02-08 20:08:42 +00:00
|
|
|
$attribs = [];
|
|
|
|
|
|
|
|
|
|
// An empty alt indicates an image is not a key part of the content and
|
|
|
|
|
// that non-visual browsers may omit it from rendering. Only set the
|
|
|
|
|
// parameter if it's explicitly requested.
|
|
|
|
|
if ( isset( $options['alt'] ) ) {
|
|
|
|
|
$attribs['alt'] = $options['alt'];
|
|
|
|
|
}
|
|
|
|
|
|
Add magnify links on thumbs when not linking to file desc page
This is all in the context of !$enableLegacyMediaDOM where explicit
magnify links aren't added to the html.
If we're linking to the file description page, magnify links are already
added via css. However, if we're not, we're missing both the url for
the media and something to click on, which can't be added with css.
Here we add the necessary url as a resource attribute on the
mw-file-element. Parsoid already does this unconditionally so there's
precedent for it. And, in the common case, there isn't any redundancy
since it will be omitted where mw-file-description links are present.
A script is provided to add the magnify links where needed. These
aren't added directly to the html since they are considered purely
presentational. Though they're styled the same as mw-file-description
links, they are given a different class, mw-file-magnify, to avoid any
confusion about their purpose and the expectation that the media element
would be found inside them.
As above, since the legacy output always includes the magnify links in
the html, it's a significant reduction in size to only be including the
resource attribute where necessary and adding the links via css.
Bug: T329413
Depends-On: Ia22eeef770980fd555fdeb83049fc0ff700be884
Change-Id: Id46d1b2ab1af3baebff13e10f1485f3cfd9a4b37
2023-05-12 02:16:25 +00:00
|
|
|
// Description links get the mw-file-description class and link
|
|
|
|
|
// to the file description page, making the resource redundant
|
|
|
|
|
if (
|
|
|
|
|
!$enableLegacyMediaDOM &&
|
|
|
|
|
isset( $options['magnify-resource'] ) &&
|
|
|
|
|
!( $options['desc-link'] ?? false )
|
|
|
|
|
) {
|
|
|
|
|
$attribs['resource'] = $options['magnify-resource'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 20:08:42 +00:00
|
|
|
$attribs += [
|
2019-03-30 11:16:51 +00:00
|
|
|
'src' => $this->url,
|
|
|
|
|
'decoding' => 'async',
|
|
|
|
|
];
|
|
|
|
|
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( $options['loading'] ?? $nativeImageLazyLoading ) {
|
2020-05-21 17:30:58 +00:00
|
|
|
$attribs['loading'] = $options['loading'] ?? 'lazy';
|
2019-08-21 09:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-30 11:16:51 +00:00
|
|
|
if ( !empty( $options['custom-url-link'] ) ) {
|
|
|
|
|
$linkAttribs = [ 'href' => $options['custom-url-link'] ];
|
|
|
|
|
if ( !empty( $options['title'] ) ) {
|
|
|
|
|
$linkAttribs['title'] = $options['title'];
|
|
|
|
|
}
|
|
|
|
|
if ( !empty( $options['custom-target-link'] ) ) {
|
|
|
|
|
$linkAttribs['target'] = $options['custom-target-link'];
|
|
|
|
|
} elseif ( !empty( $options['parser-extlink-target'] ) ) {
|
|
|
|
|
$linkAttribs['target'] = $options['parser-extlink-target'];
|
|
|
|
|
}
|
|
|
|
|
if ( !empty( $options['parser-extlink-rel'] ) ) {
|
|
|
|
|
$linkAttribs['rel'] = $options['parser-extlink-rel'];
|
|
|
|
|
}
|
|
|
|
|
} elseif ( !empty( $options['custom-title-link'] ) ) {
|
|
|
|
|
/** @var Title $title */
|
|
|
|
|
$title = $options['custom-title-link'];
|
|
|
|
|
$linkAttribs = [
|
2022-02-18 20:17:18 +00:00
|
|
|
'href' => $title->getLinkURL( $options['custom-title-link-query'] ?? null ),
|
2022-05-09 22:15:28 +00:00
|
|
|
'title' => empty( $options['title'] ) ? $title->getPrefixedText() : $options['title']
|
2019-03-30 11:16:51 +00:00
|
|
|
];
|
|
|
|
|
} elseif ( !empty( $options['desc-link'] ) ) {
|
2022-02-18 23:29:59 +00:00
|
|
|
$linkAttribs = $this->getDescLinkAttribs(
|
|
|
|
|
empty( $options['title'] ) ? null : $options['title'],
|
|
|
|
|
$query
|
|
|
|
|
);
|
2019-03-30 11:16:51 +00:00
|
|
|
} elseif ( !empty( $options['file-link'] ) ) {
|
|
|
|
|
$linkAttribs = [ 'href' => $this->file->getUrl() ];
|
|
|
|
|
} else {
|
|
|
|
|
$linkAttribs = false;
|
|
|
|
|
if ( !empty( $options['title'] ) ) {
|
2022-05-09 20:35:04 +00:00
|
|
|
if ( $enableLegacyMediaDOM ) {
|
|
|
|
|
$attribs['title'] = $options['title'];
|
|
|
|
|
} else {
|
|
|
|
|
$linkAttribs = [ 'title' => $options['title'] ];
|
|
|
|
|
}
|
2019-03-30 11:16:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( empty( $options['no-dimensions'] ) ) {
|
|
|
|
|
$attribs['width'] = $this->width;
|
|
|
|
|
$attribs['height'] = $this->height;
|
|
|
|
|
}
|
|
|
|
|
if ( !empty( $options['valign'] ) ) {
|
|
|
|
|
$attribs['style'] = "vertical-align: {$options['valign']}";
|
|
|
|
|
}
|
|
|
|
|
if ( !empty( $options['img-class'] ) ) {
|
|
|
|
|
$attribs['class'] = $options['img-class'];
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $options['override-height'] ) ) {
|
|
|
|
|
$attribs['height'] = $options['override-height'];
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $options['override-width'] ) ) {
|
|
|
|
|
$attribs['width'] = $options['override-width'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Additional densities for responsive images, if specified.
|
|
|
|
|
// If any of these urls is the same as src url, it'll be excluded.
|
|
|
|
|
$responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] );
|
2023-09-08 21:28:11 +00:00
|
|
|
if ( $responsiveUrls ) {
|
2019-03-30 11:16:51 +00:00
|
|
|
$attribs['srcset'] = Html::srcSet( $responsiveUrls );
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 20:01:10 +00:00
|
|
|
( new HookRunner( $services->getHookContainer() ) )
|
|
|
|
|
->onThumbnailBeforeProduceHTML( $this, $attribs, $linkAttribs );
|
2019-03-30 11:16:51 +00:00
|
|
|
|
|
|
|
|
return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
|
|
|
|
|
}
|
|
|
|
|
}
|