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