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