MediaWiki  master
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 ( func_num_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 
131  if ( $wgPriorityHints
132  && !self::$firstNonIconImageRendered
133  && $this->width * $this->height > 100 * 100 ) {
134  self::$firstNonIconImageRendered = true;
135 
136  // Generate a random number between 0.01 and 1.0, included
137  $random = rand( 1, 100 ) / 100.0;
138 
139  if ( $random <= $wgPriorityHintsRatio ) {
140  $attribs['importance'] = 'high';
141  $elementTimingName = 'thumbnail-high';
142  } else {
143  // This lets us track that the thumbnail *would* have gotten high priority but didn't.
144  $elementTimingName = 'thumbnail-top';
145  }
146  }
147 
148  if ( $wgElementTiming ) {
149  $attribs['elementtiming'] = $elementTimingName;
150  }
151 
152  if ( !empty( $options['custom-url-link'] ) ) {
153  $linkAttribs = [ 'href' => $options['custom-url-link'] ];
154  if ( !empty( $options['title'] ) ) {
155  $linkAttribs['title'] = $options['title'];
156  }
157  if ( !empty( $options['custom-target-link'] ) ) {
158  $linkAttribs['target'] = $options['custom-target-link'];
159  } elseif ( !empty( $options['parser-extlink-target'] ) ) {
160  $linkAttribs['target'] = $options['parser-extlink-target'];
161  }
162  if ( !empty( $options['parser-extlink-rel'] ) ) {
163  $linkAttribs['rel'] = $options['parser-extlink-rel'];
164  }
165  } elseif ( !empty( $options['custom-title-link'] ) ) {
167  $title = $options['custom-title-link'];
168  $linkAttribs = [
169  'href' => $title->getLinkURL(),
170  'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
171  ];
172  } elseif ( !empty( $options['desc-link'] ) ) {
173  $linkAttribs = $this->getDescLinkAttribs(
174  empty( $options['title'] ) ? null : $options['title'],
175  $query
176  );
177  } elseif ( !empty( $options['file-link'] ) ) {
178  $linkAttribs = [ 'href' => $this->file->getUrl() ];
179  } else {
180  $linkAttribs = false;
181  if ( !empty( $options['title'] ) ) {
182  $attribs['title'] = $options['title'];
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::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] );
211 
212  return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
213  }
214 }
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:1566
toHtml( $options=[])
Return HTML.
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
static $firstNonIconImageRendered
bool string $path
Filesystem path to the thumb.
string $url
URL path to the thumb.
width
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1...
target page
Base class for the output of MediaHandler::doTransform() and File::transform().
bool $wgPriorityHints
Enable client-side Priority Hints.
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing we can concentrate it all in an extension file
Definition: hooks.txt:91
__construct( $file, $url, $path=false, $parameters=[])
Get a thumbnail object from a file and parameters.
Media transform output for images.
getDescLinkAttribs( $title=null, $params=[])
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:1971
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:1971
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
linkWrap( $linkAttribs, $contents)
Wrap some XHTML text in an anchor tag with the given attributes.
bool $wgElementTiming
Enable Element Timing.
float $wgPriorityHintsRatio
Ratio of requests that should get Priority Hints when the feature is enabled.
static srcSet(array $urls)
Generate a srcset attribute value.
Definition: Html.php:1062
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
int $height
Image height.
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200