Copied some of the CSS added in r22726 into other skins, leaving pure aesthetics...
[mediawiki.git] / includes / ImageGallery.php
blob6045ad682a16207d44f3f3955512ecec4d077dca
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
5 /**
6 */
8 /**
9 * Image gallery
11 * Add images to the gallery using add(), then render that list to HTML using toHTML().
13 * @addtogroup Media
15 class ImageGallery
17 var $mImages, $mShowBytes, $mShowFilename;
18 var $mCaption = false;
19 var $mSkin = false;
20 var $mRevisionId = 0;
22 /**
23 * Is the gallery on a wiki page (i.e. not a special page)
25 var $mParsing;
27 /**
28 * Contextual title, used when images are being screened
29 * against the bad image list
31 private $contextTitle = false;
33 private $mPerRow = 4; // How many images wide should the gallery be?
34 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
36 /**
37 * Create a new image gallery object.
39 function __construct( ) {
40 $this->mImages = array();
41 $this->mShowBytes = true;
42 $this->mShowFilename = true;
43 $this->mParsing = false;
46 /**
47 * Set the "parse" bit so we know to hide "bad" images
49 function setParsing( $val = true ) {
50 $this->mParsing = $val;
53 /**
54 * Set the caption (as plain text)
56 * @param $caption Caption
58 function setCaption( $caption ) {
59 $this->mCaption = htmlspecialchars( $caption );
62 /**
63 * Set the caption (as HTML)
65 * @param $caption Caption
67 public function setCaptionHtml( $caption ) {
68 $this->mCaption = $caption;
71 /**
72 * Set how many images will be displayed per row.
74 * @param int $num > 0; invalid numbers will be rejected
76 public function setPerRow( $num ) {
77 if ($num > 0) {
78 $this->mPerRow = (int)$num;
82 /**
83 * Set how wide each image will be, in pixels.
85 * @param int $num > 0; invalid numbers will be ignored
87 public function setWidths( $num ) {
88 if ($num > 0) {
89 $this->mWidths = (int)$num;
93 /**
94 * Set how high each image will be, in pixels.
96 * @param int $num > 0; invalid numbers will be ignored
98 public function setHeights( $num ) {
99 if ($num > 0) {
100 $this->mHeights = (int)$num;
105 * Instruct the class to use a specific skin for rendering
107 * @param $skin Skin object
109 function useSkin( $skin ) {
110 $this->mSkin = $skin;
114 * Return the skin that should be used
116 * @return Skin object
118 function getSkin() {
119 if( !$this->mSkin ) {
120 global $wgUser;
121 $skin = $wgUser->getSkin();
122 } else {
123 $skin = $this->mSkin;
125 return $skin;
129 * Add an image to the gallery.
131 * @param $title Title object of the image that is added to the gallery
132 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
134 function add( $title, $html='' ) {
135 if ( $title instanceof File ) {
136 // Old calling convention
137 $title = $title->getTitle();
139 $this->mImages[] = array( $title, $html );
140 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
144 * Add an image at the beginning of 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 insert( $title, $html='' ) {
150 array_unshift( $this->mImages, array( &$title, $html ) );
155 * isEmpty() returns true if the gallery contains no images
157 function isEmpty() {
158 return empty( $this->mImages );
162 * Enable/Disable showing of the file size of an image in the gallery.
163 * Enabled by default.
165 * @param $f Boolean: set to false to disable.
167 function setShowBytes( $f ) {
168 $this->mShowBytes = ( $f == true);
172 * Enable/Disable showing of the filename of an image in the gallery.
173 * Enabled by default.
175 * @param $f Boolean: set to false to disable.
177 function setShowFilename( $f ) {
178 $this->mShowFilename = ( $f == true);
182 * Return a HTML representation of the image gallery
184 * For each image in the gallery, display
185 * - a thumbnail
186 * - the image name
187 * - the additional text provided when adding the image
188 * - the size of the image
191 function toHTML() {
192 global $wgLang;
194 $sk = $this->getSkin();
196 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
197 if( $this->mCaption )
198 $s .= "\n\t<caption>{$this->mCaption}</caption>";
200 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
201 $i = 0;
202 foreach ( $this->mImages as $pair ) {
203 $nt = $pair[0];
204 $text = $pair[1];
206 # Give extensions a chance to select the file revision for us
207 $time = false;
208 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time ) );
210 $img = wfFindFile( $nt, $time );
212 if( $nt->getNamespace() != NS_IMAGE || !$img ) {
213 # We're dealing with a non-image, spit out the name and be done with it.
214 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
215 . htmlspecialchars( $nt->getText() ) . '</div>';
216 } elseif( $this->mParsing && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
217 # The image is blacklisted, just show it as a text link.
218 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
219 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
220 } elseif( !( $thumb = $img->transform( $params ) ) ) {
221 # Error generating thumbnail.
222 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
223 . htmlspecialchars( $img->getLastError() ) . '</div>';
224 } else {
225 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
226 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+30).'px;">'
227 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
230 //TODO
231 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
233 if( $this->mShowBytes ) {
234 if( $img ) {
235 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
236 $wgLang->formatNum( $img->getSize() ) );
237 } else {
238 $nb = wfMsgHtml( 'filemissing' );
240 $nb = "$nb<br />\n";
241 } else {
242 $nb = '';
245 $textlink = $this->mShowFilename ?
246 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
247 '' ;
249 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
250 # in version 4.8.6 generated crackpot html in its absence, see:
251 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
253 if ( $i % $this->mPerRow == 0 ) {
254 $s .= "\n\t<tr>";
256 $s .=
257 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths*1.25).'px;">'
258 . $thumbhtml
259 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
260 . $textlink . $text . $nb
261 . "\n\t\t\t</div>"
262 . "\n\t\t</div></td>";
263 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
264 $s .= "\n\t</tr>";
266 ++$i;
268 if( $i % $this->mPerRow != 0 ) {
269 $s .= "\n\t</tr>";
271 $s .= "\n</table>";
273 return $s;
277 * @return int Number of images in the gallery
279 public function count() {
280 return count( $this->mImages );
284 * Set the contextual title
286 * @param Title $title Contextual title
288 public function setContextTitle( $title ) {
289 $this->contextTitle = $title;
293 * Get the contextual title, if applicable
295 * @return mixed Title or false
297 public function getContextTitle() {
298 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
299 ? $this->contextTitle
300 : false;
303 } //class