MediaWiki REL1_33
ThumbnailImage.php
Go to the documentation of this file.
1<?php
30 private static $firstNonIconImageRendered = false;
31
44 function __construct( $file, $url, $path = false, $parameters = [] ) {
45 # Previous parameters:
46 # $file, $url, $width, $height, $path = false, $page = false
47
48 $defaults = [
49 'page' => false,
50 'lang' => false
51 ];
52
53 if ( is_array( $parameters ) ) {
54 $actualParams = $parameters + $defaults;
55 } else {
56 # Using old format, should convert. Later a warning could be added here.
57 $numArgs = func_num_args();
58 $actualParams = [
59 'width' => $path,
60 'height' => $parameters,
61 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
62 ] + $defaults;
63 $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
64 }
65
66 $this->file = $file;
67 $this->url = $url;
68 $this->path = $path;
69
70 # These should be integers when they get here.
71 # If not, there's a bug somewhere. But let's at
72 # least produce valid HTML code regardless.
73 $this->width = round( $actualParams['width'] );
74 $this->height = round( $actualParams['height'] );
75
76 $this->page = $actualParams['page'];
77 $this->lang = $actualParams['lang'];
78 }
79
112 function toHtml( $options = [] ) {
114
115 if ( count( func_get_args() ) == 2 ) {
116 throw new MWException( __METHOD__ . ' called in the old style' );
117 }
118
119 $alt = $options['alt'] ?? '';
120
121 $query = $options['desc-query'] ?? '';
122
123 $attribs = [
124 'alt' => $alt,
125 'src' => $this->url,
126 'decoding' => 'async',
127 ];
128
129 $elementTimingName = 'thumbnail';
130
132 && !self::$firstNonIconImageRendered
133 && $this->width * $this->height > 100 * 100 ) {
134 self::$firstNonIconImageRendered = true;
135
136 $attribs['importance'] = 'high';
137 $elementTimingName = 'thumbnail-high';
138 }
139
140 if ( $wgElementTiming ) {
141 $attribs['elementtiming'] = $elementTimingName;
142 }
143
144 if ( !empty( $options['custom-url-link'] ) ) {
145 $linkAttribs = [ 'href' => $options['custom-url-link'] ];
146 if ( !empty( $options['title'] ) ) {
147 $linkAttribs['title'] = $options['title'];
148 }
149 if ( !empty( $options['custom-target-link'] ) ) {
150 $linkAttribs['target'] = $options['custom-target-link'];
151 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
152 $linkAttribs['target'] = $options['parser-extlink-target'];
153 }
154 if ( !empty( $options['parser-extlink-rel'] ) ) {
155 $linkAttribs['rel'] = $options['parser-extlink-rel'];
156 }
157 } elseif ( !empty( $options['custom-title-link'] ) ) {
159 $title = $options['custom-title-link'];
160 $linkAttribs = [
161 'href' => $title->getLinkURL(),
162 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
163 ];
164 } elseif ( !empty( $options['desc-link'] ) ) {
165 $linkAttribs = $this->getDescLinkAttribs(
166 empty( $options['title'] ) ? null : $options['title'],
167 $query
168 );
169 } elseif ( !empty( $options['file-link'] ) ) {
170 $linkAttribs = [ 'href' => $this->file->getUrl() ];
171 } else {
172 $linkAttribs = false;
173 if ( !empty( $options['title'] ) ) {
174 $attribs['title'] = $options['title'];
175 }
176 }
177
178 if ( empty( $options['no-dimensions'] ) ) {
179 $attribs['width'] = $this->width;
180 $attribs['height'] = $this->height;
181 }
182 if ( !empty( $options['valign'] ) ) {
183 $attribs['style'] = "vertical-align: {$options['valign']}";
184 }
185 if ( !empty( $options['img-class'] ) ) {
186 $attribs['class'] = $options['img-class'];
187 }
188 if ( isset( $options['override-height'] ) ) {
189 $attribs['height'] = $options['override-height'];
190 }
191 if ( isset( $options['override-width'] ) ) {
192 $attribs['width'] = $options['override-width'];
193 }
194
195 // Additional densities for responsive images, if specified.
196 // If any of these urls is the same as src url, it'll be excluded.
197 $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] );
198 if ( !empty( $responsiveUrls ) ) {
199 $attribs['srcset'] = Html::srcSet( $responsiveUrls );
200 }
201
202 Hooks::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] );
203
204 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
205 }
206}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
bool $wgPriorityHints
Enable client-side Priority Hints.
bool $wgElementTiming
Enable Element Timing.
MediaWiki exception.
Base class for the output of MediaHandler::doTransform() and File::transform().
string $url
URL path to the thumb.
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes.
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1....
bool string $path
Filesystem path to the thumb.
getDescLinkAttribs( $title=null, $params=[])
Media transform output for images.
__construct( $file, $url, $path=false, $parameters=[])
Get a thumbnail object from a file and parameters.
toHtml( $options=[])
Return HTML.
static $firstNonIconImageRendered
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1999
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2012
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1617
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
width