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;
31 private $mPerRow = 4; // How many images wide should the gallery be?
32 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
35 * Create a new image gallery object.
37 function __construct( ) {
38 $this->mImages
= array();
39 $this->mShowBytes
= true;
40 $this->mShowFilename
= true;
41 $this->mParsing
= false;
45 * Set the "parse" bit so we know to hide "bad" images
47 function setParsing( $val = true ) {
48 $this->mParsing
= $val;
52 * Set the caption (as plain text)
54 * @param $caption Caption
56 function setCaption( $caption ) {
57 $this->mCaption
= htmlspecialchars( $caption );
61 * Set the caption (as HTML)
63 * @param $caption Caption
65 public function setCaptionHtml( $caption ) {
66 $this->mCaption
= $caption;
70 * Set how many images will be displayed per row.
72 * @param int $num > 0; invalid numbers will be rejected
74 public function setPerRow( $num ) {
76 $this->mPerRow
= (int)$num;
81 * Set how wide each image will be, in pixels.
83 * @param int $num > 0; invalid numbers will be ignored
85 public function setWidths( $num ) {
87 $this->mWidths
= (int)$num;
92 * Set how high each image will be, in pixels.
94 * @param int $num > 0; invalid numbers will be ignored
96 public function setHeights( $num ) {
98 $this->mHeights
= (int)$num;
103 * Instruct the class to use a specific skin for rendering
105 * @param $skin Skin object
107 function useSkin( $skin ) {
108 $this->mSkin
= $skin;
112 * Return the skin that should be used
114 * @return Skin object
117 if( !$this->mSkin
) {
119 $skin = $wgUser->getSkin();
121 $skin = $this->mSkin
;
127 * Add an image to the gallery.
129 * @param $image Image object that is added to the gallery
130 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
132 function add( $image, $html='' ) {
133 $this->mImages
[] = array( &$image, $html );
134 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
138 * Add an image at the beginning of the gallery.
140 * @param $image Image object that is added to the gallery
141 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
143 function insert( $image, $html='' ) {
144 array_unshift( $this->mImages
, array( &$image, $html ) );
149 * isEmpty() returns true if the gallery contains no images
152 return empty( $this->mImages
);
156 * Enable/Disable showing of the file size of an image in the gallery.
157 * Enabled by default.
159 * @param $f Boolean: set to false to disable.
161 function setShowBytes( $f ) {
162 $this->mShowBytes
= ( $f == true);
166 * Enable/Disable showing of the filename of an image in the gallery.
167 * Enabled by default.
169 * @param $f Boolean: set to false to disable.
171 function setShowFilename( $f ) {
172 $this->mShowFilename
= ( $f == true);
176 * Return a HTML representation of the image gallery
178 * For each image in the gallery, display
181 * - the additional text provided when adding the image
182 * - the size of the image
186 global $wgLang, $wgGenerateThumbnailOnParse;
188 $sk = $this->getSkin();
190 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
191 if( $this->mCaption
)
192 $s .= "\n\t<caption>{$this->mCaption}</caption>";
195 foreach ( $this->mImages
as $pair ) {
199 $nt = $img->getTitle();
201 if( $nt->getNamespace() != NS_IMAGE
) {
202 # We're dealing with a non-image, spit out the name and be done with it.
203 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
204 . htmlspecialchars( $nt->getText() ) . '</div>';
205 } elseif( $this->mParsing
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
206 # The image is blacklisted, just show it as a text link.
207 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
208 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
209 } elseif( !( $thumb = $img->getThumbnail( $this->mWidths
, $this->mHeights
, $wgGenerateThumbnailOnParse ) ) ) {
210 # Error generating thumbnail.
211 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
212 . htmlspecialchars( $img->getLastError() ) . '</div>';
214 $vpad = floor( ( 1.25*$this->mHeights
- $thumb->height
) /2 ) - 2;
215 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+
30).'px;">'
216 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
220 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
222 if( $this->mShowBytes
) {
223 if( $img->exists() ) {
224 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
225 $wgLang->formatNum( $img->getSize() ) );
227 $nb = wfMsgHtml( 'filemissing' );
234 $textlink = $this->mShowFilename ?
235 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
238 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
239 # in version 4.8.6 generated crackpot html in its absence, see:
240 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
242 if ( $i %
$this->mPerRow
== 0 ) {
246 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths
*1.25).'px;">'
248 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
249 . $textlink . $text . $nb
251 . "\n\t\t</div></td>";
252 if ( $i %
$this->mPerRow
== $this->mPerRow
- 1 ) {
257 if( $i %
$this->mPerRow
!= 0 ) {
266 * @return int Number of images in the gallery
268 public function count() {
269 return count( $this->mImages
);
273 * Set the contextual title
275 * @param Title $title Contextual title
277 public function setContextTitle( $title ) {
278 $this->contextTitle
= $title;
282 * Get the contextual title, if applicable
284 * @return mixed Title or false
286 public function getContextTitle() {
287 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
288 ?
$this->contextTitle