MediaWiki master
ThumbnailImage.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Media;
11
12use InvalidArgumentException;
19
38 public function __construct( $file, $url, $path = false, $parameters = [] ) {
39 // Previous parameters:
40 // $file, $url, $width, $height, $path = false, $page = false
41
42 $defaults = [
43 'page' => false,
44 'lang' => false
45 ];
46
47 if ( is_array( $parameters ) ) {
48 $actualParams = $parameters + $defaults;
49 } else {
50 // Using old format, should convert. Later a warning could be added here.
51 $numArgs = func_num_args();
52 $actualParams = [
53 'width' => $path,
54 'height' => $parameters,
55 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
56 ] + $defaults;
57 $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
58 }
59
60 $this->file = $file;
61 $this->url = $url;
62 $this->path = $path;
63
64 // These should be integers when they get here.
65 // If not, there's a bug somewhere. But let's at
66 // least produce valid HTML code regardless.
67 // @phan-suppress-next-line PhanTypeMismatchArgumentInternal Confused by old signature
68 $this->width = (int)round( $actualParams['width'] );
69 $this->height = (int)round( $actualParams['height'] );
70
71 $this->page = $actualParams['page'];
72 $this->lang = $actualParams['lang'];
73 }
74
109 public function toHtml( $options = [] ) {
110 $services = MediaWikiServices::getInstance();
111 $mainConfig = $services->getMainConfig();
112 $nativeImageLazyLoading = $mainConfig->get( MainConfigNames::NativeImageLazyLoading );
113
114 if ( func_num_args() === 2 ) {
115 throw new InvalidArgumentException( __METHOD__ . ' called in the old style' );
116 }
117
118 $query = $options['desc-query'] ?? '';
119
120 $attribs = [];
121
122 // An empty alt indicates an image is not a key part of the content and
123 // that non-visual browsers may omit it from rendering. Only set the
124 // parameter if it's explicitly requested.
125 if ( isset( $options['alt'] ) ) {
126 $attribs['alt'] = $options['alt'];
127 }
128
129 // Description links get the mw-file-description class and link
130 // to the file description page, making the resource redundant
131 if (
132 isset( $options['magnify-resource'] ) &&
133 !( $options['desc-link'] ?? false )
134 ) {
135 $attribs['resource'] = $options['magnify-resource'];
136 }
137
138 $attribs += [
139 'src' => $this->getUrl(),
140 'decoding' => 'async',
141 ];
142
143 if ( $options['loading'] ?? $nativeImageLazyLoading ) {
144 $attribs['loading'] = $options['loading'] ?? 'lazy';
145 }
146
147 if ( !empty( $options['custom-url-link'] ) ) {
148 $linkAttribs = [ 'href' => $options['custom-url-link'] ];
149 if ( !empty( $options['title'] ) ) {
150 $linkAttribs['title'] = $options['title'];
151 }
152 if ( !empty( $options['custom-target-link'] ) ) {
153 $linkAttribs['target'] = $options['custom-target-link'];
154 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
155 $linkAttribs['target'] = $options['parser-extlink-target'];
156 }
157 if ( !empty( $options['parser-extlink-rel'] ) ) {
158 $linkAttribs['rel'] = $options['parser-extlink-rel'];
159 }
160 } elseif ( !empty( $options['custom-title-link'] ) ) {
162 $title = $options['custom-title-link'];
163 $linkAttribs = [
164 'href' => $title->getLinkURL( $options['custom-title-link-query'] ?? null ),
165 'title' => empty( $options['title'] ) ? $title->getPrefixedText() : $options['title']
166 ];
167 } elseif ( !empty( $options['desc-link'] ) ) {
168 $linkAttribs = $this->getDescLinkAttribs(
169 empty( $options['title'] ) ? null : $options['title'],
170 $query
171 );
172 } elseif ( !empty( $options['file-link'] ) ) {
173 $linkAttribs = [ 'href' => $this->file->getUrl() ];
174 } else {
175 $linkAttribs = false;
176 if ( !empty( $options['title'] ) ) {
177 $linkAttribs = [ 'title' => $options['title'] ];
178 }
179 }
180
181 if ( empty( $options['no-dimensions'] ) ) {
182 $attribs['width'] = $this->width;
183 $attribs['height'] = $this->height;
184 }
185 if ( !empty( $options['valign'] ) ) {
186 $attribs['style'] = "vertical-align: {$options['valign']}";
187 }
188 if ( !empty( $options['img-class'] ) ) {
189 $attribs['class'] = $options['img-class'];
190 }
191 if ( isset( $options['override-height'] ) ) {
192 $attribs['height'] = $options['override-height'];
193 }
194 if ( isset( $options['override-width'] ) ) {
195 $attribs['width'] = $options['override-width'];
196 }
197
198 // Additional densities for responsive images, if specified.
199 // If any of these urls is the same as src url, it'll be excluded.
200 $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->getUrl() ] );
201 if ( $responsiveUrls ) {
202 $attribs['srcset'] = Html::srcSet( $responsiveUrls );
203 }
204
205 ( new HookRunner( $services->getHookContainer() ) )
206 ->onThumbnailBeforeProduceHTML( $this, $attribs, $linkAttribs );
207
208 return $this->linkWrap( $linkAttribs, Html::element( 'img', $attribs ) );
209 }
210}
211
213class_alias( ThumbnailImage::class, 'ThumbnailImage' );
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
A class containing constants representing the names of configuration variables.
const NativeImageLazyLoading
Name constant for the NativeImageLazyLoading setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Base class for the output of MediaHandler::doTransform() and File::transform().
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes or, fallback to a span in the absence...
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1....
string null false $path
Filesystem path to the thumb.
string false $url
URL path to the thumb.
Media transform output for images.
toHtml( $options=[])
Return HTML <img ... /> tag for the thumbnail, will include width and height attributes and a blank a...
__construct( $file, $url, $path=false, $parameters=[])
Get a thumbnail object from a file and parameters.
Represents a title within MediaWiki.
Definition Title.php:69
element(SerializerNode $parent, SerializerNode $node, $contents)