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