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
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-06 18:44:56 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2019-03-30 11:16:51 +00:00
|
|
|
/**
|
|
|
|
|
* Media transform output for images
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
|
|
|
|
class ThumbnailImage extends MediaTransformOutput {
|
|
|
|
|
private static $firstNonIconImageRendered = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
* @param string|bool $path Filesystem path to the thumb
|
|
|
|
|
* @param array $parameters Associative array of parameters
|
|
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function __construct( $file, $url, $path = false, $parameters = [] ) {
|
2019-03-30 11:16:51 +00:00
|
|
|
# Previous parameters:
|
|
|
|
|
# $file, $url, $width, $height, $path = false, $page = false
|
|
|
|
|
|
|
|
|
|
$defaults = [
|
|
|
|
|
'page' => false,
|
|
|
|
|
'lang' => false
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ( is_array( $parameters ) ) {
|
|
|
|
|
$actualParams = $parameters + $defaults;
|
|
|
|
|
} else {
|
|
|
|
|
# Using old format, should convert. Later a warning could be added here.
|
|
|
|
|
$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;
|
|
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
$this->width = round( $actualParams['width'] );
|
|
|
|
|
$this->height = round( $actualParams['height'] );
|
|
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
* 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-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
|
2021-12-15 01:03:58 +00:00
|
|
|
* resource Override the resource derived from the description link
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function toHtml( $options = [] ) {
|
2022-01-06 18:44:56 +00:00
|
|
|
$mainConfig = MediaWikiServices::getInstance()->getMainConfig();
|
|
|
|
|
$priorityHints = $mainConfig->get( 'PriorityHints' );
|
|
|
|
|
$priorityHintsRatio = $mainConfig->get( 'PriorityHintsRatio' );
|
|
|
|
|
$elementTiming = $mainConfig->get( 'ElementTiming' );
|
|
|
|
|
$nativeImageLazyLoading = $mainConfig->get( 'NativeImageLazyLoading' );
|
|
|
|
|
$parserEnableLegacyMediaDOM = $mainConfig->get( 'ParserEnableLegacyMediaDOM' );
|
2019-03-30 11:16:51 +00:00
|
|
|
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
if ( func_num_args() == 2 ) {
|
2019-03-30 11:16:51 +00:00
|
|
|
throw new MWException( __METHOD__ . ' called in the old style' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$alt = $options['alt'] ?? '';
|
|
|
|
|
$query = $options['desc-query'] ?? '';
|
2021-12-15 01:03:58 +00:00
|
|
|
$descLinkAttribs = $this->getDescLinkAttribs(
|
|
|
|
|
empty( $options['title'] ) ? null : $options['title'],
|
|
|
|
|
$query
|
|
|
|
|
);
|
2019-03-30 11:16:51 +00:00
|
|
|
|
|
|
|
|
$attribs = [
|
2021-12-15 01:03:58 +00:00
|
|
|
'alt' => $alt
|
|
|
|
|
];
|
|
|
|
|
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( !$parserEnableLegacyMediaDOM ) {
|
2021-12-15 01:03:58 +00:00
|
|
|
$attribs['resource'] = $options['resource'] ?? $descLinkAttribs['href'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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
|
|
|
$elementTimingName = 'thumbnail';
|
|
|
|
|
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( $priorityHints
|
2019-03-30 11:16:51 +00:00
|
|
|
&& !self::$firstNonIconImageRendered
|
|
|
|
|
&& $this->width * $this->height > 100 * 100 ) {
|
|
|
|
|
self::$firstNonIconImageRendered = true;
|
|
|
|
|
|
2019-04-03 07:06:38 +00:00
|
|
|
// Generate a random number between 0.01 and 1.0, included
|
|
|
|
|
$random = rand( 1, 100 ) / 100.0;
|
|
|
|
|
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( $random <= $priorityHintsRatio ) {
|
2019-04-03 07:06:38 +00:00
|
|
|
$attribs['importance'] = 'high';
|
|
|
|
|
$elementTimingName = 'thumbnail-high';
|
|
|
|
|
} else {
|
|
|
|
|
// This lets us track that the thumbnail *would* have gotten high priority but didn't.
|
|
|
|
|
$elementTimingName = 'thumbnail-top';
|
|
|
|
|
}
|
2019-03-30 11:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( $elementTiming ) {
|
2019-03-30 11:16:51 +00:00
|
|
|
$attribs['elementtiming'] = $elementTimingName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = [
|
|
|
|
|
'href' => $title->getLinkURL(),
|
|
|
|
|
'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
|
|
|
|
|
];
|
|
|
|
|
} elseif ( !empty( $options['desc-link'] ) ) {
|
2021-12-15 01:03:58 +00:00
|
|
|
$linkAttribs = $descLinkAttribs;
|
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'] ) ) {
|
|
|
|
|
$attribs['title'] = $options['title'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ] );
|
|
|
|
|
if ( !empty( $responsiveUrls ) ) {
|
|
|
|
|
$attribs['srcset'] = Html::srcSet( $responsiveUrls );
|
|
|
|
|
}
|
|
|
|
|
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
Hooks::runner()->onThumbnailBeforeProduceHTML( $this, $attribs, $linkAttribs );
|
2019-03-30 11:16:51 +00:00
|
|
|
|
|
|
|
|
return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
|
|
|
|
|
}
|
|
|
|
|
}
|