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