* Drop 5.1 compat code
[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 var $mSkin = false;
17
18 /**
19 * Hide blacklisted images?
20 */
21 var $mHideBadImages;
22
23 /**
24 * Registered parser object for output callbacks
25 * @var Parser
26 */
27 var $mParser;
28
29 /**
30 * Contextual title, used when images are being screened
31 * against the bad image list
32 */
33 private $contextTitle = false;
34
35 private $mAttribs = array();
36
37 /**
38 * Fixed margins
39 */
40 const THUMB_PADDING = 30;
41 const GB_PADDING = 5;
42 const GB_BORDERS = 6;
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 */
131 function useSkin( $skin ) {
132 $this->mSkin = $skin;
133 }
134
135 /**
136 * Return the skin that should be used
137 *
138 * @return Skin object
139 */
140 function getSkin() {
141 if( !$this->mSkin ) {
142 global $wgUser;
143 $skin = $wgUser->getSkin();
144 } else {
145 $skin = $this->mSkin;
146 }
147 return $skin;
148 }
149
150 /**
151 * Add an image to the gallery.
152 *
153 * @param $title Title object of the image that is added to the gallery
154 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
155 */
156 function add( $title, $html='' ) {
157 if ( $title instanceof File ) {
158 // Old calling convention
159 $title = $title->getTitle();
160 }
161 $this->mImages[] = array( $title, $html );
162 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
163 }
164
165 /**
166 * Add an image at the beginning of the gallery.
167 *
168 * @param $title Title object of the image that is added to the gallery
169 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
170 */
171 function insert( $title, $html='' ) {
172 if ( $title instanceof File ) {
173 // Old calling convention
174 $title = $title->getTitle();
175 }
176 array_unshift( $this->mImages, array( &$title, $html ) );
177 }
178
179
180 /**
181 * isEmpty() returns true if the gallery contains no images
182 */
183 function isEmpty() {
184 return empty( $this->mImages );
185 }
186
187 /**
188 * Enable/Disable showing of the file size of an image in the gallery.
189 * Enabled by default.
190 *
191 * @param $f Boolean: set to false to disable.
192 */
193 function setShowBytes( $f ) {
194 $this->mShowBytes = (bool)$f;
195 }
196
197 /**
198 * Enable/Disable showing of the filename of an image in the gallery.
199 * Enabled by default.
200 *
201 * @param $f Boolean: set to false to disable.
202 */
203 function setShowFilename( $f ) {
204 $this->mShowFilename = (bool)$f;
205 }
206
207 /**
208 * Set arbitrary attributes to go on the HTML gallery output element.
209 * Should be suitable for a <ul> element.
210 *
211 * Note -- if taking from user input, you should probably run through
212 * Sanitizer::validateAttributes() first.
213 *
214 * @param $attribs Array of HTML attribute pairs
215 */
216 function setAttributes( $attribs ) {
217 $this->mAttribs = $attribs;
218 }
219
220 /**
221 * Return a HTML representation of the image gallery
222 *
223 * For each image in the gallery, display
224 * - a thumbnail
225 * - the image name
226 * - the additional text provided when adding the image
227 * - the size of the image
228 *
229 */
230 function toHTML() {
231 global $wgLang;
232
233 $sk = $this->getSkin();
234
235 if ( $this->mPerRow > 0 ) {
236 $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS );
237 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : "";
238 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
239 }
240
241 $attribs = Sanitizer::mergeAttributes(
242 array( 'class' => 'gallery' ), $this->mAttribs );
243
244 $s = Xml::openElement( 'ul', $attribs );
245 if ( $this->mCaption ) {
246 $s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
247 }
248
249 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
250 # Output each image...
251 foreach ( $this->mImages as $pair ) {
252 $nt = $pair[0];
253 $text = $pair[1]; # "text" means "caption" here
254
255 $descQuery = false;
256 if ( $nt->getNamespace() == NS_FILE ) {
257 # Get the file...
258 if ( $this->mParser instanceof Parser ) {
259 # Give extensions a chance to select the file revision for us
260 $time = $sha1 = false;
261 wfRunHooks( 'BeforeParserFetchFileAndTitle',
262 array( $this->mParser, &$nt, &$time, &$sha1, &$descQuery ) );
263 # Fetch and register the file (file title may be different via hooks)
264 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $time, $sha1 );
265 } else {
266 $img = wfFindFile( $nt );
267 }
268 } else {
269 $img = false;
270 }
271
272 if( !$img ) {
273 # We're dealing with a non-image, spit out the name and be done with it.
274 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">'
275 . htmlspecialchars( $nt->getText() ) . '</div>';
276 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
277 # The image is blacklisted, just show it as a text link.
278 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">' .
279 $sk->link(
280 $nt,
281 htmlspecialchars( $nt->getText() ),
282 array(),
283 array(),
284 array( 'known', 'noclasses' )
285 ) .
286 '</div>';
287 } elseif( !( $thumb = $img->transform( $params ) ) ) {
288 # Error generating thumbnail.
289 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">'
290 . htmlspecialchars( $img->getLastError() ) . '</div>';
291 } else {
292 //We get layout problems with the margin, if the image is smaller
293 //than the line-height, so we less margin in these cases.
294 $minThumbHeight = $thumb->height > 17 ? $thumb->height : 17;
295 $vpad = floor(( self::THUMB_PADDING + $this->mHeights - $minThumbHeight ) /2);
296
297
298 $imageParameters = array(
299 'desc-link' => true,
300 'desc-query' => $descQuery
301 );
302 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
303 if ( $text == '' ) {
304 $imageParameters['alt'] = $nt->getText();
305 }
306
307 # Set both fixed width and min-height.
308 $thumbhtml = "\n\t\t\t".
309 '<div class="thumb" style="width: ' .($this->mWidths + self::THUMB_PADDING).'px;">'
310 # Auto-margin centering for block-level elements. Needed now that we have video
311 # handlers since they may emit block-level elements as opposed to simple <img> tags.
312 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
313 . '<div style="margin:'.$vpad.'px auto;">'
314 . $thumb->toHtml( $imageParameters ) . '</div></div>';
315
316 // Call parser transform hook
317 if ( $this->mParser && $img->getHandler() ) {
318 $img->getHandler()->parserTransformHook( $this->mParser, $img );
319 }
320 }
321
322 //TODO
323 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
324 // $ul = $sk->link( $linkTarget, $ut );
325
326 if( $this->mShowBytes ) {
327 if( $img ) {
328 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
329 $wgLang->formatNum( $img->getSize() ) );
330 } else {
331 $nb = wfMsgHtml( 'filemissing' );
332 }
333 $nb = "$nb<br />\n";
334 } else {
335 $nb = '';
336 }
337
338 $textlink = $this->mShowFilename ?
339 $sk->link(
340 $nt,
341 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength ) ),
342 array(),
343 array(),
344 array( 'known', 'noclasses' )
345 ) . "<br />\n" :
346 '' ;
347
348 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
349 # in version 4.8.6 generated crackpot html in its absence, see:
350 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
351
352 # Weird double wrapping in div needed due to FF2 bug
353 # Can be safely removed if FF2 falls completely out of existance
354 $s .=
355 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
356 . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
357 . $thumbhtml
358 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
359 . $textlink . $text . $nb
360 . "\n\t\t\t</div>"
361 . "\n\t\t</div></li>";
362 }
363 $s .= "\n</ul>";
364
365 return $s;
366 }
367
368 /**
369 * @return Integer: number of images in the gallery
370 */
371 public function count() {
372 return count( $this->mImages );
373 }
374
375 /**
376 * Set the contextual title
377 *
378 * @param $title Title: contextual title
379 */
380 public function setContextTitle( $title ) {
381 $this->contextTitle = $title;
382 }
383
384 /**
385 * Get the contextual title, if applicable
386 *
387 * @return mixed Title or false
388 */
389 public function getContextTitle() {
390 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
391 ? $this->contextTitle
392 : false;
393 }
394
395 } //class