Don't look for pipes in the root node.
[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 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;";
231 }
232
233 $attribs = Sanitizer::mergeAttributes(
234 array(
235 'class' => 'gallery'),
236 $this->mAttribs );
237 $s = Xml::openElement( 'ul', $attribs );
238 if ( $this->mCaption ) {
239 $s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
240 }
241
242 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
243 $i = 0;
244 foreach ( $this->mImages as $pair ) {
245 $nt = $pair[0];
246 $text = $pair[1]; # "text" means "caption" here
247
248 # Give extensions a chance to select the file revision for us
249 $time = $descQuery = false;
250 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
251
252 if ( $nt->getNamespace() == NS_FILE ) {
253 $img = wfFindFile( $nt, array( 'time' => $time ) );
254 } else {
255 $img = false;
256 }
257
258 if( !$img ) {
259 # We're dealing with a non-image, spit out the name and be done with it.
260 $thumbhtml = "\n\t\t\t".'<div style="height: '.(30 + $this->mHeights).'px;">'
261 . htmlspecialchars( $nt->getText() ) . '</div>';
262 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
263 # The image is blacklisted, just show it as a text link.
264 $thumbhtml = "\n\t\t\t".'<div style="height: '.(30 + $this->mHeights).'px;">' .
265 $sk->link(
266 $nt,
267 htmlspecialchars( $nt->getText() ),
268 array(),
269 array(),
270 array( 'known', 'noclasses' )
271 ) .
272 '</div>';
273 } elseif( !( $thumb = $img->transform( $params ) ) ) {
274 # Error generating thumbnail.
275 $thumbhtml = "\n\t\t\t".'<div style="height: '.(30 + $this->mHeights).'px;">'
276 . htmlspecialchars( $img->getLastError() ) . '</div>';
277 } else {
278 $vpad = floor(( 30 + $this->mHeights - $thumb->height ) /2);
279
280 $imageParameters = array(
281 'desc-link' => true,
282 'desc-query' => $descQuery
283 );
284 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
285 if ( $text == '' ) {
286 $imageParameters['alt'] = $nt->getText();
287 }
288
289 # Set both fixed width and height. Otherwise we might have problems
290 # with the vertical centering of images where height<line-size
291 $thumbhtml = "\n\t\t\t".
292 '<div class="thumb" style="width: ' .($this->mWidths+30).'px; height: ' .($this->mHeights+30).'px;">'
293 # Auto-margin centering for block-level elements. Needed now that we have video
294 # handlers since they may emit block-level elements as opposed to simple <img> tags.
295 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
296 . '<div style="margin:'.$vpad.'px auto;">'
297 . $thumb->toHtml( $imageParameters ) . '</div></div>';
298
299 // Call parser transform hook
300 if ( $this->mParser && $img->getHandler() ) {
301 $img->getHandler()->parserTransformHook( $this->mParser, $img );
302 }
303 }
304
305 //TODO
306 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
307 // $ul = $sk->link( $linkTarget, $ut );
308
309 if( $this->mShowBytes ) {
310 if( $img ) {
311 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
312 $wgLang->formatNum( $img->getSize() ) );
313 } else {
314 $nb = wfMsgHtml( 'filemissing' );
315 }
316 $nb = "$nb<br />\n";
317 } else {
318 $nb = '';
319 }
320
321 $textlink = $this->mShowFilename ?
322 $sk->link(
323 $nt,
324 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength ) ),
325 array(),
326 array(),
327 array( 'known', 'noclasses' )
328 ) . "<br />\n" :
329 '' ;
330
331 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
332 # in version 4.8.6 generated crackpot html in its absence, see:
333 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
334
335 # Weird double wrapping in div needed due to FF2 bug
336 # Can be safely removed if FF2 falls completely out of existance
337 $s .=
338 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + 35 ) . 'px">'
339 . '<div style="width: ' . ( $this->mWidths + 35 ) . 'px">'
340 . $thumbhtml
341 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
342 . $textlink . $text . $nb
343 . "\n\t\t\t</div>"
344 . "\n\t\t</div></li>";
345 ++$i;
346 }
347 $s .= "\n</ul>";
348
349 return $s;
350 }
351
352 /**
353 * @return Integer: number of images in the gallery
354 */
355 public function count() {
356 return count( $this->mImages );
357 }
358
359 /**
360 * Set the contextual title
361 *
362 * @param $title Title: contextual title
363 */
364 public function setContextTitle( $title ) {
365 $this->contextTitle = $title;
366 }
367
368 /**
369 * Get the contextual title, if applicable
370 *
371 * @return mixed Title or false
372 */
373 public function getContextTitle() {
374 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
375 ? $this->contextTitle
376 : false;
377 }
378
379 } //class