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