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 $title Title object of the image 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( $title, $html='' ) {
134 if ( $title instanceof File
) {
135 // Old calling convention
136 $title = $title->getTitle();
138 $this->mImages
[] = array( $title, $html );
139 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
143 * Add an image at the beginning of the gallery.
145 * @param $title Title object of the image that is added to the gallery
146 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
148 function insert( $title, $html='' ) {
149 array_unshift( $this->mImages
, array( &$title, $html ) );
154 * isEmpty() returns true if the gallery contains no images
157 return empty( $this->mImages
);
161 * Enable/Disable showing of the file size of an image in the gallery.
162 * Enabled by default.
164 * @param $f Boolean: set to false to disable.
166 function setShowBytes( $f ) {
167 $this->mShowBytes
= ( $f == true);
171 * Enable/Disable showing of the filename of an image in the gallery.
172 * Enabled by default.
174 * @param $f Boolean: set to false to disable.
176 function setShowFilename( $f ) {
177 $this->mShowFilename
= ( $f == true);
181 * Return a HTML representation of the image gallery
183 * For each image in the gallery, display
186 * - the additional text provided when adding the image
187 * - the size of the image
193 $sk = $this->getSkin();
195 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
196 if( $this->mCaption
)
197 $s .= "\n\t<caption>{$this->mCaption}</caption>";
199 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
201 foreach ( $this->mImages
as $pair ) {
205 $img = wfFindFile( $nt );
207 if( $nt->getNamespace() != NS_IMAGE ||
!$img ) {
208 # We're dealing with a non-image, spit out the name and be done with it.
209 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
210 . htmlspecialchars( $nt->getText() ) . '</div>';
211 } elseif( $this->mParsing
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
212 # The image is blacklisted, just show it as a text link.
213 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
214 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
215 } elseif( !( $thumb = $img->transform( $params ) ) ) {
216 # Error generating thumbnail.
217 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
218 . htmlspecialchars( $img->getLastError() ) . '</div>';
220 $vpad = floor( ( 1.25*$this->mHeights
- $thumb->height
) /2 ) - 2;
221 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+
30).'px;">'
222 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
226 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
228 if( $this->mShowBytes
) {
230 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
231 $wgLang->formatNum( $img->getSize() ) );
233 $nb = wfMsgHtml( 'filemissing' );
240 $textlink = $this->mShowFilename ?
241 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
244 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
245 # in version 4.8.6 generated crackpot html in its absence, see:
246 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
248 if ( $i %
$this->mPerRow
== 0 ) {
252 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths
*1.25).'px;">'
254 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
255 . $textlink . $text . $nb
257 . "\n\t\t</div></td>";
258 if ( $i %
$this->mPerRow
== $this->mPerRow
- 1 ) {
263 if( $i %
$this->mPerRow
!= 0 ) {
272 * @return int Number of images in the gallery
274 public function count() {
275 return count( $this->mImages
);
279 * Set the contextual title
281 * @param Title $title Contextual title
283 public function setContextTitle( $title ) {
284 $this->contextTitle
= $title;
288 * Get the contextual title, if applicable
290 * @return mixed Title or false
292 public function getContextTitle() {
293 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
294 ?
$this->contextTitle