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