2 if ( ! defined( 'MEDIAWIKI' ) )
8 * Add images to the gallery using add(), then render that list to HTML using toHTML().
14 var $mImages, $mShowBytes, $mShowFilename;
15 var $mCaption = false;
20 * Hide blacklisted images?
25 * Registered parser object for output callbacks
30 * Contextual title, used when images are being screened
31 * against the bad image list
33 private $contextTitle = false;
35 private $mPerRow = 4; // How many images wide should the gallery be?
36 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
38 private $mAttribs = array();
41 * Create a new image gallery object.
43 function __construct( ) {
44 $this->mImages
= array();
45 $this->mShowBytes
= true;
46 $this->mShowFilename
= true;
47 $this->mParser
= false;
48 $this->mHideBadImages
= false;
52 * Register a parser object
54 function setParser( $parser ) {
55 $this->mParser
= $parser;
61 function setHideBadImages( $flag = true ) {
62 $this->mHideBadImages
= $flag;
66 * Set the caption (as plain text)
68 * @param $caption Caption
70 function setCaption( $caption ) {
71 $this->mCaption
= htmlspecialchars( $caption );
75 * Set the caption (as HTML)
77 * @param $caption Caption
79 public function setCaptionHtml( $caption ) {
80 $this->mCaption
= $caption;
84 * Set how many images will be displayed per row.
86 * @param int $num > 0; invalid numbers will be rejected
88 public function setPerRow( $num ) {
90 $this->mPerRow
= (int)$num;
95 * Set how wide each image will be, in pixels.
97 * @param int $num > 0; invalid numbers will be ignored
99 public function setWidths( $num ) {
101 $this->mWidths
= (int)$num;
106 * Set how high each image will be, in pixels.
108 * @param int $num > 0; invalid numbers will be ignored
110 public function setHeights( $num ) {
112 $this->mHeights
= (int)$num;
117 * Instruct the class to use a specific skin for rendering
119 * @param $skin Skin object
121 function useSkin( $skin ) {
122 $this->mSkin
= $skin;
126 * Return the skin that should be used
128 * @return Skin object
131 if( !$this->mSkin
) {
133 $skin = $wgUser->getSkin();
135 $skin = $this->mSkin
;
141 * Add an image to the gallery.
143 * @param $title Title object of the image that is added to the gallery
144 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
146 function add( $title, $html='' ) {
147 if ( $title instanceof File
) {
148 // Old calling convention
149 $title = $title->getTitle();
151 $this->mImages
[] = array( $title, $html );
152 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
156 * Add an image at the beginning of the gallery.
158 * @param $title Title object of the image that is added to the gallery
159 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
161 function insert( $title, $html='' ) {
162 if ( $title instanceof File
) {
163 // Old calling convention
164 $title = $title->getTitle();
166 array_unshift( $this->mImages
, array( &$title, $html ) );
171 * isEmpty() returns true if the gallery contains no images
174 return empty( $this->mImages
);
178 * Enable/Disable showing of the file size of an image in the gallery.
179 * Enabled by default.
181 * @param $f Boolean: set to false to disable.
183 function setShowBytes( $f ) {
184 $this->mShowBytes
= ( $f == true);
188 * Enable/Disable showing of the filename of an image in the gallery.
189 * Enabled by default.
191 * @param $f Boolean: set to false to disable.
193 function setShowFilename( $f ) {
194 $this->mShowFilename
= ( $f == true);
198 * Set arbitrary attributes to go on the HTML gallery output element.
199 * Should be suitable for a <table> element.
201 * Note -- if taking from user input, you should probably run through
202 * Sanitizer::validateAttributes() first.
204 * @param array of HTML attribute pairs
206 function setAttributes( $attribs ) {
207 $this->mAttribs
= $attribs;
211 * Return a HTML representation of the image gallery
213 * For each image in the gallery, display
216 * - the additional text provided when adding the image
217 * - the size of the image
223 $sk = $this->getSkin();
225 $attribs = Sanitizer
::mergeAttributes(
227 'class' => 'gallery',
228 'cellspacing' => '0',
229 'cellpadding' => '0' ),
231 $s = Xml
::openElement( 'table', $attribs );
232 if( $this->mCaption
)
233 $s .= "\n\t<caption>{$this->mCaption}</caption>";
235 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
237 foreach ( $this->mImages
as $pair ) {
241 # Give extensions a chance to select the file revision for us
242 $time = $descQuery = false;
243 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
245 $img = wfFindFile( $nt, $time );
247 if( $nt->getNamespace() != NS_FILE ||
!$img ) {
248 # We're dealing with a non-image, spit out the name and be done with it.
249 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
250 . htmlspecialchars( $nt->getText() ) . '</div>';
251 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
252 # The image is blacklisted, just show it as a text link.
253 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
254 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
255 } elseif( !( $thumb = $img->transform( $params ) ) ) {
256 # Error generating thumbnail.
257 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
258 . htmlspecialchars( $img->getLastError() ) . '</div>';
260 $vpad = floor( ( 1.25*$this->mHeights
- $thumb->height
) /2 ) - 2;
262 $thumbhtml = "\n\t\t\t".
263 '<div class="thumb" style="padding: ' . $vpad . 'px 0; width: ' .($this->mWidths+
30).'px;">'
264 # Auto-margin centering for block-level elements. Needed now that we have video
265 # handlers since they may emit block-level elements as opposed to simple <img> tags.
266 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
267 . '<div style="margin-left: auto; margin-right: auto; width: ' .$this->mWidths
.'px;">'
268 . $thumb->toHtml( array( 'desc-link' => true, 'desc-query' => $descQuery ) ) . '</div></div>';
270 // Call parser transform hook
271 if ( $this->mParser
&& $img->getHandler() ) {
272 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
277 //$ul = $sk->makeLink( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}", $ut );
279 if( $this->mShowBytes
) {
281 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
282 $wgLang->formatNum( $img->getSize() ) );
284 $nb = wfMsgHtml( 'filemissing' );
291 $textlink = $this->mShowFilename ?
292 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20 ) ) ) . "<br />\n" :
295 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
296 # in version 4.8.6 generated crackpot html in its absence, see:
297 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
299 if ( $i %
$this->mPerRow
== 0 ) {
303 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths+
35).'px;">'
305 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
306 . $textlink . $text . $nb
308 . "\n\t\t</div></td>";
309 if ( $i %
$this->mPerRow
== $this->mPerRow
- 1 ) {
314 if( $i %
$this->mPerRow
!= 0 ) {
323 * @return int Number of images in the gallery
325 public function count() {
326 return count( $this->mImages
);
330 * Set the contextual title
332 * @param Title $title Contextual title
334 public function setContextTitle( $title ) {
335 $this->contextTitle
= $title;
339 * Get the contextual title, if applicable
341 * @return mixed Title or false
343 public function getContextTitle() {
344 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
345 ?
$this->contextTitle