* (bug 19944) Link on image thumbnails no longer link to "Media:" namespace in some...
[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 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; invalid numbers will be rejected
89 */
90 public function setPerRow( $num ) {
91 if ($num > 0) {
92 $this->mPerRow = (int)$num;
93 }
94 }
95
96 /**
97 * Set how wide each image will be, in pixels.
98 *
99 * @param $num Integer > 0; invalid numbers will be ignored
100 */
101 public function setWidths( $num ) {
102 if ($num > 0) {
103 $this->mWidths = (int)$num;
104 }
105 }
106
107 /**
108 * Set how high each image will be, in pixels.
109 *
110 * @param $num Integer > 0; invalid numbers will be ignored
111 */
112 public function setHeights( $num ) {
113 if ($num > 0) {
114 $this->mHeights = (int)$num;
115 }
116 }
117
118 /**
119 * Instruct the class to use a specific skin for rendering
120 *
121 * @param $skin Skin object
122 */
123 function useSkin( $skin ) {
124 $this->mSkin = $skin;
125 }
126
127 /**
128 * Return the skin that should be used
129 *
130 * @return Skin object
131 */
132 function getSkin() {
133 if( !$this->mSkin ) {
134 global $wgUser;
135 $skin = $wgUser->getSkin();
136 } else {
137 $skin = $this->mSkin;
138 }
139 return $skin;
140 }
141
142 /**
143 * Add an image to the gallery.
144 *
145 * @param $title Title object of the image that is added to the gallery
146 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
147 */
148 function add( $title, $html='' ) {
149 if ( $title instanceof File ) {
150 // Old calling convention
151 $title = $title->getTitle();
152 }
153 $this->mImages[] = array( $title, $html );
154 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
155 }
156
157 /**
158 * Add an image at the beginning of the gallery.
159 *
160 * @param $title Title object of the image that is added to the gallery
161 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
162 */
163 function insert( $title, $html='' ) {
164 if ( $title instanceof File ) {
165 // Old calling convention
166 $title = $title->getTitle();
167 }
168 array_unshift( $this->mImages, array( &$title, $html ) );
169 }
170
171
172 /**
173 * isEmpty() returns true if the gallery contains no images
174 */
175 function isEmpty() {
176 return empty( $this->mImages );
177 }
178
179 /**
180 * Enable/Disable showing of the file size of an image in the gallery.
181 * Enabled by default.
182 *
183 * @param $f Boolean: set to false to disable.
184 */
185 function setShowBytes( $f ) {
186 $this->mShowBytes = (bool)$f;
187 }
188
189 /**
190 * Enable/Disable showing of the filename of an image in the gallery.
191 * Enabled by default.
192 *
193 * @param $f Boolean: set to false to disable.
194 */
195 function setShowFilename( $f ) {
196 $this->mShowFilename = (bool)$f;
197 }
198
199 /**
200 * Set arbitrary attributes to go on the HTML gallery output element.
201 * Should be suitable for a &lt;table&gt; element.
202 *
203 * Note -- if taking from user input, you should probably run through
204 * Sanitizer::validateAttributes() first.
205 *
206 * @param $attribs Array of HTML attribute pairs
207 */
208 function setAttributes( $attribs ) {
209 $this->mAttribs = $attribs;
210 }
211
212 /**
213 * Return a HTML representation of the image gallery
214 *
215 * For each image in the gallery, display
216 * - a thumbnail
217 * - the image name
218 * - the additional text provided when adding the image
219 * - the size of the image
220 *
221 */
222 function toHTML() {
223 global $wgLang;
224
225 $sk = $this->getSkin();
226
227 $attribs = Sanitizer::mergeAttributes(
228 array(
229 'class' => 'gallery',
230 'cellspacing' => '0',
231 'cellpadding' => '0' ),
232 $this->mAttribs );
233 $s = Xml::openElement( 'table', $attribs );
234 if( $this->mCaption )
235 $s .= "\n\t<caption>{$this->mCaption}</caption>";
236
237 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
238 $i = 0;
239 foreach ( $this->mImages as $pair ) {
240 $nt = $pair[0];
241 $text = $pair[1]; # "text" means "caption" here
242
243 # Give extensions a chance to select the file revision for us
244 $time = $descQuery = false;
245 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
246
247 if ( $nt->getNamespace() == NS_FILE ) {
248 $img = wfFindFile( $nt, array( 'time' => $time ) );
249 } else {
250 $img = false;
251 }
252
253 if( !$img ) {
254 # We're dealing with a non-image, spit out the name and be done with it.
255 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
256 . htmlspecialchars( $nt->getText() ) . '</div>';
257 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
258 # The image is blacklisted, just show it as a text link.
259 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">' .
260 $sk->link(
261 $nt,
262 htmlspecialchars( $nt->getText() ),
263 array(),
264 array(),
265 array( 'known', 'noclasses' )
266 ) .
267 '</div>';
268 } elseif( !( $thumb = $img->transform( $params ) ) ) {
269 # Error generating thumbnail.
270 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
271 . htmlspecialchars( $img->getLastError() ) . '</div>';
272 } else {
273 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
274
275 $imageParameters = array(
276 'desc-link' => true,
277 'desc-query' => $descQuery
278 );
279 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
280 if ( $text == '' ) {
281 $imageParameters['alt'] = $nt->getText();
282 }
283
284 $thumbhtml = "\n\t\t\t".
285 '<div class="thumb" style="padding: ' . $vpad . 'px 0; width: ' .($this->mWidths+30).'px;">'
286 # Auto-margin centering for block-level elements. Needed now that we have video
287 # handlers since they may emit block-level elements as opposed to simple <img> tags.
288 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
289 . '<div style="margin-left: auto; margin-right: auto; width: ' .$this->mWidths.'px;">'
290 . $thumb->toHtml( $imageParameters ) . '</div></div>';
291
292 // Call parser transform hook
293 if ( $this->mParser && $img->getHandler() ) {
294 $img->getHandler()->parserTransformHook( $this->mParser, $img );
295 }
296 }
297
298 //TODO
299 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
300 // $ul = $sk->link( $linkTarget, $ut );
301
302 if( $this->mShowBytes ) {
303 if( $img ) {
304 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
305 $wgLang->formatNum( $img->getSize() ) );
306 } else {
307 $nb = wfMsgHtml( 'filemissing' );
308 }
309 $nb = "$nb<br />\n";
310 } else {
311 $nb = '';
312 }
313
314 $textlink = $this->mShowFilename ?
315 $sk->link(
316 $nt,
317 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength ) ),
318 array(),
319 array(),
320 array( 'known', 'noclasses' )
321 ) . "<br />\n" :
322 '' ;
323
324 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
325 # in version 4.8.6 generated crackpot html in its absence, see:
326 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
327
328 if ( $i % $this->mPerRow == 0 ) {
329 $s .= "\n\t<tr>";
330 }
331 $s .=
332 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths+35).'px;">'
333 . $thumbhtml
334 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
335 . $textlink . $text . $nb
336 . "\n\t\t\t</div>"
337 . "\n\t\t</div></td>";
338 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
339 $s .= "\n\t</tr>";
340 }
341 ++$i;
342 }
343 if( $i % $this->mPerRow != 0 ) {
344 $s .= "\n\t</tr>";
345 }
346 $s .= "\n</table>";
347
348 return $s;
349 }
350
351 /**
352 * @return Integer: number of images in the gallery
353 */
354 public function count() {
355 return count( $this->mImages );
356 }
357
358 /**
359 * Set the contextual title
360 *
361 * @param $title Title: contextual title
362 */
363 public function setContextTitle( $title ) {
364 $this->contextTitle = $title;
365 }
366
367 /**
368 * Get the contextual title, if applicable
369 *
370 * @return mixed Title or false
371 */
372 public function getContextTitle() {
373 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
374 ? $this->contextTitle
375 : false;
376 }
377
378 } //class