MediaWiki  1.34.4
TraditionalImageGallery.php
Go to the documentation of this file.
1 <?php
2 
4 
38  function toHTML() {
39  if ( $this->mPerRow > 0 ) {
40  $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
41  $oldStyle = $this->mAttribs['style'] ?? '';
42  # _width is ignored by any sane browser. IE6 doesn't know max-width
43  # so it uses _width instead
44  $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" .
45  $oldStyle;
46  }
47 
48  $attribs = Sanitizer::mergeAttributes(
49  [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
50 
51  $modules = $this->getModules();
52 
53  if ( $this->mParser ) {
54  $this->mParser->getOutput()->addModules( $modules );
55  $this->mParser->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
56  } else {
57  $this->getOutput()->addModules( $modules );
58  $this->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
59  }
60  $output = Xml::openElement( 'ul', $attribs );
61  if ( $this->mCaption ) {
62  $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
63  }
64 
65  if ( $this->mShowFilename ) {
66  // Preload LinkCache info for when generating links
67  // of the filename below
68  $lb = new LinkBatch();
69  foreach ( $this->mImages as $img ) {
70  $lb->addObj( $img[0] );
71  }
72  $lb->execute();
73  }
74 
75  $lang = $this->getRenderLang();
76  # Output each image...
77  foreach ( $this->mImages as $pair ) {
78  // "text" means "caption" here
80  list( $nt, $text, $alt, $link ) = $pair;
81 
82  $descQuery = false;
83  if ( $nt->getNamespace() === NS_FILE ) {
84  # Get the file...
85  if ( $this->mParser instanceof Parser ) {
86  # Give extensions a chance to select the file revision for us
87  $options = [];
88  Hooks::run( 'BeforeParserFetchFileAndTitle',
89  [ $this->mParser, $nt, &$options, &$descQuery ] );
90  # Fetch and register the file (file title may be different via hooks)
91  list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
92  } else {
93  $img = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $nt );
94  }
95  } else {
96  $img = false;
97  }
98 
99  $params = $this->getThumbParams( $img );
100  // $pair[4] is per image handler options
101  $transformOptions = $params + $pair[4];
102 
103  $thumb = false;
104 
105  if ( !$img ) {
106  # We're dealing with a non-image, spit out the name and be done with it.
107  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
108  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
109  . htmlspecialchars( $nt->getText() ) . '</div>';
110 
111  if ( $this->mParser instanceof Parser ) {
112  $this->mParser->addTrackingCategory( 'broken-file-category' );
113  }
114  } elseif ( $this->mHideBadImages && MediaWikiServices::getInstance()->getBadFileLookup()
115  ->isBadFile( $nt->getDBkey(), $this->getContextTitle() )
116  ) {
117  # The image is blacklisted, just show it as a text link.
118  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
119  ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
121  $nt,
122  htmlspecialchars( $nt->getText() )
123  ) .
124  '</div>';
125  } else {
126  $thumb = $img->transform( $transformOptions );
127  if ( !$thumb ) {
128  # Error generating thumbnail.
129  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
130  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
131  . htmlspecialchars( $img->getLastError() ) . '</div>';
132  } else {
134  $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
135 
136  $imageParameters = [
137  'desc-link' => true,
138  'desc-query' => $descQuery,
139  'alt' => $alt,
140  'custom-url-link' => $link
141  ];
142 
143  // In the absence of both alt text and caption, fall back on
144  // providing screen readers with the filename as alt text
145  if ( $alt == '' && $text == '' ) {
146  $imageParameters['alt'] = $nt->getText();
147  }
148 
149  $this->adjustImageParameters( $thumb, $imageParameters );
150 
151  Linker::processResponsiveImages( $img, $thumb, $transformOptions );
152 
153  # Set both fixed width and min-height.
154  $thumbhtml = "\n\t\t\t"
155  . '<div class="thumb" style="width: '
156  . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
157  # Auto-margin centering for block-level elements. Needed
158  # now that we have video handlers since they may emit block-
159  # level elements as opposed to simple <img> tags. ref
160  # http://css-discuss.incutio.com/?page=CenteringBlockElement
161  . '<div style="margin:' . $vpad . 'px auto;">'
162  . $thumb->toHtml( $imageParameters ) . '</div></div>';
163 
164  // Call parser transform hook
166  $handler = $img->getHandler();
167  if ( $this->mParser && $handler ) {
168  $handler->parserTransformHook( $this->mParser, $img );
169  }
170  }
171  }
172 
173  // @todo Code is incomplete.
174  // $linkTarget = Title::newFromText( MediaWikiServices::getInstance()->
175  // getContentLanguage()->getNsText( MediaWikiServices::getInstance()->
176  // getNamespaceInfo()->getUser() ) . ":{$ut}" );
177  // $ul = Linker::link( $linkTarget, $ut );
178 
179  $meta = [];
180  if ( $img ) {
181  if ( $this->mShowDimensions ) {
182  $meta[] = $img->getDimensionsString();
183  }
184  if ( $this->mShowBytes ) {
185  $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
186  }
187  } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
188  $meta[] = $this->msg( 'filemissing' )->escaped();
189  }
190  $meta = $lang->semicolonList( $meta );
191  if ( $meta ) {
192  $meta .= "<br />\n";
193  }
194 
195  $textlink = $this->mShowFilename ?
196  $this->getCaptionHtml( $nt, $lang ) :
197  '';
198 
199  $galleryText = $textlink . $text . $meta;
200  $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
201 
202  $gbWidth = $this->getGBWidth( $thumb ) . 'px';
203  if ( $this->getGBWidthOverwrite( $thumb ) ) {
204  $gbWidth = $this->getGBWidthOverwrite( $thumb );
205  }
206  # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
207  # Can be safely removed if FF2 falls completely out of existence
208  $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
209  . $gbWidth . '">'
210  . '<div style="width: ' . $gbWidth . '">'
211  . $thumbhtml
212  . $galleryText
213  . "\n\t\t</div></li>";
214  }
215  $output .= "\n</ul>";
216 
217  return $output;
218  }
219 
225  protected function getCaptionHtml( Title $nt, Language $lang ) {
226  // Preloaded into LinkCache in toHTML
227  return Linker::linkKnown(
228  $nt,
229  htmlspecialchars(
230  is_int( $this->getCaptionLength() ) ?
231  $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
232  $nt->getText()
233  ),
234  [
235  'class' => 'galleryfilename' .
236  ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
237  ]
238  ) . "\n";
239  }
240 
249  protected function wrapGalleryText( $galleryText, $thumb ) {
250  # ATTENTION: The newline after <div class="gallerytext"> is needed to
251  # accommodate htmltidy which in version 4.8.6 generated crackpot html in
252  # its absence, see: https://phabricator.wikimedia.org/T3765
253  # -Ævar
254 
255  return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
256  . $galleryText
257  . "\n\t\t\t</div>";
258  }
259 
266  protected function getThumbPadding() {
267  return 30;
268  }
269 
275  protected function getGBPadding() {
276  return 5;
277  }
278 
286  protected function getGBBorders() {
287  return 8;
288  }
289 
297  protected function getCaptionLength() {
298  return $this->mCaptionLength;
299  }
300 
306  protected function getAllPadding() {
307  return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
308  }
309 
319  protected function getVPad( $boxHeight, $thumbHeight ) {
320  return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
321  }
322 
329  protected function getThumbParams( $img ) {
330  return [
331  'width' => $this->mWidths,
332  'height' => $this->mHeights
333  ];
334  }
335 
343  protected function getThumbDivWidth( $thumbWidth ) {
344  return $this->mWidths + $this->getThumbPadding();
345  }
346 
357  protected function getGBWidth( $thumb ) {
358  return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
359  }
360 
372  protected function getGBWidthOverwrite( $thumb ) {
373  return false;
374  }
375 
383  protected function getModules() {
384  return [];
385  }
386 
394  protected function adjustImageParameters( $thumb, &$imageParameters ) {
395  }
396 }
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
TraditionalImageGallery\getThumbParams
getThumbParams( $img)
Get the transform parameters for a thumbnail.
Definition: TraditionalImageGallery.php:329
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:32
TraditionalImageGallery\toHTML
toHTML()
Return a HTML representation of the image gallery.
Definition: TraditionalImageGallery.php:38
Language\truncateForVisual
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
Definition: Language.php:3438
Sanitizer\mergeAttributes
static mergeAttributes( $a, $b)
Merge two sets of HTML attributes.
Definition: Sanitizer.php:936
MediaTransformOutput\toHtml
toHtml( $options=[])
Fetch HTML for this transform output.
ImageGalleryBase\$mHeights
int $mHeights
Definition: ImageGalleryBase.php:98
NS_FILE
const NS_FILE
Definition: Defines.php:75
ImageGalleryBase\$mCaptionLength
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
Definition: ImageGalleryBase.php:70
TraditionalImageGallery\getCaptionLength
getCaptionLength()
Length (in characters) to truncate filename to in caption when using "showfilename" (if int).
Definition: TraditionalImageGallery.php:297
Linker\linkKnown
static linkKnown( $target, $html=null, $customAttribs=[], $query=[], $options=[ 'known'])
Identical to link(), except $options defaults to 'known'.
Definition: Linker.php:141
Linker\processResponsiveImages
static processResponsiveImages( $file, $thumb, $hp)
Process responsive images: add 1.5x and 2x subimages to the thumbnail, where applicable.
Definition: Linker.php:642
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
TraditionalImageGallery\getGBWidthOverwrite
getGBWidthOverwrite( $thumb)
Allows overwriting the computed width of the gallerybox with a string, like '100'.
Definition: TraditionalImageGallery.php:372
TraditionalImageGallery\getGBBorders
getGBBorders()
Get how much extra space the borders around the image takes up.
Definition: TraditionalImageGallery.php:286
TraditionalImageGallery\getVPad
getVPad( $boxHeight, $thumbHeight)
Get vertical padding for a thumbnail.
Definition: TraditionalImageGallery.php:319
TraditionalImageGallery\getCaptionHtml
getCaptionHtml(Title $nt, Language $lang)
Definition: TraditionalImageGallery.php:225
TraditionalImageGallery
Definition: TraditionalImageGallery.php:26
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
$modules
$modules
Definition: HTMLFormElement.php:13
TraditionalImageGallery\wrapGalleryText
wrapGalleryText( $galleryText, $thumb)
Add the wrapper html around the thumb's caption.
Definition: TraditionalImageGallery.php:249
TraditionalImageGallery\getModules
getModules()
Get a list of modules to include in the page.
Definition: TraditionalImageGallery.php:383
TraditionalImageGallery\getGBWidth
getGBWidth( $thumb)
Computed width of gallerybox .
Definition: TraditionalImageGallery.php:357
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
TraditionalImageGallery\adjustImageParameters
adjustImageParameters( $thumb, &$imageParameters)
Adjust the image parameters for a thumbnail.
Definition: TraditionalImageGallery.php:394
TraditionalImageGallery\getGBPadding
getGBPadding()
Definition: TraditionalImageGallery.php:275
TraditionalImageGallery\getAllPadding
getAllPadding()
Get total padding.
Definition: TraditionalImageGallery.php:306
Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:74
ImageGalleryBase\getRenderLang
getRenderLang()
Determines the correct language to be used for this image gallery.
Definition: ImageGalleryBase.php:394
MediaTransformOutput
Base class for the output of MediaHandler::doTransform() and File::transform().
Definition: MediaTransformOutput.php:29
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ImageGalleryBase\$mWidths
int $mWidths
Definition: ImageGalleryBase.php:95
TraditionalImageGallery\getThumbPadding
getThumbPadding()
How much padding the thumb has between the image and the inner div that contains the border.
Definition: TraditionalImageGallery.php:266
TraditionalImageGallery\getThumbDivWidth
getThumbDivWidth( $thumbWidth)
Get the width of the inner div that contains the thumbnail in question.
Definition: TraditionalImageGallery.php:343
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
Title\getText
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:995
Language
Internationalisation code.
Definition: Language.php:37