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