2 if ( ! defined( 'MEDIAWIKI' ) )
11 * Add images to the gallery using add(), then render that list to HTML using toHTML().
16 var $mImages, $mShowBytes, $mShowFilename;
17 var $mCaption = false;
21 * Is the gallery on a wiki page (i.e. not a special page)
26 * Create a new image gallery object.
28 function __construct( ) {
29 $this->mImages
= array();
30 $this->mShowBytes
= true;
31 $this->mShowFilename
= true;
32 $this->mParsing
= false;
36 * Set the "parse" bit so we know to hide "bad" images
38 function setParsing( $val = true ) {
39 $this->mParsing
= $val;
43 * Set the caption (as plain text)
45 * @param $caption Caption
47 function setCaption( $caption ) {
48 $this->mCaption
= htmlspecialchars( $caption );
52 * Set the caption (as HTML)
54 * @param $caption Caption
56 function setCaptionHtml( $caption ) {
57 $this->mCaption
= $caption;
61 * Instruct the class to use a specific skin for rendering
63 * @param $skin Skin object
65 function useSkin( $skin ) {
66 $this->mSkin
=& $skin;
70 * Return the skin that should be used
77 $skin =& $wgUser->getSkin();
79 $skin =& $this->mSkin
;
85 * Add an image to the gallery.
87 * @param $image Image object that is added to the gallery
88 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
90 function add( $image, $html='' ) {
91 $this->mImages
[] = array( &$image, $html );
92 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
96 * Add an image at the beginning of the gallery.
98 * @param $image Image object that is added to the gallery
99 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
101 function insert( $image, $html='' ) {
102 array_unshift( $this->mImages
, array( &$image, $html ) );
107 * isEmpty() returns true if the gallery contains no images
110 return empty( $this->mImages
);
114 * Enable/Disable showing of the file size of an image in the gallery.
115 * Enabled by default.
117 * @param $f Boolean: set to false to disable.
119 function setShowBytes( $f ) {
120 $this->mShowBytes
= ( $f == true);
124 * Enable/Disable showing of the filename of an image in the gallery.
125 * Enabled by default.
127 * @param $f Boolean: set to false to disable.
129 function setShowFilename( $f ) {
130 $this->mShowFilename
= ( $f == true);
134 * Return a HTML representation of the image gallery
136 * For each image in the gallery, display
139 * - the additional text provided when adding the image
140 * - the size of the image
144 global $wgLang, $wgGenerateThumbnailOnParse;
146 $sk = $this->getSkin();
148 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
149 if( $this->mCaption
)
150 $s .= '<td class="galleryheader" colspan="4"><big>' . $this->mCaption
. '</big></td>';
153 foreach ( $this->mImages
as $pair ) {
157 $nt = $img->getTitle();
159 if( $nt->getNamespace() != NS_IMAGE
) {
160 # We're dealing with a non-image, spit out the name and be done with it.
161 $thumbhtml = '<div style="height: 152px;">' . htmlspecialchars( $nt->getText() ) . '</div>';
163 else if( $this->mParsing
&& wfIsBadImage( $nt->getDBkey() ) ) {
164 # The image is blacklisted, just show it as a text link.
165 $thumbhtml = '<div style="height: 152px;">'
166 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
167 } else if( !( $thumb = $img->getThumbnail( 120, 120, $wgGenerateThumbnailOnParse ) ) ) {
168 # Error generating thumbnail.
169 $thumbhtml = '<div style="height: 152px;">'
170 . htmlspecialchars( $img->getLastError() ) . '</div>';
173 $vpad = floor( ( 150 - $thumb->height
) /2 ) - 2;
174 $thumbhtml = '<div class="thumb" style="padding: ' . $vpad . 'px 0;">'
175 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
179 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
181 if( $this->mShowBytes
) {
182 if( $img->exists() ) {
183 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
184 $wgLang->formatNum( $img->getSize() ) );
186 $nb = wfMsgHtml( 'filemissing' );
193 $textlink = $this->mShowFilename ?
194 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
197 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
198 # in version 4.8.6 generated crackpot html in its absence, see:
199 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
201 $s .= ($i%4
==0) ?
'<tr>' : '';
202 $s .= '<td><div class="gallerybox">' . $thumbhtml
203 . '<div class="gallerytext">' . "\n" . $textlink . $text . $nb
204 . "</div></div></td>\n";
205 $s .= ($i%4
==3) ?
'</tr>' : '';
217 * @return int Number of images in the gallery
219 public function count() {
220 return count( $this->mImages
);