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;
18 * Hide blacklisted images?
23 * Registered parser object for output callbacks
29 * Contextual title, used when images are being screened
30 * against the bad image list
32 private $contextTitle = false;
34 private $mAttribs = array();
39 const THUMB_PADDING
= 30;
41 //2px borders on each side + 2px implied padding on each side
45 * Create a new image gallery object.
47 function __construct( ) {
48 global $wgGalleryOptions;
49 $this->mImages
= array();
50 $this->mShowBytes
= $wgGalleryOptions['showBytes'];
51 $this->mShowFilename
= true;
52 $this->mParser
= false;
53 $this->mHideBadImages
= false;
54 $this->mPerRow
= $wgGalleryOptions['imagesPerRow'];
55 $this->mWidths
= $wgGalleryOptions['imageWidth'];
56 $this->mHeights
= $wgGalleryOptions['imageHeight'];
57 $this->mCaptionLength
= $wgGalleryOptions['captionLength'];
61 * Register a parser object
63 function setParser( $parser ) {
64 $this->mParser
= $parser;
70 function setHideBadImages( $flag = true ) {
71 $this->mHideBadImages
= $flag;
75 * Set the caption (as plain text)
77 * @param $caption Caption
79 function setCaption( $caption ) {
80 $this->mCaption
= htmlspecialchars( $caption );
84 * Set the caption (as HTML)
86 * @param $caption String: Caption
88 public function setCaptionHtml( $caption ) {
89 $this->mCaption
= $caption;
93 * Set how many images will be displayed per row.
95 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
96 * invalid numbers will be rejected
98 public function setPerRow( $num ) {
100 $this->mPerRow
= (int)$num;
105 * Set how wide each image will be, in pixels.
107 * @param $num Integer > 0; invalid numbers will be ignored
109 public function setWidths( $num ) {
111 $this->mWidths
= (int)$num;
116 * Set how high each image will be, in pixels.
118 * @param $num Integer > 0; invalid numbers will be ignored
120 public function setHeights( $num ) {
122 $this->mHeights
= (int)$num;
127 * Instruct the class to use a specific skin for rendering
129 * @param $skin Skin object
130 * @deprecated since 1.18 Not used anymore
132 function useSkin( $skin ) {
133 wfDeprecated( __METHOD__
);
138 * Add an image to the gallery.
140 * @param $title Title object of the image that is added to the gallery
141 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
142 * @param $alt String: Alt text for the image
144 function add( $title, $html = '', $alt = '' ) {
145 if ( $title instanceof File
) {
146 // Old calling convention
147 $title = $title->getTitle();
149 $this->mImages
[] = array( $title, $html, $alt );
150 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
154 * Add an image at the beginning of the gallery.
156 * @param $title Title object of the image that is added to the gallery
157 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
158 * @param $alt String: Alt text for the image
160 function insert( $title, $html='', $alt='' ) {
161 if ( $title instanceof File
) {
162 // Old calling convention
163 $title = $title->getTitle();
165 array_unshift( $this->mImages
, array( &$title, $html, $alt ) );
170 * isEmpty() returns true if the gallery contains no images
173 return empty( $this->mImages
);
177 * Enable/Disable showing of the file size of an image in the gallery.
178 * Enabled by default.
180 * @param $f Boolean: set to false to disable.
182 function setShowBytes( $f ) {
183 $this->mShowBytes
= (bool)$f;
187 * Enable/Disable showing of the filename of an image in the gallery.
188 * Enabled by default.
190 * @param $f Boolean: set to false to disable.
192 function setShowFilename( $f ) {
193 $this->mShowFilename
= (bool)$f;
197 * Set arbitrary attributes to go on the HTML gallery output element.
198 * Should be suitable for a <ul> element.
200 * Note -- if taking from user input, you should probably run through
201 * Sanitizer::validateAttributes() first.
203 * @param $attribs Array of HTML attribute pairs
205 function setAttributes( $attribs ) {
206 $this->mAttribs
= $attribs;
210 * Return a HTML representation of the image gallery
212 * For each image in the gallery, display
215 * - the additional text provided when adding the image
216 * - the size of the image
222 if ( $this->mPerRow
> 0 ) {
223 $maxwidth = $this->mPerRow
* ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING + self
::GB_BORDERS
);
224 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : "";
225 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
226 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
229 $attribs = Sanitizer
::mergeAttributes(
230 array( 'class' => 'gallery' ), $this->mAttribs
);
232 $output = Xml
::openElement( 'ul', $attribs );
233 if ( $this->mCaption
) {
234 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
237 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
238 # Output each image...
239 foreach ( $this->mImages
as $pair ) {
241 $text = $pair[1]; # "text" means "caption" here
245 if ( $nt->getNamespace() == NS_FILE
) {
247 if ( $this->mParser
instanceof Parser
) {
248 # Give extensions a chance to select the file revision for us
249 $time = $sha1 = false;
250 wfRunHooks( 'BeforeParserFetchFileAndTitle',
251 array( $this->mParser
, $nt, &$time, &$sha1, &$descQuery ) );
252 # Fetch and register the file (file title may be different via hooks)
253 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $time, $sha1 );
255 $img = wfFindFile( $nt );
262 # We're dealing with a non-image, spit out the name and be done with it.
263 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
264 . htmlspecialchars( $nt->getText() ) . '</div>';
265 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
266 # The image is blacklisted, just show it as a text link.
267 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">' .
270 htmlspecialchars( $nt->getText() ),
273 array( 'known', 'noclasses' )
276 } elseif( !( $thumb = $img->transform( $params ) ) ) {
277 # Error generating thumbnail.
278 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
279 . htmlspecialchars( $img->getLastError() ) . '</div>';
281 # We get layout problems with the margin, if the image is smaller
282 # than the line-height (17), so we add less margin in these cases.
283 $minThumbHeight = $thumb->height
> 17 ?
$thumb->height
: 17;
284 $vpad = floor(( self
::THUMB_PADDING +
$this->mHeights
- $minThumbHeight ) /2);
287 $imageParameters = array(
289 'desc-query' => $descQuery,
292 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
293 if ( $alt == '' && $text == '' ) {
294 $imageParameters['alt'] = $nt->getText();
297 # Set both fixed width and min-height.
298 $thumbhtml = "\n\t\t\t".
299 '<div class="thumb" style="width: ' .($this->mWidths + self
::THUMB_PADDING
).'px;">'
300 # Auto-margin centering for block-level elements. Needed now that we have video
301 # handlers since they may emit block-level elements as opposed to simple <img> tags.
302 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
303 . '<div style="margin:'.$vpad.'px auto;">'
304 . $thumb->toHtml( $imageParameters ) . '</div></div>';
306 // Call parser transform hook
307 if ( $this->mParser
&& $img->getHandler() ) {
308 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
313 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
314 // $ul = Linker::link( $linkTarget, $ut );
316 if( $this->mShowBytes
) {
318 $fileSize = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
319 $wgLang->formatNum( $img->getSize() ) );
321 $fileSize = wfMsgHtml( 'filemissing' );
323 $fileSize = "$fileSize<br />\n";
328 $textlink = $this->mShowFilename ?
331 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength
) ),
334 array( 'known', 'noclasses' )
338 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
339 # in version 4.8.6 generated crackpot html in its absence, see:
340 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
342 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
343 # Can be safely removed if FF2 falls completely out of existance
345 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
346 . '<div style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
348 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
349 . $textlink . $text . $fileSize
351 . "\n\t\t</div></li>";
353 $output .= "\n</ul>";
359 * @return Integer: number of images in the gallery
361 public function count() {
362 return count( $this->mImages
);
366 * Set the contextual title
368 * @param $title Title: contextual title
370 public function setContextTitle( $title ) {
371 $this->contextTitle
= $title;
375 * Get the contextual title, if applicable
377 * @return mixed Title or false
379 public function getContextTitle() {
380 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
381 ?
$this->contextTitle