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