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 Not used anymore
132 function useSkin( $skin ) {
137 * Add an image to the gallery.
139 * @param $title Title object of the image that is added to the gallery
140 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
141 * @param $alt String: Alt text for the image
143 function add( $title, $html = '', $alt = '' ) {
144 if ( $title instanceof File
) {
145 // Old calling convention
146 $title = $title->getTitle();
148 $this->mImages
[] = array( $title, $html, $alt );
149 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
153 * Add an image at the beginning of the gallery.
155 * @param $title Title object of the image that is added to the gallery
156 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
157 * @param $alt String: Alt text for the image
159 function insert( $title, $html='', $alt='' ) {
160 if ( $title instanceof File
) {
161 // Old calling convention
162 $title = $title->getTitle();
164 array_unshift( $this->mImages
, array( &$title, $html, $alt ) );
169 * isEmpty() returns true if the gallery contains no images
172 return empty( $this->mImages
);
176 * Enable/Disable showing of the file size of an image in the gallery.
177 * Enabled by default.
179 * @param $f Boolean: set to false to disable.
181 function setShowBytes( $f ) {
182 $this->mShowBytes
= (bool)$f;
186 * Enable/Disable showing of the filename of an image in the gallery.
187 * Enabled by default.
189 * @param $f Boolean: set to false to disable.
191 function setShowFilename( $f ) {
192 $this->mShowFilename
= (bool)$f;
196 * Set arbitrary attributes to go on the HTML gallery output element.
197 * Should be suitable for a <ul> element.
199 * Note -- if taking from user input, you should probably run through
200 * Sanitizer::validateAttributes() first.
202 * @param $attribs Array of HTML attribute pairs
204 function setAttributes( $attribs ) {
205 $this->mAttribs
= $attribs;
209 * Return a HTML representation of the image gallery
211 * For each image in the gallery, display
214 * - the additional text provided when adding the image
215 * - the size of the image
221 if ( $this->mPerRow
> 0 ) {
222 $maxwidth = $this->mPerRow
* ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING + self
::GB_BORDERS
);
223 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : "";
224 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
225 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
228 $attribs = Sanitizer
::mergeAttributes(
229 array( 'class' => 'gallery' ), $this->mAttribs
);
231 $output = Xml
::openElement( 'ul', $attribs );
232 if ( $this->mCaption
) {
233 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
236 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
237 # Output each image...
238 foreach ( $this->mImages
as $pair ) {
240 $text = $pair[1]; # "text" means "caption" here
244 if ( $nt->getNamespace() == NS_FILE
) {
246 if ( $this->mParser
instanceof Parser
) {
247 # Give extensions a chance to select the file revision for us
248 $time = $sha1 = false;
249 wfRunHooks( 'BeforeParserFetchFileAndTitle',
250 array( $this->mParser
, $nt, &$time, &$sha1, &$descQuery ) );
251 # Fetch and register the file (file title may be different via hooks)
252 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $time, $sha1 );
254 $img = wfFindFile( $nt );
261 # We're dealing with a non-image, spit out the name and be done with it.
262 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
263 . htmlspecialchars( $nt->getText() ) . '</div>';
264 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
265 # The image is blacklisted, just show it as a text link.
266 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">' .
269 htmlspecialchars( $nt->getText() ),
272 array( 'known', 'noclasses' )
275 } elseif( !( $thumb = $img->transform( $params ) ) ) {
276 # Error generating thumbnail.
277 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
278 . htmlspecialchars( $img->getLastError() ) . '</div>';
280 # We get layout problems with the margin, if the image is smaller
281 # than the line-height (17), so we add less margin in these cases.
282 $minThumbHeight = $thumb->height
> 17 ?
$thumb->height
: 17;
283 $vpad = floor(( self
::THUMB_PADDING +
$this->mHeights
- $minThumbHeight ) /2);
286 $imageParameters = array(
288 'desc-query' => $descQuery,
291 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
292 if ( $alt == '' && $text == '' ) {
293 $imageParameters['alt'] = $nt->getText();
296 # Set both fixed width and min-height.
297 $thumbhtml = "\n\t\t\t".
298 '<div class="thumb" style="width: ' .($this->mWidths + self
::THUMB_PADDING
).'px;">'
299 # Auto-margin centering for block-level elements. Needed now that we have video
300 # handlers since they may emit block-level elements as opposed to simple <img> tags.
301 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
302 . '<div style="margin:'.$vpad.'px auto;">'
303 . $thumb->toHtml( $imageParameters ) . '</div></div>';
305 // Call parser transform hook
306 if ( $this->mParser
&& $img->getHandler() ) {
307 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
312 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
313 // $ul = Linker::link( $linkTarget, $ut );
315 if( $this->mShowBytes
) {
317 $fileSize = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
318 $wgLang->formatNum( $img->getSize() ) );
320 $fileSize = wfMsgHtml( 'filemissing' );
322 $fileSize = "$fileSize<br />\n";
327 $textlink = $this->mShowFilename ?
330 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength
) ),
333 array( 'known', 'noclasses' )
337 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
338 # in version 4.8.6 generated crackpot html in its absence, see:
339 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
341 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
342 # Can be safely removed if FF2 falls completely out of existance
344 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
345 . '<div style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
347 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
348 . $textlink . $text . $fileSize
350 . "\n\t\t</div></li>";
352 $output .= "\n</ul>";
358 * @return Integer: number of images in the gallery
360 public function count() {
361 return count( $this->mImages
);
365 * Set the contextual title
367 * @param $title Title: contextual title
369 public function setContextTitle( $title ) {
370 $this->contextTitle
= $title;
374 * Get the contextual title, if applicable
376 * @return mixed Title or false
378 public function getContextTitle() {
379 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
380 ?
$this->contextTitle