Merge "Remove parameter 'options' from hook 'SkinEditSectionLinks'"
[lhc/web/wiklou.git] / includes / gallery / TraditionalImageGallery.php
1 <?php
2 /**
3 * Image gallery.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 class TraditionalImageGallery extends ImageGalleryBase {
24 /**
25 * Return a HTML representation of the image gallery
26 *
27 * For each image in the gallery, display
28 * - a thumbnail
29 * - the image name
30 * - the additional text provided when adding the image
31 * - the size of the image
32 *
33 * @return string
34 */
35 function toHTML() {
36 if ( $this->mPerRow > 0 ) {
37 $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
38 $oldStyle = $this->mAttribs['style'] ?? '';
39 # _width is ignored by any sane browser. IE6 doesn't know max-width
40 # so it uses _width instead
41 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" .
42 $oldStyle;
43 }
44
45 $attribs = Sanitizer::mergeAttributes(
46 [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
47
48 $modules = $this->getModules();
49
50 if ( $this->mParser ) {
51 $this->mParser->getOutput()->addModules( $modules );
52 $this->mParser->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
53 } else {
54 $this->getOutput()->addModules( $modules );
55 $this->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
56 }
57 $output = Xml::openElement( 'ul', $attribs );
58 if ( $this->mCaption ) {
59 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
60 }
61
62 if ( $this->mShowFilename ) {
63 // Preload LinkCache info for when generating links
64 // of the filename below
65 $lb = new LinkBatch();
66 foreach ( $this->mImages as $img ) {
67 $lb->addObj( $img[0] );
68 }
69 $lb->execute();
70 }
71
72 $lang = $this->getRenderLang();
73 # Output each image...
74 foreach ( $this->mImages as $pair ) {
75 // "text" means "caption" here
76 /** @var Title $nt */
77 list( $nt, $text, $alt, $link ) = $pair;
78
79 $descQuery = false;
80 if ( $nt->getNamespace() === NS_FILE ) {
81 # Get the file...
82 if ( $this->mParser instanceof Parser ) {
83 # Give extensions a chance to select the file revision for us
84 $options = [];
85 Hooks::run( 'BeforeParserFetchFileAndTitle',
86 [ $this->mParser, $nt, &$options, &$descQuery ] );
87 # Fetch and register the file (file title may be different via hooks)
88 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
89 } else {
90 $img = wfFindFile( $nt );
91 }
92 } else {
93 $img = false;
94 }
95
96 $params = $this->getThumbParams( $img );
97 // $pair[4] is per image handler options
98 $transformOptions = $params + $pair[4];
99
100 $thumb = false;
101
102 if ( !$img ) {
103 # We're dealing with a non-image, spit out the name and be done with it.
104 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
105 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
106 . htmlspecialchars( $nt->getText() ) . '</div>';
107
108 if ( $this->mParser instanceof Parser ) {
109 $this->mParser->addTrackingCategory( 'broken-file-category' );
110 }
111 } elseif ( $this->mHideBadImages
112 && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
113 ) {
114 # The image is blacklisted, just show it as a text link.
115 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
116 ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
117 Linker::linkKnown(
118 $nt,
119 htmlspecialchars( $nt->getText() )
120 ) .
121 '</div>';
122 } else {
123 $thumb = $img->transform( $transformOptions );
124 if ( !$thumb ) {
125 # Error generating thumbnail.
126 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
127 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
128 . htmlspecialchars( $img->getLastError() ) . '</div>';
129 } else {
130 /** @var MediaTransformOutput $thumb */
131 $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
132
133 $imageParameters = [
134 'desc-link' => true,
135 'desc-query' => $descQuery,
136 'alt' => $alt,
137 'custom-url-link' => $link
138 ];
139
140 // In the absence of both alt text and caption, fall back on
141 // providing screen readers with the filename as alt text
142 if ( $alt == '' && $text == '' ) {
143 $imageParameters['alt'] = $nt->getText();
144 }
145
146 $this->adjustImageParameters( $thumb, $imageParameters );
147
148 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
149
150 # Set both fixed width and min-height.
151 $thumbhtml = "\n\t\t\t"
152 . '<div class="thumb" style="width: '
153 . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
154 # Auto-margin centering for block-level elements. Needed
155 # now that we have video handlers since they may emit block-
156 # level elements as opposed to simple <img> tags. ref
157 # http://css-discuss.incutio.com/?page=CenteringBlockElement
158 . '<div style="margin:' . $vpad . 'px auto;">'
159 . $thumb->toHtml( $imageParameters ) . '</div></div>';
160
161 // Call parser transform hook
162 /** @var MediaHandler $handler */
163 $handler = $img->getHandler();
164 if ( $this->mParser && $handler ) {
165 $handler->parserTransformHook( $this->mParser, $img );
166 }
167 }
168 }
169
170 // @todo Code is incomplete.
171 // $linkTarget = Title::newFromText( MediaWikiServices::getInstance()->
172 // getContentLanguage()->getNsText( MediaWikiServices::getInstance()->
173 // getNamespaceInfo()->getUser() ) . ":{$ut}" );
174 // $ul = Linker::link( $linkTarget, $ut );
175
176 $meta = [];
177 if ( $img ) {
178 if ( $this->mShowDimensions ) {
179 $meta[] = $img->getDimensionsString();
180 }
181 if ( $this->mShowBytes ) {
182 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
183 }
184 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
185 $meta[] = $this->msg( 'filemissing' )->escaped();
186 }
187 $meta = $lang->semicolonList( $meta );
188 if ( $meta ) {
189 $meta .= "<br />\n";
190 }
191
192 $textlink = $this->mShowFilename ?
193 $this->getCaptionHtml( $nt, $lang ) :
194 '';
195
196 $galleryText = $textlink . $text . $meta;
197 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
198
199 $gbWidth = $this->getGBWidth( $thumb ) . 'px';
200 if ( $this->getGBWidthOverwrite( $thumb ) ) {
201 $gbWidth = $this->getGBWidthOverwrite( $thumb );
202 }
203 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
204 # Can be safely removed if FF2 falls completely out of existence
205 $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
206 . $gbWidth . '">'
207 . '<div style="width: ' . $gbWidth . '">'
208 . $thumbhtml
209 . $galleryText
210 . "\n\t\t</div></li>";
211 }
212 $output .= "\n</ul>";
213
214 return $output;
215 }
216
217 /**
218 * @param Title $nt
219 * @param Language $lang
220 * @return string HTML
221 */
222 protected function getCaptionHtml( Title $nt, Language $lang ) {
223 // Preloaded into LinkCache in toHTML
224 return Linker::linkKnown(
225 $nt,
226 htmlspecialchars(
227 is_int( $this->getCaptionLength() ) ?
228 $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
229 $nt->getText()
230 ),
231 [
232 'class' => 'galleryfilename' .
233 ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
234 ]
235 ) . "\n";
236 }
237
238 /**
239 * Add the wrapper html around the thumb's caption
240 *
241 * @param string $galleryText The caption
242 * @param MediaTransformOutput|bool $thumb The thumb this caption is for
243 * or false for bad image.
244 * @return string
245 */
246 protected function wrapGalleryText( $galleryText, $thumb ) {
247 # ATTENTION: The newline after <div class="gallerytext"> is needed to
248 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
249 # its absence, see: https://phabricator.wikimedia.org/T3765
250 # -Ævar
251
252 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
253 . $galleryText
254 . "\n\t\t\t</div>";
255 }
256
257 /**
258 * How much padding the thumb has between the image and the inner div
259 * that contains the border. This is for both vertical and horizontal
260 * padding. (However, it is cut in half in the vertical direction).
261 * @return int
262 */
263 protected function getThumbPadding() {
264 return 30;
265 }
266
267 /**
268 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
269 *
270 * @return int
271 */
272 protected function getGBPadding() {
273 return 5;
274 }
275
276 /**
277 * Get how much extra space the borders around the image takes up.
278 *
279 * For this mode, it is 2px borders on each side + 2px implied padding on
280 * each side from the stylesheet, giving us 2*2+2*2 = 8.
281 * @return int
282 */
283 protected function getGBBorders() {
284 return 8;
285 }
286
287 /**
288 * Length (in characters) to truncate filename to in caption when using "showfilename" (if int).
289 * A value of 'true' will truncate the filename to one line using CSS, while
290 * 'false' will disable truncating.
291 *
292 * @return int|bool
293 */
294 protected function getCaptionLength() {
295 return $this->mCaptionLength;
296 }
297
298 /**
299 * Get total padding.
300 *
301 * @return int Number of pixels of whitespace surrounding the thumbnail.
302 */
303 protected function getAllPadding() {
304 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
305 }
306
307 /**
308 * Get vertical padding for a thumbnail
309 *
310 * Generally this is the total height minus how high the thumb is.
311 *
312 * @param int $boxHeight How high we want the box to be.
313 * @param int $thumbHeight How high the thumbnail is.
314 * @return int Vertical padding to add on each side.
315 */
316 protected function getVPad( $boxHeight, $thumbHeight ) {
317 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
318 }
319
320 /**
321 * Get the transform parameters for a thumbnail.
322 *
323 * @param File $img The file in question. May be false for invalid image
324 * @return array
325 */
326 protected function getThumbParams( $img ) {
327 return [
328 'width' => $this->mWidths,
329 'height' => $this->mHeights
330 ];
331 }
332
333 /**
334 * Get the width of the inner div that contains the thumbnail in
335 * question. This is the div with the class of "thumb".
336 *
337 * @param int $thumbWidth The width of the thumbnail.
338 * @return int Width of inner thumb div.
339 */
340 protected function getThumbDivWidth( $thumbWidth ) {
341 return $this->mWidths + $this->getThumbPadding();
342 }
343
344 /**
345 * Computed width of gallerybox <li>.
346 *
347 * Generally is the width of the image, plus padding on image
348 * plus padding on gallerybox.
349 *
350 * @note Important: parameter will be false if no thumb used.
351 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
352 * @return int Width of gallerybox element
353 */
354 protected function getGBWidth( $thumb ) {
355 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
356 }
357
358 /**
359 * Allows overwriting the computed width of the gallerybox <li> with a string,
360 * like '100%'.
361 *
362 * Generally is the width of the image, plus padding on image
363 * plus padding on gallerybox.
364 *
365 * @note Important: parameter will be false if no thumb used.
366 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
367 * @return bool|string Ignored if false.
368 */
369 protected function getGBWidthOverwrite( $thumb ) {
370 return false;
371 }
372
373 /**
374 * Get a list of modules to include in the page.
375 *
376 * Primarily intended for subclasses.
377 *
378 * @return array Modules to include
379 */
380 protected function getModules() {
381 return [];
382 }
383
384 /**
385 * Adjust the image parameters for a thumbnail.
386 *
387 * Used by a subclass to insert extra high resolution images.
388 * @param MediaTransformOutput $thumb The thumbnail
389 * @param array &$imageParameters Array of options
390 */
391 protected function adjustImageParameters( $thumb, &$imageParameters ) {
392 }
393 }