MediaWiki master
ThumbnailImage.php
Go to the documentation of this file.
1<?php
2
32
51 public function __construct( $file, $url, $path = false, $parameters = [] ) {
52 // Previous parameters:
53 // $file, $url, $width, $height, $path = false, $page = false
54
55 $defaults = [
56 'page' => false,
57 'lang' => false
58 ];
59
60 if ( is_array( $parameters ) ) {
61 $actualParams = $parameters + $defaults;
62 } else {
63 // Using old format, should convert. Later a warning could be added here.
64 $numArgs = func_num_args();
65 $actualParams = [
66 'width' => $path,
67 'height' => $parameters,
68 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
69 ] + $defaults;
70 $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
71 }
72
73 $this->file = $file;
74 $this->url = $url;
75 $this->path = $path;
76
77 // These should be integers when they get here.
78 // If not, there's a bug somewhere. But let's at
79 // least produce valid HTML code regardless.
80 // @phan-suppress-next-line PhanTypeMismatchArgumentInternal Confused by old signature
81 $this->width = (int)round( $actualParams['width'] );
82 $this->height = (int)round( $actualParams['height'] );
83
84 $this->page = $actualParams['page'];
85 $this->lang = $actualParams['lang'];
86 }
87
122 public function toHtml( $options = [] ) {
123 $services = MediaWikiServices::getInstance();
124 $mainConfig = $services->getMainConfig();
125 $nativeImageLazyLoading = $mainConfig->get( MainConfigNames::NativeImageLazyLoading );
126 $enableLegacyMediaDOM = $mainConfig->get( MainConfigNames::ParserEnableLegacyMediaDOM );
127
128 if ( func_num_args() === 2 ) {
129 throw new InvalidArgumentException( __METHOD__ . ' called in the old style' );
130 }
131
132 $query = $options['desc-query'] ?? '';
133
134 $attribs = [];
135
136 // An empty alt indicates an image is not a key part of the content and
137 // that non-visual browsers may omit it from rendering. Only set the
138 // parameter if it's explicitly requested.
139 if ( isset( $options['alt'] ) ) {
140 $attribs['alt'] = $options['alt'];
141 }
142
143 // Description links get the mw-file-description class and link
144 // to the file description page, making the resource redundant
145 if (
146 !$enableLegacyMediaDOM &&
147 isset( $options['magnify-resource'] ) &&
148 !( $options['desc-link'] ?? false )
149 ) {
150 $attribs['resource'] = $options['magnify-resource'];
151 }
152
153 $attribs += [
154 'src' => $this->url,
155 'decoding' => 'async',
156 ];
157
158 if ( $options['loading'] ?? $nativeImageLazyLoading ) {
159 $attribs['loading'] = $options['loading'] ?? 'lazy';
160 }
161
162 if ( !empty( $options['custom-url-link'] ) ) {
163 $linkAttribs = [ 'href' => $options['custom-url-link'] ];
164 if ( !empty( $options['title'] ) ) {
165 $linkAttribs['title'] = $options['title'];
166 }
167 if ( !empty( $options['custom-target-link'] ) ) {
168 $linkAttribs['target'] = $options['custom-target-link'];
169 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
170 $linkAttribs['target'] = $options['parser-extlink-target'];
171 }
172 if ( !empty( $options['parser-extlink-rel'] ) ) {
173 $linkAttribs['rel'] = $options['parser-extlink-rel'];
174 }
175 } elseif ( !empty( $options['custom-title-link'] ) ) {
177 $title = $options['custom-title-link'];
178 $linkAttribs = [
179 'href' => $title->getLinkURL( $options['custom-title-link-query'] ?? null ),
180 'title' => empty( $options['title'] ) ? $title->getPrefixedText() : $options['title']
181 ];
182 } elseif ( !empty( $options['desc-link'] ) ) {
183 $linkAttribs = $this->getDescLinkAttribs(
184 empty( $options['title'] ) ? null : $options['title'],
185 $query
186 );
187 } elseif ( !empty( $options['file-link'] ) ) {
188 $linkAttribs = [ 'href' => $this->file->getUrl() ];
189 } else {
190 $linkAttribs = false;
191 if ( !empty( $options['title'] ) ) {
192 if ( $enableLegacyMediaDOM ) {
193 $attribs['title'] = $options['title'];
194 } else {
195 $linkAttribs = [ 'title' => $options['title'] ];
196 }
197 }
198 }
199
200 if ( empty( $options['no-dimensions'] ) ) {
201 $attribs['width'] = $this->width;
202 $attribs['height'] = $this->height;
203 }
204 if ( !empty( $options['valign'] ) ) {
205 $attribs['style'] = "vertical-align: {$options['valign']}";
206 }
207 if ( !empty( $options['img-class'] ) ) {
208 $attribs['class'] = $options['img-class'];
209 }
210 if ( isset( $options['override-height'] ) ) {
211 $attribs['height'] = $options['override-height'];
212 }
213 if ( isset( $options['override-width'] ) ) {
214 $attribs['width'] = $options['override-width'];
215 }
216
217 // Additional densities for responsive images, if specified.
218 // If any of these urls is the same as src url, it'll be excluded.
219 $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] );
220 if ( $responsiveUrls ) {
221 $attribs['srcset'] = Html::srcSet( $responsiveUrls );
222 }
223
224 ( new HookRunner( $services->getHookContainer() ) )
225 ->onThumbnailBeforeProduceHTML( $this, $attribs, $linkAttribs );
226
227 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
228 }
229}
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....
getDescLinkAttribs( $title=null, $params=[])
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
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:57
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:78
Module of static functions for generating XML.
Definition Xml.php:37
Media transform output for images.
__construct( $file, $url, $path=false, $parameters=[])
Get a thumbnail object from a file and parameters.
toHtml( $options=[])
Return HTML.