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 * Contextual title, used when images are being screened
27 * against the bad image list
29 private $contextTitle = false;
32 * Create a new image gallery object.
34 function __construct( ) {
35 $this->mImages
= array();
36 $this->mShowBytes
= true;
37 $this->mShowFilename
= true;
38 $this->mParsing
= false;
42 * Set the "parse" bit so we know to hide "bad" images
44 function setParsing( $val = true ) {
45 $this->mParsing
= $val;
49 * Set the caption (as plain text)
51 * @param $caption Caption
53 function setCaption( $caption ) {
54 $this->mCaption
= htmlspecialchars( $caption );
58 * Set the caption (as HTML)
60 * @param $caption Caption
62 function setCaptionHtml( $caption ) {
63 $this->mCaption
= $caption;
67 * Instruct the class to use a specific skin for rendering
69 * @param $skin Skin object
71 function useSkin( $skin ) {
72 $this->mSkin
=& $skin;
76 * Return the skin that should be used
83 $skin =& $wgUser->getSkin();
85 $skin =& $this->mSkin
;
91 * Add an image to the gallery.
93 * @param $image Image object that is added to the gallery
94 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
96 function add( $image, $html='' ) {
97 $this->mImages
[] = array( &$image, $html );
98 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
102 * Add an image at the beginning of the gallery.
104 * @param $image Image object that is added to the gallery
105 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
107 function insert( $image, $html='' ) {
108 array_unshift( $this->mImages
, array( &$image, $html ) );
113 * isEmpty() returns true if the gallery contains no images
116 return empty( $this->mImages
);
120 * Enable/Disable showing of the file size of an image in the gallery.
121 * Enabled by default.
123 * @param $f Boolean: set to false to disable.
125 function setShowBytes( $f ) {
126 $this->mShowBytes
= ( $f == true);
130 * Enable/Disable showing of the filename of an image in the gallery.
131 * Enabled by default.
133 * @param $f Boolean: set to false to disable.
135 function setShowFilename( $f ) {
136 $this->mShowFilename
= ( $f == true);
140 * Return a HTML representation of the image gallery
142 * For each image in the gallery, display
145 * - the additional text provided when adding the image
146 * - the size of the image
150 global $wgLang, $wgGenerateThumbnailOnParse;
152 $sk = $this->getSkin();
154 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
155 if( $this->mCaption
)
156 $s .= '<td class="galleryheader" colspan="4"><big>' . $this->mCaption
. '</big></td>';
159 foreach ( $this->mImages
as $pair ) {
163 $nt = $img->getTitle();
165 if( $nt->getNamespace() != NS_IMAGE
) {
166 # We're dealing with a non-image, spit out the name and be done with it.
167 $thumbhtml = '<div style="height: 152px;">' . htmlspecialchars( $nt->getText() ) . '</div>';
168 } elseif( $this->mParsing
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
169 # The image is blacklisted, just show it as a text link.
170 $thumbhtml = '<div style="height: 152px;">'
171 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
172 } else if( !( $thumb = $img->getThumbnail( 120, 120, $wgGenerateThumbnailOnParse ) ) ) {
173 # Error generating thumbnail.
174 $thumbhtml = '<div style="height: 152px;">'
175 . htmlspecialchars( $img->getLastError() ) . '</div>';
177 $vpad = floor( ( 150 - $thumb->height
) /2 ) - 2;
178 $thumbhtml = '<div class="thumb" style="padding: ' . $vpad . 'px 0;">'
179 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
183 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
185 if( $this->mShowBytes
) {
186 if( $img->exists() ) {
187 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
188 $wgLang->formatNum( $img->getSize() ) );
190 $nb = wfMsgHtml( 'filemissing' );
197 $textlink = $this->mShowFilename ?
198 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
201 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
202 # in version 4.8.6 generated crackpot html in its absence, see:
203 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
205 $s .= ($i%4
==0) ?
'<tr>' : '';
206 $s .= '<td><div class="gallerybox">' . $thumbhtml
207 . '<div class="gallerytext">' . "\n" . $textlink . $text . $nb
208 . "</div></div></td>\n";
209 $s .= ($i%4
==3) ?
'</tr>' : '';
221 * @return int Number of images in the gallery
223 public function count() {
224 return count( $this->mImages
);
228 * Set the contextual title
230 * @param Title $title Contextual title
232 public function setContextTitle( $title ) {
233 $this->contextTitle
= $title;
237 * Get the contextual title, if applicable
239 * @return mixed Title or false
241 public function getContextTitle() {
242 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
243 ?
$this->contextTitle