Merge "Add more test cases to OldChangesListTest"
[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 array( 'class' => 'gallery mw-gallery-' . $this->mMode ), $this->mAttribs );
47
48 $modules = $this->getModules();
49 $modules[] = 'mediawiki.page.gallery.styles';
50
51 if ( $this->mParser ) {
52 $this->mParser->getOutput()->addModules( $modules );
53 } else {
54 $this->getOutput()->addModules( $modules );
55 }
56 $output = Xml::openElement( 'ul', $attribs );
57 if ( $this->mCaption ) {
58 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
59 }
60
61 $lang = $this->getRenderLang();
62 # Output each image...
63 foreach ( $this->mImages as $pair ) {
64 /** @var Title $nt */
65 $nt = $pair[0];
66 $text = $pair[1]; # "text" means "caption" here
67 $alt = $pair[2];
68 $link = $pair[3];
69
70 $descQuery = false;
71 if ( $nt->getNamespace() === NS_FILE ) {
72 # Get the file...
73 if ( $this->mParser instanceof Parser ) {
74 # Give extensions a chance to select the file revision for us
75 $options = array();
76 Hooks::run( 'BeforeParserFetchFileAndTitle',
77 array( $this->mParser, $nt, &$options, &$descQuery ) );
78 # Fetch and register the file (file title may be different via hooks)
79 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
80 } else {
81 $img = wfFindFile( $nt );
82 }
83 } else {
84 $img = false;
85 }
86
87 $params = $this->getThumbParams( $img );
88 // $pair[4] is per image handler options
89 $transformOptions = $params + $pair[4];
90
91 $thumb = false;
92
93 if ( !$img ) {
94 # We're dealing with a non-image, spit out the name and be done with it.
95 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
96 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
97 . htmlspecialchars( $nt->getText() ) . '</div>';
98
99 if ( $this->mParser instanceof Parser ) {
100 $this->mParser->addTrackingCategory( 'broken-file-category' );
101 }
102 } elseif ( $this->mHideBadImages
103 && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
104 ) {
105 # The image is blacklisted, just show it as a text link.
106 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
107 ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
108 Linker::linkKnown(
109 $nt,
110 htmlspecialchars( $nt->getText() )
111 ) .
112 '</div>';
113 } elseif ( !( $thumb = $img->transform( $transformOptions ) ) ) {
114 # Error generating thumbnail.
115 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
116 . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
117 . htmlspecialchars( $img->getLastError() ) . '</div>';
118 } else {
119 /** @var MediaTransformOutput $thumb */
120 $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
121
122 $imageParameters = array(
123 'desc-link' => true,
124 'desc-query' => $descQuery,
125 'alt' => $alt,
126 'custom-url-link' => $link
127 );
128
129 // In the absence of both alt text and caption, fall back on
130 // providing screen readers with the filename as alt text
131 if ( $alt == '' && $text == '' ) {
132 $imageParameters['alt'] = $nt->getText();
133 }
134
135 $this->adjustImageParameters( $thumb, $imageParameters );
136
137 Linker::processResponsiveImages( $img, $thumb, $transformOptions );
138
139 # Set both fixed width and min-height.
140 $thumbhtml = "\n\t\t\t"
141 . '<div class="thumb" style="width: '
142 . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
143 # Auto-margin centering for block-level elements. Needed
144 # now that we have video handlers since they may emit block-
145 # level elements as opposed to simple <img> tags. ref
146 # http://css-discuss.incutio.com/?page=CenteringBlockElement
147 . '<div style="margin:' . $vpad . 'px auto;">'
148 . $thumb->toHtml( $imageParameters ) . '</div></div>';
149
150 // Call parser transform hook
151 /** @var MediaHandler $handler */
152 $handler = $img->getHandler();
153 if ( $this->mParser && $handler ) {
154 $handler->parserTransformHook( $this->mParser, $img );
155 }
156 }
157
158 // @todo Code is incomplete.
159 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) .
160 // ":{$ut}" );
161 // $ul = Linker::link( $linkTarget, $ut );
162
163 if ( $this->mShowBytes ) {
164 if ( $img ) {
165 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
166 } else {
167 $fileSize = $this->msg( 'filemissing' )->escaped();
168 }
169 $fileSize = "$fileSize<br />\n";
170 } else {
171 $fileSize = '';
172 }
173
174 $textlink = $this->mShowFilename ?
175 Linker::linkKnown(
176 $nt,
177 htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) )
178 ) . "<br />\n" :
179 '';
180
181 $galleryText = $textlink . $text . $fileSize;
182 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
183
184 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
185 # Can be safely removed if FF2 falls completely out of existence
186 $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
187 . $this->getGBWidth( $thumb ) . 'px">'
188 . '<div style="width: ' . $this->getGBWidth( $thumb ) . 'px">'
189 . $thumbhtml
190 . $galleryText
191 . "\n\t\t</div></li>";
192 }
193 $output .= "\n</ul>";
194
195 return $output;
196 }
197
198 /**
199 * Add the wrapper html around the thumb's caption
200 *
201 * @param string $galleryText The caption
202 * @param MediaTransformOutput|bool $thumb The thumb this caption is for
203 * or false for bad image.
204 * @return string
205 */
206 protected function wrapGalleryText( $galleryText, $thumb ) {
207 # ATTENTION: The newline after <div class="gallerytext"> is needed to
208 # accommodate htmltidy which in version 4.8.6 generated crackpot html in
209 # its absence, see: https://phabricator.wikimedia.org/T3765
210 # -Ævar
211
212 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
213 . $galleryText
214 . "\n\t\t\t</div>";
215 }
216
217 /**
218 * How much padding such the thumb have between image and inner div that
219 * that contains the border. This is both for verical and horizontal
220 * padding. (However, it is cut in half in the vertical direction).
221 * @return int
222 */
223 protected function getThumbPadding() {
224 return 30;
225 }
226
227 /**
228 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
229 *
230 * @return int
231 */
232 protected function getGBPadding() {
233 return 5;
234 }
235
236 /**
237 * Get how much extra space the borders around the image takes up.
238 *
239 * For this mode, it is 2px borders on each side + 2px implied padding on
240 * each side from the stylesheet, giving us 2*2+2*2 = 8.
241 * @return int
242 */
243 protected function getGBBorders() {
244 return 8;
245 }
246
247 /**
248 * Get total padding.
249 *
250 * @return int Number of pixels of whitespace surrounding the thumbnail.
251 */
252 protected function getAllPadding() {
253 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
254 }
255
256 /**
257 * Get vertical padding for a thumbnail
258 *
259 * Generally this is the total height minus how high the thumb is.
260 *
261 * @param int $boxHeight How high we want the box to be.
262 * @param int $thumbHeight How high the thumbnail is.
263 * @return int Vertical padding to add on each side.
264 */
265 protected function getVPad( $boxHeight, $thumbHeight ) {
266 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
267 }
268
269 /**
270 * Get the transform parameters for a thumbnail.
271 *
272 * @param File $img The file in question. May be false for invalid image
273 * @return array
274 */
275 protected function getThumbParams( $img ) {
276 return array(
277 'width' => $this->mWidths,
278 'height' => $this->mHeights
279 );
280 }
281
282 /**
283 * Get the width of the inner div that contains the thumbnail in
284 * question. This is the div with the class of "thumb".
285 *
286 * @param int $thumbWidth The width of the thumbnail.
287 * @return int Width of inner thumb div.
288 */
289 protected function getThumbDivWidth( $thumbWidth ) {
290 return $this->mWidths + $this->getThumbPadding();
291 }
292
293 /**
294 * Width of gallerybox <li>.
295 *
296 * Generally is the width of the image, plus padding on image
297 * plus padding on gallerybox.
298 *
299 * @note Important: parameter will be false if no thumb used.
300 * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
301 * @return int Width of gallerybox element
302 */
303 protected function getGBWidth( $thumb ) {
304 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
305 }
306
307 /**
308 * Get a list of modules to include in the page.
309 *
310 * Primarily intended for subclasses.
311 *
312 * @return array Modules to include
313 */
314 protected function getModules() {
315 return array();
316 }
317
318 /**
319 * Adjust the image parameters for a thumbnail.
320 *
321 * Used by a subclass to insert extra high resolution images.
322 * @param MediaTransformOutput $thumb The thumbnail
323 * @param array $imageParameters Array of options
324 */
325 protected function adjustImageParameters( $thumb, &$imageParameters ) {
326 }
327 }
328
329 /**
330 * Backwards compatibility. This always uses traditional mode
331 * if called the old way, for extensions that may expect traditional
332 * mode.
333 *
334 * @deprecated since 1.22 Use ImageGalleryBase::factory instead.
335 */
336 class ImageGallery extends TraditionalImageGallery {
337 function __construct( $mode = 'traditional' ) {
338 wfDeprecated( __METHOD__, '1.22' );
339 parent::__construct( $mode );
340 }
341 }