Localisation updates from http://translatewiki.net.
[lhc/web/wiklou.git] / includes / ImageGallery.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 /**
24 * Image gallery
25 *
26 * Add images to the gallery using add(), then render that list to HTML using toHTML().
27 *
28 * @ingroup Media
29 */
30 class ImageGallery {
31 var $mImages, $mShowBytes, $mShowFilename;
32 var $mCaption = false;
33
34 /**
35 * Hide blacklisted images?
36 */
37 var $mHideBadImages;
38
39 /**
40 * Registered parser object for output callbacks
41 * @var Parser
42 */
43 var $mParser;
44
45 /**
46 * Contextual title, used when images are being screened
47 * against the bad image list
48 */
49 protected $contextTitle = false;
50
51 protected $mAttribs = array();
52
53 /**
54 * Fixed margins
55 */
56 const THUMB_PADDING = 30;
57 const GB_PADDING = 5;
58 // 2px borders on each side + 2px implied padding on each side
59 const GB_BORDERS = 8;
60
61 /**
62 * Create a new image gallery object.
63 */
64 function __construct() {
65 global $wgGalleryOptions;
66 $this->mImages = array();
67 $this->mShowBytes = $wgGalleryOptions['showBytes'];
68 $this->mShowFilename = true;
69 $this->mParser = false;
70 $this->mHideBadImages = false;
71 $this->mPerRow = $wgGalleryOptions['imagesPerRow'];
72 $this->mWidths = $wgGalleryOptions['imageWidth'];
73 $this->mHeights = $wgGalleryOptions['imageHeight'];
74 $this->mCaptionLength = $wgGalleryOptions['captionLength'];
75 }
76
77 /**
78 * Register a parser object
79 *
80 * @param $parser Parser
81 */
82 function setParser( $parser ) {
83 $this->mParser = $parser;
84 }
85
86 /**
87 * Set bad image flag
88 */
89 function setHideBadImages( $flag = true ) {
90 $this->mHideBadImages = $flag;
91 }
92
93 /**
94 * Set the caption (as plain text)
95 *
96 * @param string $caption Caption
97 */
98 function setCaption( $caption ) {
99 $this->mCaption = htmlspecialchars( $caption );
100 }
101
102 /**
103 * Set the caption (as HTML)
104 *
105 * @param string $caption Caption
106 */
107 public function setCaptionHtml( $caption ) {
108 $this->mCaption = $caption;
109 }
110
111 /**
112 * Set how many images will be displayed per row.
113 *
114 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
115 * invalid numbers will be rejected
116 */
117 public function setPerRow( $num ) {
118 if ( $num >= 0 ) {
119 $this->mPerRow = (int)$num;
120 }
121 }
122
123 /**
124 * Set how wide each image will be, in pixels.
125 *
126 * @param $num Integer > 0; invalid numbers will be ignored
127 */
128 public function setWidths( $num ) {
129 if ( $num > 0 ) {
130 $this->mWidths = (int)$num;
131 }
132 }
133
134 /**
135 * Set how high each image will be, in pixels.
136 *
137 * @param $num Integer > 0; invalid numbers will be ignored
138 */
139 public function setHeights( $num ) {
140 if ( $num > 0 ) {
141 $this->mHeights = (int)$num;
142 }
143 }
144
145 /**
146 * Instruct the class to use a specific skin for rendering
147 *
148 * @param $skin Skin object
149 * @deprecated since 1.18 Not used anymore
150 */
151 function useSkin( $skin ) {
152 wfDeprecated( __METHOD__, '1.18' );
153 /* no op */
154 }
155
156 /**
157 * Add an image to the gallery.
158 *
159 * @param $title Title object of the image that is added to the gallery
160 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
161 * @param $alt String: Alt text for the image
162 * @param $link String: Override image link (optional)
163 * @param $handlerOpts Array: Array of options for image handler (aka page number)
164 */
165 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
166 if ( $title instanceof File ) {
167 // Old calling convention
168 $title = $title->getTitle();
169 }
170 $this->mImages[] = array( $title, $html, $alt, $link, $handlerOpts );
171 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
172 }
173
174 /**
175 * Add an image at the beginning of the gallery.
176 *
177 * @param $title Title object of the image that is added to the gallery
178 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
179 * @param $alt String: Alt text for the image
180 * @param $link String: Override image link (optional)
181 * @param $handlerOpts Array: Array of options for image handler (aka page number)
182 */
183 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
184 if ( $title instanceof File ) {
185 // Old calling convention
186 $title = $title->getTitle();
187 }
188 array_unshift( $this->mImages, array( &$title, $html, $alt, $link, $handlerOpts ) );
189 }
190
191 /**
192 * isEmpty() returns true if the gallery contains no images
193 * @return bool
194 */
195 function isEmpty() {
196 return empty( $this->mImages );
197 }
198
199 /**
200 * Enable/Disable showing of the file size of an image in the gallery.
201 * Enabled by default.
202 *
203 * @param $f Boolean: set to false to disable.
204 */
205 function setShowBytes( $f ) {
206 $this->mShowBytes = (bool)$f;
207 }
208
209 /**
210 * Enable/Disable showing of the filename of an image in the gallery.
211 * Enabled by default.
212 *
213 * @param $f Boolean: set to false to disable.
214 */
215 function setShowFilename( $f ) {
216 $this->mShowFilename = (bool)$f;
217 }
218
219 /**
220 * Set arbitrary attributes to go on the HTML gallery output element.
221 * Should be suitable for a <ul> element.
222 *
223 * Note -- if taking from user input, you should probably run through
224 * Sanitizer::validateAttributes() first.
225 *
226 * @param array $attribs of HTML attribute pairs
227 */
228 function setAttributes( $attribs ) {
229 $this->mAttribs = $attribs;
230 }
231
232 /**
233 * Return a HTML representation of the image gallery
234 *
235 * For each image in the gallery, display
236 * - a thumbnail
237 * - the image name
238 * - the additional text provided when adding the image
239 * - the size of the image
240 *
241 * @return string
242 */
243 function toHTML() {
244 if ( $this->mPerRow > 0 ) {
245 $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS );
246 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : '';
247 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
248 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
249 }
250
251 $attribs = Sanitizer::mergeAttributes(
252 array( 'class' => 'gallery' ), $this->mAttribs );
253
254 $output = Xml::openElement( 'ul', $attribs );
255 if ( $this->mCaption ) {
256 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
257 }
258
259 $lang = $this->getLang();
260 $params = array(
261 'width' => $this->mWidths,
262 'height' => $this->mHeights
263 );
264 # Output each image...
265 foreach ( $this->mImages as $pair ) {
266 $nt = $pair[0];
267 $text = $pair[1]; # "text" means "caption" here
268 $alt = $pair[2];
269 $link = $pair[3];
270 // $pair[4] is per image handler options
271 $transformOptions = $params + $pair[4];
272
273 $descQuery = false;
274 if ( $nt->getNamespace() == NS_FILE ) {
275 # Get the file...
276 if ( $this->mParser instanceof Parser ) {
277 # Give extensions a chance to select the file revision for us
278 $options = array();
279 wfRunHooks( 'BeforeParserFetchFileAndTitle',
280 array( $this->mParser, $nt, &$options, &$descQuery ) );
281 # Fetch and register the file (file title may be different via hooks)
282 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
283 } else {
284 $img = wfFindFile( $nt );
285 }
286 } else {
287 $img = false;
288 }
289
290 if ( !$img ) {
291 # We're dealing with a non-image, spit out the name and be done with it.
292 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">'
293 . htmlspecialchars( $nt->getText() ) . '</div>';
294
295 if ( $this->mParser instanceof Parser ) {
296 $this->mParser->addTrackingCategory( 'broken-file-category' );
297 }
298 } elseif ( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
299 # The image is blacklisted, just show it as a text link.
300 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' .
301 Linker::link(
302 $nt,
303 htmlspecialchars( $nt->getText() ),
304 array(),
305 array(),
306 array( 'known', 'noclasses' )
307 ) .
308 '</div>';
309 } elseif ( !( $thumb = $img->transform( $transformOptions ) ) ) {
310 # Error generating thumbnail.
311 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">'
312 . htmlspecialchars( $img->getLastError() ) . '</div>';
313 } else {
314 $vpad = ( self::THUMB_PADDING + $this->mHeights - $thumb->height ) / 2;
315
316 $imageParameters = array(
317 'desc-link' => true,
318 'desc-query' => $descQuery,
319 'alt' => $alt,
320 'custom-url-link' => $link
321 );
322 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
323 if ( $alt == '' && $text == '' ) {
324 $imageParameters['alt'] = $nt->getText();
325 }
326
327 # Set both fixed width and min-height.
328 $thumbhtml = "\n\t\t\t" .
329 '<div class="thumb" style="width: ' . ( $this->mWidths + self::THUMB_PADDING ) . 'px;">'
330 # Auto-margin centering for block-level elements. Needed now that we have video
331 # handlers since they may emit block-level elements as opposed to simple <img> tags.
332 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
333 . '<div style="margin:' . $vpad . 'px auto;">'
334 . $thumb->toHtml( $imageParameters ) . '</div></div>';
335
336 // Call parser transform hook
337 if ( $this->mParser && $img->getHandler() ) {
338 $img->getHandler()->parserTransformHook( $this->mParser, $img );
339 }
340 }
341
342 //TODO
343 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
344 // $ul = Linker::link( $linkTarget, $ut );
345
346 if ( $this->mShowBytes ) {
347 if ( $img ) {
348 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
349 } else {
350 $fileSize = wfMessage( 'filemissing' )->escaped();
351 }
352 $fileSize = "$fileSize<br />\n";
353 } else {
354 $fileSize = '';
355 }
356
357 $textlink = $this->mShowFilename ?
358 Linker::link(
359 $nt,
360 htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) ),
361 array(),
362 array(),
363 array( 'known', 'noclasses' )
364 ) . "<br />\n" :
365 '';
366
367 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
368 # in version 4.8.6 generated crackpot html in its absence, see:
369 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
370
371 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
372 # Can be safely removed if FF2 falls completely out of existence
373 $output .=
374 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
375 . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
376 . $thumbhtml
377 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
378 . $textlink . $text . $fileSize
379 . "\n\t\t\t</div>"
380 . "\n\t\t</div></li>";
381 }
382 $output .= "\n</ul>";
383
384 return $output;
385 }
386
387 /**
388 * @return Integer: number of images in the gallery
389 */
390 public function count() {
391 return count( $this->mImages );
392 }
393
394 /**
395 * Set the contextual title
396 *
397 * @param $title Title: contextual title
398 */
399 public function setContextTitle( $title ) {
400 $this->contextTitle = $title;
401 }
402
403 /**
404 * Get the contextual title, if applicable
405 *
406 * @return mixed Title or false
407 */
408 public function getContextTitle() {
409 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
410 ? $this->contextTitle
411 : false;
412 }
413
414 /**
415 * Determines the correct language to be used for this image gallery
416 * @return Language object
417 */
418 private function getLang() {
419 global $wgLang;
420 return $this->mParser
421 ? $this->mParser->getTargetLanguage()
422 : $wgLang;
423 }
424
425 } //class