MediaWiki  master
ThumbnailImage.php
Go to the documentation of this file.
1 <?php
2 
27 
46  public function __construct( $file, $url, $path = false, $parameters = [] ) {
47  # Previous parameters:
48  # $file, $url, $width, $height, $path = false, $page = false
49 
50  $defaults = [
51  'page' => false,
52  'lang' => false
53  ];
54 
55  if ( is_array( $parameters ) ) {
56  $actualParams = $parameters + $defaults;
57  } else {
58  # Using old format, should convert. Later a warning could be added here.
59  $numArgs = func_num_args();
60  $actualParams = [
61  'width' => $path,
62  'height' => $parameters,
63  'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
64  ] + $defaults;
65  $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
66  }
67 
68  $this->file = $file;
69  $this->url = $url;
70  $this->path = $path;
71 
72  # These should be integers when they get here.
73  # If not, there's a bug somewhere. But let's at
74  # least produce valid HTML code regardless.
75  // @phan-suppress-next-line PhanTypeMismatchArgumentInternal Confused by old signature
76  $this->width = (int)round( $actualParams['width'] );
77  $this->height = (int)round( $actualParams['height'] );
78 
79  $this->page = $actualParams['page'];
80  $this->lang = $actualParams['lang'];
81  }
82 
116  public function toHtml( $options = [] ) {
117  $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
118  $nativeImageLazyLoading = $mainConfig->get( MainConfigNames::NativeImageLazyLoading );
119  $enableLegacyMediaDOM = $mainConfig->get( MainConfigNames::ParserEnableLegacyMediaDOM );
120 
121  if ( func_num_args() == 2 ) {
122  throw new MWException( __METHOD__ . ' called in the old style' );
123  }
124 
125  $alt = $options['alt'] ?? '';
126  $query = $options['desc-query'] ?? '';
127 
128  $attribs = [
129  'alt' => $alt,
130  'src' => $this->url,
131  'decoding' => 'async',
132  ];
133 
134  if ( $options['loading'] ?? $nativeImageLazyLoading ) {
135  $attribs['loading'] = $options['loading'] ?? 'lazy';
136  }
137 
138  if ( !empty( $options['custom-url-link'] ) ) {
139  $linkAttribs = [ 'href' => $options['custom-url-link'] ];
140  if ( !empty( $options['title'] ) ) {
141  $linkAttribs['title'] = $options['title'];
142  }
143  if ( !empty( $options['custom-target-link'] ) ) {
144  $linkAttribs['target'] = $options['custom-target-link'];
145  } elseif ( !empty( $options['parser-extlink-target'] ) ) {
146  $linkAttribs['target'] = $options['parser-extlink-target'];
147  }
148  if ( !empty( $options['parser-extlink-rel'] ) ) {
149  $linkAttribs['rel'] = $options['parser-extlink-rel'];
150  }
151  } elseif ( !empty( $options['custom-title-link'] ) ) {
153  $title = $options['custom-title-link'];
154  $linkAttribs = [
155  'href' => $title->getLinkURL( $options['custom-title-link-query'] ?? null ),
156  'title' => empty( $options['title'] ) ? $title->getPrefixedText() : $options['title']
157  ];
158  } elseif ( !empty( $options['desc-link'] ) ) {
159  $linkAttribs = $this->getDescLinkAttribs(
160  empty( $options['title'] ) ? null : $options['title'],
161  $query
162  );
163  } elseif ( !empty( $options['file-link'] ) ) {
164  $linkAttribs = [ 'href' => $this->file->getUrl() ];
165  } else {
166  $linkAttribs = false;
167  if ( !empty( $options['title'] ) ) {
168  if ( $enableLegacyMediaDOM ) {
169  $attribs['title'] = $options['title'];
170  } else {
171  $linkAttribs = [ 'title' => $options['title'] ];
172  }
173  }
174  }
175 
176  if ( empty( $options['no-dimensions'] ) ) {
177  $attribs['width'] = $this->width;
178  $attribs['height'] = $this->height;
179  }
180  if ( !empty( $options['valign'] ) ) {
181  $attribs['style'] = "vertical-align: {$options['valign']}";
182  }
183  if ( !empty( $options['img-class'] ) ) {
184  $attribs['class'] = $options['img-class'];
185  }
186  if ( isset( $options['override-height'] ) ) {
187  $attribs['height'] = $options['override-height'];
188  }
189  if ( isset( $options['override-width'] ) ) {
190  $attribs['width'] = $options['override-width'];
191  }
192 
193  // Additional densities for responsive images, if specified.
194  // If any of these urls is the same as src url, it'll be excluded.
195  $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] );
196  if ( !empty( $responsiveUrls ) ) {
197  $attribs['srcset'] = Html::srcSet( $responsiveUrls );
198  }
199 
200  Hooks::runner()->onThumbnailBeforeProduceHTML( $this, $attribs, $linkAttribs );
201 
202  return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
203  }
204 }
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
static srcSet(array $urls)
Generate a srcset attribute value.
Definition: Html.php:1085
MediaWiki exception.
Definition: MWException.php:29
Base class for the output of MediaHandler::doTransform() and File::transform().
string null false $path
Filesystem path to the thumb.
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=[])
A class containing constants representing the names of configuration variables.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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 element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:43