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
36 private $mAttribs = array();
39 * Create a new image gallery object.
41 function __construct( ) {
42 $this->mImages
= array();
43 $this->mShowBytes
= true;
44 $this->mShowFilename
= true;
45 $this->mParsing
= false;
49 * Set the "parse" bit so we know to hide "bad" images
51 function setParsing( $val = true ) {
52 $this->mParsing
= $val;
56 * Set the caption (as plain text)
58 * @param $caption Caption
60 function setCaption( $caption ) {
61 $this->mCaption
= htmlspecialchars( $caption );
65 * Set the caption (as HTML)
67 * @param $caption Caption
69 public function setCaptionHtml( $caption ) {
70 $this->mCaption
= $caption;
74 * Set how many images will be displayed per row.
76 * @param int $num > 0; invalid numbers will be rejected
78 public function setPerRow( $num ) {
80 $this->mPerRow
= (int)$num;
85 * Set how wide each image will be, in pixels.
87 * @param int $num > 0; invalid numbers will be ignored
89 public function setWidths( $num ) {
91 $this->mWidths
= (int)$num;
96 * Set how high each image will be, in pixels.
98 * @param int $num > 0; invalid numbers will be ignored
100 public function setHeights( $num ) {
102 $this->mHeights
= (int)$num;
107 * Instruct the class to use a specific skin for rendering
109 * @param $skin Skin object
111 function useSkin( $skin ) {
112 $this->mSkin
= $skin;
116 * Return the skin that should be used
118 * @return Skin object
121 if( !$this->mSkin
) {
123 $skin = $wgUser->getSkin();
125 $skin = $this->mSkin
;
131 * Add an image to the gallery.
133 * @param $title Title object of the image that is added to the gallery
134 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
136 function add( $title, $html='' ) {
137 if ( $title instanceof File
) {
138 // Old calling convention
139 $title = $title->getTitle();
141 $this->mImages
[] = array( $title, $html );
142 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
146 * Add an image at the beginning of the gallery.
148 * @param $title Title object of the image that is added to the gallery
149 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
151 function insert( $title, $html='' ) {
152 if ( $title instanceof File
) {
153 // Old calling convention
154 $title = $title->getTitle();
156 array_unshift( $this->mImages
, array( &$title, $html ) );
161 * isEmpty() returns true if the gallery contains no images
164 return empty( $this->mImages
);
168 * Enable/Disable showing of the file size of an image in the gallery.
169 * Enabled by default.
171 * @param $f Boolean: set to false to disable.
173 function setShowBytes( $f ) {
174 $this->mShowBytes
= ( $f == true);
178 * Enable/Disable showing of the filename of an image in the gallery.
179 * Enabled by default.
181 * @param $f Boolean: set to false to disable.
183 function setShowFilename( $f ) {
184 $this->mShowFilename
= ( $f == true);
188 * Set arbitrary attributes to go on the HTML gallery output element.
189 * Should be suitable for a <table> element.
191 * Note -- if taking from user input, you should probably run through
192 * Sanitizer::validateAttributes() first.
194 * @param array of HTML attribute pairs
196 function setAttributes( $attribs ) {
197 $this->mAttribs
= $attribs;
201 * Return a HTML representation of the image gallery
203 * For each image in the gallery, display
206 * - the additional text provided when adding the image
207 * - the size of the image
213 $sk = $this->getSkin();
215 $attribs = Sanitizer
::mergeAttributes(
217 'class' => 'gallery',
218 'cellspacing' => '0',
219 'cellpadding' => '0' ),
221 $s = Xml
::openElement( 'table', $attribs );
222 if( $this->mCaption
)
223 $s .= "\n\t<caption>{$this->mCaption}</caption>";
225 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
227 foreach ( $this->mImages
as $pair ) {
231 # Give extensions a chance to select the file revision for us
233 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time ) );
235 $img = wfFindFile( $nt, $time );
237 if( $nt->getNamespace() != NS_IMAGE ||
!$img ) {
238 # We're dealing with a non-image, spit out the name and be done with it.
239 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
240 . htmlspecialchars( $nt->getText() ) . '</div>';
241 } elseif( $this->mParsing
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
242 # The image is blacklisted, just show it as a text link.
243 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
244 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
245 } elseif( !( $thumb = $img->transform( $params ) ) ) {
246 # Error generating thumbnail.
247 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
248 . htmlspecialchars( $img->getLastError() ) . '</div>';
250 $vpad = floor( ( 1.25*$this->mHeights
- $thumb->height
) /2 ) - 2;
251 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+
30).'px;">'
252 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
256 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
258 if( $this->mShowBytes
) {
260 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
261 $wgLang->formatNum( $img->getSize() ) );
263 $nb = wfMsgHtml( 'filemissing' );
270 $textlink = $this->mShowFilename ?
271 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
274 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
275 # in version 4.8.6 generated crackpot html in its absence, see:
276 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
278 if ( $i %
$this->mPerRow
== 0 ) {
282 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths
*1.25).'px;">'
284 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
285 . $textlink . $text . $nb
287 . "\n\t\t</div></td>";
288 if ( $i %
$this->mPerRow
== $this->mPerRow
- 1 ) {
293 if( $i %
$this->mPerRow
!= 0 ) {
302 * @return int Number of images in the gallery
304 public function count() {
305 return count( $this->mImages
);
309 * Set the contextual title
311 * @param Title $title Contextual title
313 public function setContextTitle( $title ) {
314 $this->contextTitle
= $title;
318 * Get the contextual title, if applicable
320 * @return mixed Title or false
322 public function getContextTitle() {
323 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
324 ?
$this->contextTitle