2 if ( ! defined( 'MEDIAWIKI' ) )
11 * Add images to the gallery using add(), then render that list to HTML using toHTML().
17 var $mImages, $mShowBytes, $mShowFilename;
18 var $mCaption = false;
22 * Is the gallery on a wiki page (i.e. not a special page)
27 * Contextual title, used when images are being screened
28 * against the bad image list
30 private $contextTitle = false;
32 private $mPerRow = 4; // How many images wide should the gallery be?
33 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
36 * Create a new image gallery object.
38 function __construct( ) {
39 $this->mImages
= array();
40 $this->mShowBytes
= true;
41 $this->mShowFilename
= true;
42 $this->mParsing
= false;
46 * Set the "parse" bit so we know to hide "bad" images
48 function setParsing( $val = true ) {
49 $this->mParsing
= $val;
53 * Set the caption (as plain text)
55 * @param $caption Caption
57 function setCaption( $caption ) {
58 $this->mCaption
= htmlspecialchars( $caption );
62 * Set the caption (as HTML)
64 * @param $caption Caption
66 public function setCaptionHtml( $caption ) {
67 $this->mCaption
= $caption;
71 * Set how many images will be displayed per row.
73 * @param int $num > 0; invalid numbers will be rejected
75 public function setPerRow( $num ) {
77 $this->mPerRow
= (int)$num;
82 * Set how wide each image will be, in pixels.
84 * @param int $num > 0; invalid numbers will be ignored
86 public function setWidths( $num ) {
88 $this->mWidths
= (int)$num;
93 * Set how high each image will be, in pixels.
95 * @param int $num > 0; invalid numbers will be ignored
97 public function setHeights( $num ) {
99 $this->mHeights
= (int)$num;
104 * Instruct the class to use a specific skin for rendering
106 * @param $skin Skin object
108 function useSkin( $skin ) {
109 $this->mSkin
= $skin;
113 * Return the skin that should be used
115 * @return Skin object
118 if( !$this->mSkin
) {
120 $skin = $wgUser->getSkin();
122 $skin = $this->mSkin
;
128 * Add an image to the gallery.
130 * @param $image Image object that is added to the gallery
131 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
133 function add( $image, $html='' ) {
134 $this->mImages
[] = array( &$image, $html );
135 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
139 * Add an image at the beginning of the gallery.
141 * @param $image Image object that is added to the gallery
142 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
144 function insert( $image, $html='' ) {
145 array_unshift( $this->mImages
, array( &$image, $html ) );
150 * isEmpty() returns true if the gallery contains no images
153 return empty( $this->mImages
);
157 * Enable/Disable showing of the file size of an image in the gallery.
158 * Enabled by default.
160 * @param $f Boolean: set to false to disable.
162 function setShowBytes( $f ) {
163 $this->mShowBytes
= ( $f == true);
167 * Enable/Disable showing of the filename of an image in the gallery.
168 * Enabled by default.
170 * @param $f Boolean: set to false to disable.
172 function setShowFilename( $f ) {
173 $this->mShowFilename
= ( $f == true);
177 * Return a HTML representation of the image gallery
179 * For each image in the gallery, display
182 * - the additional text provided when adding the image
183 * - the size of the image
189 $sk = $this->getSkin();
191 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
192 if( $this->mCaption
)
193 $s .= "\n\t<caption>{$this->mCaption}</caption>";
195 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
197 foreach ( $this->mImages
as $pair ) {
201 $nt = $img->getTitle();
203 if( $nt->getNamespace() != NS_IMAGE
) {
204 # We're dealing with a non-image, spit out the name and be done with it.
205 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
206 . htmlspecialchars( $nt->getText() ) . '</div>';
207 } elseif( $this->mParsing
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
208 # The image is blacklisted, just show it as a text link.
209 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
210 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
211 } elseif( !( $thumb = $img->transform( $params ) ) ) {
212 # Error generating thumbnail.
213 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
214 . htmlspecialchars( $img->getLastError() ) . '</div>';
216 $vpad = floor( ( 1.25*$this->mHeights
- $thumb->height
) /2 ) - 2;
217 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+
30).'px;">'
218 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
222 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
224 if( $this->mShowBytes
) {
225 if( $img->exists() ) {
226 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
227 $wgLang->formatNum( $img->getSize() ) );
229 $nb = wfMsgHtml( 'filemissing' );
236 $textlink = $this->mShowFilename ?
237 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
240 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
241 # in version 4.8.6 generated crackpot html in its absence, see:
242 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
244 if ( $i %
$this->mPerRow
== 0 ) {
248 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths
*1.25).'px;">'
250 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
251 . $textlink . $text . $nb
253 . "\n\t\t</div></td>";
254 if ( $i %
$this->mPerRow
== $this->mPerRow
- 1 ) {
259 if( $i %
$this->mPerRow
!= 0 ) {
268 * @return int Number of images in the gallery
270 public function count() {
271 return count( $this->mImages
);
275 * Set the contextual title
277 * @param Title $title Contextual title
279 public function setContextTitle( $title ) {
280 $this->contextTitle
= $title;
284 * Get the contextual title, if applicable
286 * @return mixed Title or false
288 public function getContextTitle() {
289 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
290 ?
$this->contextTitle