2 if ( ! defined( 'MEDIAWIKI' ) )
12 * Add images to the gallery using add(), then render that list to HTML using toHTML().
18 var $mImages, $mShowBytes, $mShowFilename;
19 var $mCaption = false;
23 * Is the gallery on a wiki page (i.e. not a special page)
28 * Create a new image gallery object.
30 function ImageGallery( ) {
31 $this->mImages
= array();
32 $this->mShowBytes
= true;
33 $this->mShowFilename
= true;
34 $this->mParsing
= false;
38 * Set the "parse" bit so we know to hide "bad" images
40 function setParsing( $val = true ) {
41 $this->mParsing
= $val;
45 * Set the caption (as plain text)
47 * @param $caption Caption
49 function setCaption( $caption ) {
50 $this->mCaption
= htmlspecialchars( $caption );
54 * Set the caption (as HTML)
56 * @param $caption Caption
58 function setCaptionHtml( $caption ) {
59 $this->mCaption
= $caption;
63 * Instruct the class to use a specific skin for rendering
65 * @param $skin Skin object
67 function useSkin( $skin ) {
68 $this->mSkin
=& $skin;
72 * Return the skin that should be used
79 $skin =& $wgUser->getSkin();
81 $skin =& $this->mSkin
;
87 * Add an image to the gallery.
89 * @param $image Image object that is added to the gallery
90 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
92 function add( $image, $html='' ) {
93 $this->mImages
[] = array( &$image, $html );
94 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
98 * Add an image at the beginning of the gallery.
100 * @param $image Image object that is added to the gallery
101 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
103 function insert( $image, $html='' ) {
104 array_unshift( $this->mImages
, array( &$image, $html ) );
109 * isEmpty() returns true if the gallery contains no images
112 return empty( $this->mImages
);
116 * Enable/Disable showing of the file size of an image in the gallery.
117 * Enabled by default.
119 * @param $f Boolean: set to false to disable.
121 function setShowBytes( $f ) {
122 $this->mShowBytes
= ( $f == true);
126 * Enable/Disable showing of the filename of an image in the gallery.
127 * Enabled by default.
129 * @param $f Boolean: set to false to disable.
131 function setShowFilename( $f ) {
132 $this->mShowFilename
= ( $f == true);
136 * Return a HTML representation of the image gallery
138 * For each image in the gallery, display
141 * - the additional text provided when adding the image
142 * - the size of the image
146 global $wgLang, $wgGenerateThumbnailOnParse;
148 $sk = $this->getSkin();
150 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
151 if( $this->mCaption
)
152 $s .= '<td class="galleryheader" colspan="4"><big>' . $this->mCaption
. '</big></td>';
155 foreach ( $this->mImages
as $pair ) {
159 $nt = $img->getTitle();
161 if( $nt->getNamespace() != NS_IMAGE
) {
162 # We're dealing with a non-image, spit out the name and be done with it.
163 $thumbhtml = '<div style="height: 152px;">' . htmlspecialchars( $nt->getText() ) . '</div>';
165 else if( $this->mParsing
&& wfIsBadImage( $nt->getDBkey() ) ) {
166 # The image is blacklisted, just show it as a text link.
167 $thumbhtml = '<div style="height: 152px;">'
168 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
169 } else if( !( $thumb = $img->getThumbnail( 120, 120, $wgGenerateThumbnailOnParse ) ) ) {
170 # Error generating thumbnail.
171 $thumbhtml = '<div style="height: 152px;">'
172 . htmlspecialchars( $img->getLastError() ) . '</div>';
175 $vpad = floor( ( 150 - $thumb->height
) /2 ) - 2;
176 $thumbhtml = '<div class="thumb" style="padding: ' . $vpad . 'px 0;">'
177 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
181 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
183 if( $this->mShowBytes
) {
184 if( $img->exists() ) {
185 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
186 $wgLang->formatNum( $img->getSize() ) );
188 $nb = wfMsgHtml( 'filemissing' );
195 $textlink = $this->mShowFilename ?
196 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
199 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
200 # in version 4.8.6 generated crackpot html in its absence, see:
201 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
203 $s .= ($i%4
==0) ?
'<tr>' : '';
204 $s .= '<td><div class="gallerybox">' . $thumbhtml
205 . '<div class="gallerytext">' . "\n" . $textlink . $text . $nb
206 . "</div></div></td>\n";
207 $s .= ($i%4
==3) ?
'</tr>' : '';
219 * @return int Number of images in the gallery
221 public function count() {
222 return count( $this->mImages
);