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;
23 * Is the gallery on a wiki page (i.e. not a special page)
28 * Contextual title, used when images are being screened
29 * against the bad image list
31 private $contextTitle = false;
33 private $mPerRow = 4; // How many images wide should the gallery be?
34 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
37 * Create a new image gallery object.
39 function __construct( ) {
40 $this->mImages
= array();
41 $this->mShowBytes
= true;
42 $this->mShowFilename
= true;
43 $this->mParsing
= false;
47 * Set the "parse" bit so we know to hide "bad" images
49 function setParsing( $val = true ) {
50 $this->mParsing
= $val;
54 * Set the caption (as plain text)
56 * @param $caption Caption
58 function setCaption( $caption ) {
59 $this->mCaption
= htmlspecialchars( $caption );
63 * Set the caption (as HTML)
65 * @param $caption Caption
67 public function setCaptionHtml( $caption ) {
68 $this->mCaption
= $caption;
72 * Set how many images will be displayed per row.
74 * @param int $num > 0; invalid numbers will be rejected
76 public function setPerRow( $num ) {
78 $this->mPerRow
= (int)$num;
83 * Set how wide each image will be, in pixels.
85 * @param int $num > 0; invalid numbers will be ignored
87 public function setWidths( $num ) {
89 $this->mWidths
= (int)$num;
94 * Set how high each image will be, in pixels.
96 * @param int $num > 0; invalid numbers will be ignored
98 public function setHeights( $num ) {
100 $this->mHeights
= (int)$num;
105 * Instruct the class to use a specific skin for rendering
107 * @param $skin Skin object
109 function useSkin( $skin ) {
110 $this->mSkin
= $skin;
114 * Return the skin that should be used
116 * @return Skin object
119 if( !$this->mSkin
) {
121 $skin = $wgUser->getSkin();
123 $skin = $this->mSkin
;
129 * Add an image to the gallery.
131 * @param $title Title object of the image that is added to the gallery
132 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
134 function add( $title, $html='' ) {
135 if ( $title instanceof File
) {
136 // Old calling convention
137 $title = $title->getTitle();
139 $this->mImages
[] = array( $title, $html );
140 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
144 * Add an image at the beginning of the gallery.
146 * @param $title Title object of the image that is added to the gallery
147 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
149 function insert( $title, $html='' ) {
150 if ( $title instanceof File
) {
151 // Old calling convention
152 $title = $title->getTitle();
154 array_unshift( $this->mImages
, array( &$title, $html ) );
159 * isEmpty() returns true if the gallery contains no images
162 return empty( $this->mImages
);
166 * Enable/Disable showing of the file size of an image in the gallery.
167 * Enabled by default.
169 * @param $f Boolean: set to false to disable.
171 function setShowBytes( $f ) {
172 $this->mShowBytes
= ( $f == true);
176 * Enable/Disable showing of the filename of an image in the gallery.
177 * Enabled by default.
179 * @param $f Boolean: set to false to disable.
181 function setShowFilename( $f ) {
182 $this->mShowFilename
= ( $f == true);
186 * Return a HTML representation of the image gallery
188 * For each image in the gallery, display
191 * - the additional text provided when adding the image
192 * - the size of the image
198 $sk = $this->getSkin();
200 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
201 if( $this->mCaption
)
202 $s .= "\n\t<caption>{$this->mCaption}</caption>";
204 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
206 foreach ( $this->mImages
as $pair ) {
210 # Give extensions a chance to select the file revision for us
212 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time ) );
214 $img = wfFindFile( $nt, $time );
216 if( $nt->getNamespace() != NS_IMAGE ||
!$img ) {
217 # We're dealing with a non-image, spit out the name and be done with it.
218 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
219 . htmlspecialchars( $nt->getText() ) . '</div>';
220 } elseif( $this->mParsing
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
221 # The image is blacklisted, just show it as a text link.
222 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
223 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
224 } elseif( !( $thumb = $img->transform( $params ) ) ) {
225 # Error generating thumbnail.
226 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
227 . htmlspecialchars( $img->getLastError() ) . '</div>';
229 $vpad = floor( ( 1.25*$this->mHeights
- $thumb->height
) /2 ) - 2;
230 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+
30).'px;">'
231 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
235 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
237 if( $this->mShowBytes
) {
239 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
240 $wgLang->formatNum( $img->getSize() ) );
242 $nb = wfMsgHtml( 'filemissing' );
249 $textlink = $this->mShowFilename ?
250 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
253 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
254 # in version 4.8.6 generated crackpot html in its absence, see:
255 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
257 if ( $i %
$this->mPerRow
== 0 ) {
261 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths
*1.25).'px;">'
263 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
264 . $textlink . $text . $nb
266 . "\n\t\t</div></td>";
267 if ( $i %
$this->mPerRow
== $this->mPerRow
- 1 ) {
272 if( $i %
$this->mPerRow
!= 0 ) {
281 * @return int Number of images in the gallery
283 public function count() {
284 return count( $this->mImages
);
288 * Set the contextual title
290 * @param Title $title Contextual title
292 public function setContextTitle( $title ) {
293 $this->contextTitle
= $title;
297 * Get the contextual title, if applicable
299 * @return mixed Title or false
301 public function getContextTitle() {
302 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
303 ?
$this->contextTitle