Per Nikerabbit, follow-up to r77972: use a string instead of boolean for readability
[mediawiki.git] / includes / ImageGallery.php
blobb12648c2aa91d087ccbd4f6aafa13dca62ed696a
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
5 /**
6 * Image gallery
8 * Add images to the gallery using add(), then render that list to HTML using toHTML().
10 * @ingroup Media
12 class ImageGallery
14 var $mImages, $mShowBytes, $mShowFilename;
15 var $mCaption = false;
16 var $mSkin = false;
17 var $mRevisionId = 0;
19 /**
20 * Hide blacklisted images?
22 var $mHideBadImages;
24 /**
25 * Registered parser object for output callbacks
27 var $mParser;
29 /**
30 * Contextual title, used when images are being screened
31 * against the bad image list
33 private $contextTitle = false;
35 private $mAttribs = array();
37 /**
38 * Create a new image gallery object.
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'];
53 /**
54 * Register a parser object
56 function setParser( $parser ) {
57 $this->mParser = $parser;
60 /**
61 * Set bad image flag
63 function setHideBadImages( $flag = true ) {
64 $this->mHideBadImages = $flag;
67 /**
68 * Set the caption (as plain text)
70 * @param $caption Caption
72 function setCaption( $caption ) {
73 $this->mCaption = htmlspecialchars( $caption );
76 /**
77 * Set the caption (as HTML)
79 * @param $caption String: Caption
81 public function setCaptionHtml( $caption ) {
82 $this->mCaption = $caption;
85 /**
86 * Set how many images will be displayed per row.
88 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
89 * invalid numbers will be rejected
91 public function setPerRow( $num ) {
92 if ($num >= 0) {
93 $this->mPerRow = (int)$num;
97 /**
98 * Set how wide each image will be, in pixels.
100 * @param $num Integer > 0; invalid numbers will be ignored
102 public function setWidths( $num ) {
103 if ($num > 0) {
104 $this->mWidths = (int)$num;
109 * Set how high each image will be, in pixels.
111 * @param $num Integer > 0; invalid numbers will be ignored
113 public function setHeights( $num ) {
114 if ($num > 0) {
115 $this->mHeights = (int)$num;
120 * Instruct the class to use a specific skin for rendering
122 * @param $skin Skin object
124 function useSkin( $skin ) {
125 $this->mSkin = $skin;
129 * Return the skin that should be used
131 * @return Skin object
133 function getSkin() {
134 if( !$this->mSkin ) {
135 global $wgUser;
136 $skin = $wgUser->getSkin();
137 } else {
138 $skin = $this->mSkin;
140 return $skin;
144 * Add an image to the gallery.
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.
149 function add( $title, $html='' ) {
150 if ( $title instanceof File ) {
151 // Old calling convention
152 $title = $title->getTitle();
154 $this->mImages[] = array( $title, $html );
155 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
159 * Add an image at the beginning of the gallery.
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.
164 function insert( $title, $html='' ) {
165 if ( $title instanceof File ) {
166 // Old calling convention
167 $title = $title->getTitle();
169 array_unshift( $this->mImages, array( &$title, $html ) );
174 * isEmpty() returns true if the gallery contains no images
176 function isEmpty() {
177 return empty( $this->mImages );
181 * Enable/Disable showing of the file size of an image in the gallery.
182 * Enabled by default.
184 * @param $f Boolean: set to false to disable.
186 function setShowBytes( $f ) {
187 $this->mShowBytes = (bool)$f;
191 * Enable/Disable showing of the filename of an image in the gallery.
192 * Enabled by default.
194 * @param $f Boolean: set to false to disable.
196 function setShowFilename( $f ) {
197 $this->mShowFilename = (bool)$f;
201 * Set arbitrary attributes to go on the HTML gallery output element.
202 * Should be suitable for a <ul> element.
204 * Note -- if taking from user input, you should probably run through
205 * Sanitizer::validateAttributes() first.
207 * @param $attribs Array of HTML attribute pairs
209 function setAttributes( $attribs ) {
210 $this->mAttribs = $attribs;
214 * Return a HTML representation of the image gallery
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
223 function toHTML() {
224 global $wgLang;
226 $sk = $this->getSkin();
228 if ( $this->mPerRow > 0 ) {
229 $maxwidth = $this->mPerRow * ( $this->mWidths + 50 );
230 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;";
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>";
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
248 # Give extensions a chance to select the file revision for us
249 $time = $descQuery = false;
250 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
252 if ( $nt->getNamespace() == NS_FILE ) {
253 $img = wfFindFile( $nt, array( 'time' => $time ) );
254 } else {
255 $img = false;
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' )
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);
280 $imageParameters = array(
281 'desc-link' => true,
282 'desc-query' => $descQuery
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();
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>';
299 // Call parser transform hook
300 if ( $this->mParser && $img->getHandler() ) {
301 $img->getHandler()->parserTransformHook( $this->mParser, $img );
305 //TODO
306 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
307 // $ul = $sk->link( $linkTarget, $ut );
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' );
316 $nb = "$nb<br />\n";
317 } else {
318 $nb = '';
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 '' ;
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
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;
347 $s .= "\n</ul>";
349 return $s;
353 * @return Integer: number of images in the gallery
355 public function count() {
356 return count( $this->mImages );
360 * Set the contextual title
362 * @param $title Title: contextual title
364 public function setContextTitle( $title ) {
365 $this->contextTitle = $title;
369 * Get the contextual title, if applicable
371 * @return mixed Title or false
373 public function getContextTitle() {
374 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
375 ? $this->contextTitle
376 : false;
379 } //class