Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[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 = isset( $this->mAttribs['style'] ) ? $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 /** @var Title $nt */
76 $nt = $pair[0];
77 $text = $pair[1]; # "text" means "caption" here
78 $alt = $pair[2];
79 $link = $pair[3];
80
81 $descQuery = false;
82 if ( $nt->getNamespace() === NS_FILE ) {
83 # Get the file...
84 if ( $this->mParser instanceof Parser ) {
85 # Give extensions a chance to select the file revision for us
86 $options = [];
87 Hooks::run( 'BeforeParserFetchFileAndTitle',
88 [ $this->mParser, $nt, &$options, &$descQuery ] );
89 # Fetch and register the file (file title may be different via hooks)
90 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
91 } else {
92 $img = wfFindFile( $nt );
93 }
94 } else {
95 $img = false;
96 }
97
98 $params = $this->getThumbParams( $img );
99 // $pair[4] is per image handler options
100 $transformOptions = $params + $pair[4];
101
102 $thumb = false;
103
104 if ( !$img ) {
105 # We're dealing with a non-image, spit out the name and be done with it.
106 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
107 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
108 . htmlspecialchars( $nt->getText() ) . '</div>';
109
110 if ( $this->mParser instanceof Parser ) {
111 $this->mParser->addTrackingCategory( 'broken-file-category' );
112 }
113 } elseif ( $this->mHideBadImages
114 && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
115 ) {
116 # The image is blacklisted, just show it as a text link.
117 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
118 ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
119 Linker::linkKnown(
120 $nt,
121 htmlspecialchars( $nt->getText() )
122 ) .
123 '</div>';
124 } else {
125 $thumb = $img->transform( $transformOptions );
126 if ( !$thumb ) {
127 # Error generating thumbnail.
128 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
129 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
130 . htmlspecialchars( $img->getLastError() ) . '</div>';
131 } else {
132 /** @var MediaTransformOutput $thumb */
133 $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
134
135 $imageParameters = [
136 'desc-link' => true,
137 'desc-query' => $descQuery,
138 'alt' => $alt,
139 'custom-url-link' => $link
140 ];
141
142 // In the absence of both alt text and caption, fall back on
143 // providing screen readers with the filename as alt text
144 if ( $alt == '' && $text == '' ) {
145 $imageParameters['alt'] = $nt->getText();
146 }
147
148 $this->adjustImageParameters( $thumb, $imageParameters );
149
150 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
151
152 # Set both fixed width and min-height.
153 $thumbhtml = "\n\t\t\t"
154 . '<div class="thumb" style="width: '
155 . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
156 # Auto-margin centering for block-level elements. Needed
157 # now that we have video handlers since they may emit block-
158 # level elements as opposed to simple <img> tags. ref
159 # http://css-discuss.incutio.com/?page=CenteringBlockElement
160 . '<div style="margin:' . $vpad . 'px auto;">'
161 . $thumb->toHtml( $imageParameters ) . '</div></div>';
162
163 // Call parser transform hook
164 /** @var MediaHandler $handler */
165 $handler = $img->getHandler();
166 if ( $this->mParser && $handler ) {
167 $handler->parserTransformHook( $this->mParser, $img );
168 }
169 }
170 }
171
172 // @todo Code is incomplete.
173 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) .
174 // ":{$ut}" );
175 // $ul = Linker::link( $linkTarget, $ut );
176
177 $meta = [];
178 if ( $img ) {
179 if ( $this->mShowDimensions ) {
180 $meta[] = $img->getDimensionsString();
181 }
182 if ( $this->mShowBytes ) {
183 $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
184 }
185 } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
186 $meta[] = $this->msg( 'filemissing' )->escaped();
187 }
188 $meta = $lang->semicolonList( $meta );
189 if ( $meta ) {
190 $meta .= "<br />\n";
191 }
192
193 $textlink = $this->mShowFilename ?
194 $this->getCaptionHtml( $nt, $lang ) :
195 '';
196
197 $galleryText = $textlink . $text . $meta;
198 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
199
200 $gbWidth = $this->getGBWidth( $thumb ) . 'px';
201 if ( $this->getGBWidthOverwrite( $thumb ) ) {
202 $gbWidth = $this->getGBWidthOverwrite( $thumb );
203 }
204 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
205 # Can be safely removed if FF2 falls completely out of existence
206 $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
207 . $gbWidth . '">'
208 . '<div style="width: ' . $gbWidth . '">'
209 . $thumbhtml
210 . $galleryText
211 . "\n\t\t</div></li>";
212 }
213 $output .= "\n</ul>";
214
215 return $output;
216 }
217
218 /**
219 * @param Title $nt
220 * @param Language $lang
221 * @return string HTML
222 */
223 protected function getCaptionHtml( Title $nt, Language $lang ) {
224 // Preloaded into LinkCache in toHTML
225 return Linker::linkKnown(
226 $nt,
227 htmlspecialchars(
228 is_int( $this->getCaptionLength() ) ?
229 $lang->truncate( $nt->getText(), $this->getCaptionLength() ) :
230 $nt->getText()
231 ),
232 [
233 'class' => 'galleryfilename' .
234 ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
235 ]
236 ) . "\n";
237 }
238
239 /**
240 * Add the wrapper html around the thumb's caption
241 *
242 * @param string $galleryText The caption
243 * @param MediaTransformOutput|bool $thumb The thumb this caption is for
244 * or false for bad image.
245 * @return string
246 */
247 protected function wrapGalleryText( $galleryText, $thumb ) {
248 # ATTENTION: The newline after <div class="gallerytext"> is needed to
249 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
250 # its absence, see: https://phabricator.wikimedia.org/T3765
251 # -Ævar
252
253 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
254 . $galleryText
255 . "\n\t\t\t</div>";
256 }
257
258 /**
259 * How much padding the thumb has between the image and the inner div
260 * that contains the border. This is for both vertical and horizontal
261 * padding. (However, it is cut in half in the vertical direction).
262 * @return int
263 */
264 protected function getThumbPadding() {
265 return 30;
266 }
267
268 /**
269 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
270 *
271 * @return int
272 */
273 protected function getGBPadding() {
274 return 5;
275 }
276
277 /**
278 * Get how much extra space the borders around the image takes up.
279 *
280 * For this mode, it is 2px borders on each side + 2px implied padding on
281 * each side from the stylesheet, giving us 2*2+2*2 = 8.
282 * @return int
283 */
284 protected function getGBBorders() {
285 return 8;
286 }
287
288 /**
289 * Length to truncate filename to in caption when using "showfilename" (if int).
290 * A value of 'true' will truncate the filename to one line using CSS, while
291 * 'false' will disable truncating.
292 *
293 * @return int|bool
294 */
295 protected function getCaptionLength() {
296 return $this->mCaptionLength;
297 }
298
299 /**
300 * Get total padding.
301 *
302 * @return int Number of pixels of whitespace surrounding the thumbnail.
303 */
304 protected function getAllPadding() {
305 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
306 }
307
308 /**
309 * Get vertical padding for a thumbnail
310 *
311 * Generally this is the total height minus how high the thumb is.
312 *
313 * @param int $boxHeight How high we want the box to be.
314 * @param int $thumbHeight How high the thumbnail is.
315 * @return int Vertical padding to add on each side.
316 */
317 protected function getVPad( $boxHeight, $thumbHeight ) {
318 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
319 }
320
321 /**
322 * Get the transform parameters for a thumbnail.
323 *
324 * @param File $img The file in question. May be false for invalid image
325 * @return array
326 */
327 protected function getThumbParams( $img ) {
328 return [
329 'width' => $this->mWidths,
330 'height' => $this->mHeights
331 ];
332 }
333
334 /**
335 * Get the width of the inner div that contains the thumbnail in
336 * question. This is the div with the class of "thumb".
337 *
338 * @param int $thumbWidth The width of the thumbnail.
339 * @return int Width of inner thumb div.
340 */
341 protected function getThumbDivWidth( $thumbWidth ) {
342 return $this->mWidths + $this->getThumbPadding();
343 }
344
345 /**
346 * Computed width of gallerybox <li>.
347 *
348 * Generally is the width of the image, plus padding on image
349 * plus padding on gallerybox.
350 *
351 * @note Important: parameter will be false if no thumb used.
352 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
353 * @return int Width of gallerybox element
354 */
355 protected function getGBWidth( $thumb ) {
356 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
357 }
358
359 /**
360 * Allows overwriting the computed width of the gallerybox <li> with a string,
361 * like '100%'.
362 *
363 * Generally is the width of the image, plus padding on image
364 * plus padding on gallerybox.
365 *
366 * @note Important: parameter will be false if no thumb used.
367 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
368 * @return bool|string Ignored if false.
369 */
370 protected function getGBWidthOverwrite( $thumb ) {
371 return false;
372 }
373
374 /**
375 * Get a list of modules to include in the page.
376 *
377 * Primarily intended for subclasses.
378 *
379 * @return array Modules to include
380 */
381 protected function getModules() {
382 return [];
383 }
384
385 /**
386 * Adjust the image parameters for a thumbnail.
387 *
388 * Used by a subclass to insert extra high resolution images.
389 * @param MediaTransformOutput $thumb The thumbnail
390 * @param array &$imageParameters Array of options
391 */
392 protected function adjustImageParameters( $thumb, &$imageParameters ) {
393 }
394 }