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 * Hide blacklisted images?
28 * Registered parser object for output callbacks
33 * Contextual title, used when images are being screened
34 * against the bad image list
36 private $contextTitle = false;
38 private $mPerRow = 4; // How many images wide should the gallery be?
39 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
41 private $mAttribs = array();
44 * Create a new image gallery object.
46 function __construct( ) {
47 $this->mImages
= array();
48 $this->mShowBytes
= true;
49 $this->mShowFilename
= true;
50 $this->mParser
= false;
51 $this->mHideBadImages
= false;
55 * Register a parser object
57 function setParser( $parser ) {
58 $this->mParser
= $parser;
64 function setHideBadImages( $flag = true ) {
65 $this->mHideBadImages
= $flag;
69 * Set the caption (as plain text)
71 * @param $caption Caption
73 function setCaption( $caption ) {
74 $this->mCaption
= htmlspecialchars( $caption );
78 * Set the caption (as HTML)
80 * @param $caption Caption
82 public function setCaptionHtml( $caption ) {
83 $this->mCaption
= $caption;
87 * Set how many images will be displayed per row.
89 * @param int $num > 0; invalid numbers will be rejected
91 public function setPerRow( $num ) {
93 $this->mPerRow
= (int)$num;
98 * Set how wide each image will be, in pixels.
100 * @param int $num > 0; invalid numbers will be ignored
102 public function setWidths( $num ) {
104 $this->mWidths
= (int)$num;
109 * Set how high each image will be, in pixels.
111 * @param int $num > 0; invalid numbers will be ignored
113 public function setHeights( $num ) {
115 $this->mHeights
= (int)$num;
120 * Instruct the class to use a specific skin for rendering
122 * @param $skin Skin object
124 function useSkin( $skin ) {
125 $this->mSkin
= $skin;
129 * Return the skin that should be used
131 * @return Skin object
134 if( !$this->mSkin
) {
136 $skin = $wgUser->getSkin();
138 $skin = $this->mSkin
;
144 * Add an image to 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 add( $title, $html='' ) {
150 if ( $title instanceof File
) {
151 // Old calling convention
152 $title = $title->getTitle();
154 $this->mImages
[] = array( $title, $html );
155 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
159 * Add an image at the beginning of the gallery.
161 * @param $title Title object of the image that is added to the gallery
162 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
164 function insert( $title, $html='' ) {
165 if ( $title instanceof File
) {
166 // Old calling convention
167 $title = $title->getTitle();
169 array_unshift( $this->mImages
, array( &$title, $html ) );
174 * isEmpty() returns true if the gallery contains no images
177 return empty( $this->mImages
);
181 * Enable/Disable showing of the file size of an image in the gallery.
182 * Enabled by default.
184 * @param $f Boolean: set to false to disable.
186 function setShowBytes( $f ) {
187 $this->mShowBytes
= ( $f == true);
191 * Enable/Disable showing of the filename of an image in the gallery.
192 * Enabled by default.
194 * @param $f Boolean: set to false to disable.
196 function setShowFilename( $f ) {
197 $this->mShowFilename
= ( $f == true);
201 * Set arbitrary attributes to go on the HTML gallery output element.
202 * Should be suitable for a <table> element.
204 * Note -- if taking from user input, you should probably run through
205 * Sanitizer::validateAttributes() first.
207 * @param array of HTML attribute pairs
209 function setAttributes( $attribs ) {
210 $this->mAttribs
= $attribs;
214 * Return a HTML representation of the image gallery
216 * For each image in the gallery, display
219 * - the additional text provided when adding the image
220 * - the size of the image
226 $sk = $this->getSkin();
228 $attribs = Sanitizer
::mergeAttributes(
230 'class' => 'gallery',
231 'cellspacing' => '0',
232 'cellpadding' => '0' ),
234 $s = Xml
::openElement( 'table', $attribs );
235 if( $this->mCaption
)
236 $s .= "\n\t<caption>{$this->mCaption}</caption>";
238 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
240 foreach ( $this->mImages
as $pair ) {
244 # Give extensions a chance to select the file revision for us
246 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time ) );
248 $img = wfFindFile( $nt, $time );
250 if( $nt->getNamespace() != NS_IMAGE ||
!$img ) {
251 # We're dealing with a non-image, spit out the name and be done with it.
252 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
253 . htmlspecialchars( $nt->getText() ) . '</div>';
254 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
255 # The image is blacklisted, just show it as a text link.
256 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
257 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
258 } elseif( !( $thumb = $img->transform( $params ) ) ) {
259 # Error generating thumbnail.
260 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
261 . htmlspecialchars( $img->getLastError() ) . '</div>';
263 $vpad = floor( ( 1.25*$this->mHeights
- $thumb->height
) /2 ) - 2;
265 $thumbhtml = "\n\t\t\t".
266 '<div class="thumb" style="padding: ' . $vpad . 'px 0; width: ' .($this->mWidths+
30).'px;">'
267 # Auto-margin centering for block-level elements. Needed now that we have video
268 # handlers since they may emit block-level elements as opposed to simple <img> tags.
269 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
270 . '<div style="margin-left: auto; margin-right: auto; width: ' .$this->mWidths
.'px;">'
271 . $thumb->toHtml( array( 'desc-link' => true ) ) . '</div></div>';
273 // Call parser transform hook
274 if ( $this->mParser
&& $img->getHandler() ) {
275 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
280 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
282 if( $this->mShowBytes
) {
284 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
285 $wgLang->formatNum( $img->getSize() ) );
287 $nb = wfMsgHtml( 'filemissing' );
294 $textlink = $this->mShowFilename ?
295 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
298 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
299 # in version 4.8.6 generated crackpot html in its absence, see:
300 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
302 if ( $i %
$this->mPerRow
== 0 ) {
306 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths+
35).'px;">'
308 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
309 . $textlink . $text . $nb
311 . "\n\t\t</div></td>";
312 if ( $i %
$this->mPerRow
== $this->mPerRow
- 1 ) {
317 if( $i %
$this->mPerRow
!= 0 ) {
326 * @return int Number of images in the gallery
328 public function count() {
329 return count( $this->mImages
);
333 * Set the contextual title
335 * @param Title $title Contextual title
337 public function setContextTitle( $title ) {
338 $this->contextTitle
= $title;
342 * Get the contextual title, if applicable
344 * @return mixed Title or false
346 public function getContextTitle() {
347 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
348 ?
$this->contextTitle