Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / gallery / TraditionalImageGallery.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * Image gallery.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 class TraditionalImageGallery extends ImageGalleryBase {
27 /**
28 * Return a HTML representation of the image gallery
29 *
30 * For each image in the gallery, display
31 * - a thumbnail
32 * - the image name
33 * - the additional text provided when adding the image
34 * - the size of the image
35 *
36 * @return string
37 */
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
79 /** @var Title $nt */
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
115 && wfIsBadImage( $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;">' .
120 Linker::linkKnown(
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 {
133 /** @var MediaTransformOutput $thumb */
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
165 /** @var MediaHandler $handler */
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
220 /**
221 * @param Title $nt
222 * @param Language $lang
223 * @return string HTML
224 */
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
241 /**
242 * Add the wrapper html around the thumb's caption
243 *
244 * @param string $galleryText The caption
245 * @param MediaTransformOutput|bool $thumb The thumb this caption is for
246 * or false for bad image.
247 * @return string
248 */
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
260 /**
261 * How much padding the thumb has between the image and the inner div
262 * that contains the border. This is for both vertical and horizontal
263 * padding. (However, it is cut in half in the vertical direction).
264 * @return int
265 */
266 protected function getThumbPadding() {
267 return 30;
268 }
269
270 /**
271 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
272 *
273 * @return int
274 */
275 protected function getGBPadding() {
276 return 5;
277 }
278
279 /**
280 * Get how much extra space the borders around the image takes up.
281 *
282 * For this mode, it is 2px borders on each side + 2px implied padding on
283 * each side from the stylesheet, giving us 2*2+2*2 = 8.
284 * @return int
285 */
286 protected function getGBBorders() {
287 return 8;
288 }
289
290 /**
291 * Length (in characters) to truncate filename to in caption when using "showfilename" (if int).
292 * A value of 'true' will truncate the filename to one line using CSS, while
293 * 'false' will disable truncating.
294 *
295 * @return int|bool
296 */
297 protected function getCaptionLength() {
298 return $this->mCaptionLength;
299 }
300
301 /**
302 * Get total padding.
303 *
304 * @return int Number of pixels of whitespace surrounding the thumbnail.
305 */
306 protected function getAllPadding() {
307 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
308 }
309
310 /**
311 * Get vertical padding for a thumbnail
312 *
313 * Generally this is the total height minus how high the thumb is.
314 *
315 * @param int $boxHeight How high we want the box to be.
316 * @param int $thumbHeight How high the thumbnail is.
317 * @return int Vertical padding to add on each side.
318 */
319 protected function getVPad( $boxHeight, $thumbHeight ) {
320 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
321 }
322
323 /**
324 * Get the transform parameters for a thumbnail.
325 *
326 * @param File $img The file in question. May be false for invalid image
327 * @return array
328 */
329 protected function getThumbParams( $img ) {
330 return [
331 'width' => $this->mWidths,
332 'height' => $this->mHeights
333 ];
334 }
335
336 /**
337 * Get the width of the inner div that contains the thumbnail in
338 * question. This is the div with the class of "thumb".
339 *
340 * @param int $thumbWidth The width of the thumbnail.
341 * @return int Width of inner thumb div.
342 */
343 protected function getThumbDivWidth( $thumbWidth ) {
344 return $this->mWidths + $this->getThumbPadding();
345 }
346
347 /**
348 * Computed width of gallerybox <li>.
349 *
350 * Generally is the width of the image, plus padding on image
351 * plus padding on gallerybox.
352 *
353 * @note Important: parameter will be false if no thumb used.
354 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
355 * @return int Width of gallerybox element
356 */
357 protected function getGBWidth( $thumb ) {
358 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
359 }
360
361 /**
362 * Allows overwriting the computed width of the gallerybox <li> with a string,
363 * like '100%'.
364 *
365 * Generally is the width of the image, plus padding on image
366 * plus padding on gallerybox.
367 *
368 * @note Important: parameter will be false if no thumb used.
369 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
370 * @return bool|string Ignored if false.
371 */
372 protected function getGBWidthOverwrite( $thumb ) {
373 return false;
374 }
375
376 /**
377 * Get a list of modules to include in the page.
378 *
379 * Primarily intended for subclasses.
380 *
381 * @return array Modules to include
382 */
383 protected function getModules() {
384 return [];
385 }
386
387 /**
388 * Adjust the image parameters for a thumbnail.
389 *
390 * Used by a subclass to insert extra high resolution images.
391 * @param MediaTransformOutput $thumb The thumbnail
392 * @param array &$imageParameters Array of options
393 */
394 protected function adjustImageParameters( $thumb, &$imageParameters ) {
395 }
396 }