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 array_unshift( $this->mImages
, array( &$title, $html ) );
155 * isEmpty() returns true if the gallery contains no images
158 return empty( $this->mImages
);
162 * Enable/Disable showing of the file size of an image in the gallery.
163 * Enabled by default.
165 * @param $f Boolean: set to false to disable.
167 function setShowBytes( $f ) {
168 $this->mShowBytes
= ( $f == true);
172 * Enable/Disable showing of the filename of an image in the gallery.
173 * Enabled by default.
175 * @param $f Boolean: set to false to disable.
177 function setShowFilename( $f ) {
178 $this->mShowFilename
= ( $f == true);
182 * Return a HTML representation of the image gallery
184 * For each image in the gallery, display
187 * - the additional text provided when adding the image
188 * - the size of the image
194 $sk = $this->getSkin();
196 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
197 if( $this->mCaption
)
198 $s .= "\n\t<caption>{$this->mCaption}</caption>";
200 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
202 foreach ( $this->mImages
as $pair ) {
206 # Give extensions a chance to select the file revision for us
208 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time ) );
210 $img = wfFindFile( $nt, $time );
212 if( $nt->getNamespace() != NS_IMAGE ||
!$img ) {
213 # We're dealing with a non-image, spit out the name and be done with it.
214 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
215 . htmlspecialchars( $nt->getText() ) . '</div>';
216 } elseif( $this->mParsing
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
217 # The image is blacklisted, just show it as a text link.
218 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
219 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
220 } elseif( !( $thumb = $img->transform( $params ) ) ) {
221 # Error generating thumbnail.
222 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
223 . htmlspecialchars( $img->getLastError() ) . '</div>';
225 $vpad = floor( ( 1.25*$this->mHeights
- $thumb->height
) /2 ) - 2;
226 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+
30).'px;">'
227 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
231 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
233 if( $this->mShowBytes
) {
235 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
236 $wgLang->formatNum( $img->getSize() ) );
238 $nb = wfMsgHtml( 'filemissing' );
245 $textlink = $this->mShowFilename ?
246 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
249 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
250 # in version 4.8.6 generated crackpot html in its absence, see:
251 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
253 if ( $i %
$this->mPerRow
== 0 ) {
257 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths
*1.25).'px;">'
259 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
260 . $textlink . $text . $nb
262 . "\n\t\t</div></td>";
263 if ( $i %
$this->mPerRow
== $this->mPerRow
- 1 ) {
268 if( $i %
$this->mPerRow
!= 0 ) {
277 * @return int Number of images in the gallery
279 public function count() {
280 return count( $this->mImages
);
284 * Set the contextual title
286 * @param Title $title Contextual title
288 public function setContextTitle( $title ) {
289 $this->contextTitle
= $title;
293 * Get the contextual title, if applicable
295 * @return mixed Title or false
297 public function getContextTitle() {
298 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
299 ?
$this->contextTitle