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;
44 * Create a new image gallery object.
46 function __construct( ) {
47 global $wgGalleryOptions;
48 $this->mImages
= array();
49 $this->mShowBytes
= $wgGalleryOptions['showBytes'];
50 $this->mShowFilename
= true;
51 $this->mParser
= false;
52 $this->mHideBadImages
= false;
53 $this->mPerRow
= $wgGalleryOptions['imagesPerRow'];
54 $this->mWidths
= $wgGalleryOptions['imageWidth'];
55 $this->mHeights
= $wgGalleryOptions['imageHeight'];
56 $this->mCaptionLength
= $wgGalleryOptions['captionLength'];
60 * Register a parser object
62 function setParser( $parser ) {
63 $this->mParser
= $parser;
69 function setHideBadImages( $flag = true ) {
70 $this->mHideBadImages
= $flag;
74 * Set the caption (as plain text)
76 * @param $caption Caption
78 function setCaption( $caption ) {
79 $this->mCaption
= htmlspecialchars( $caption );
83 * Set the caption (as HTML)
85 * @param $caption String: Caption
87 public function setCaptionHtml( $caption ) {
88 $this->mCaption
= $caption;
92 * Set how many images will be displayed per row.
94 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
95 * invalid numbers will be rejected
97 public function setPerRow( $num ) {
99 $this->mPerRow
= (int)$num;
104 * Set how wide each image will be, in pixels.
106 * @param $num Integer > 0; invalid numbers will be ignored
108 public function setWidths( $num ) {
110 $this->mWidths
= (int)$num;
115 * Set how high each image will be, in pixels.
117 * @param $num Integer > 0; invalid numbers will be ignored
119 public function setHeights( $num ) {
121 $this->mHeights
= (int)$num;
126 * Instruct the class to use a specific skin for rendering
128 * @param $skin Skin object
129 * @deprecated Not used anymore
131 function useSkin( $skin ) {
136 * Add an image to the gallery.
138 * @param $title Title object of the image that is added to the gallery
139 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
140 * @param $alt String: Alt text for the image
142 function add( $title, $html = '', $alt = '' ) {
143 if ( $title instanceof File
) {
144 // Old calling convention
145 $title = $title->getTitle();
147 $this->mImages
[] = array( $title, $html, $alt );
148 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
152 * Add an image at the beginning of the gallery.
154 * @param $title Title object of the image that is added to the gallery
155 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
156 * @param $alt String: Alt text for the image
158 function insert( $title, $html='', $alt='' ) {
159 if ( $title instanceof File
) {
160 // Old calling convention
161 $title = $title->getTitle();
163 array_unshift( $this->mImages
, array( &$title, $html, $alt ) );
168 * isEmpty() returns true if the gallery contains no images
171 return empty( $this->mImages
);
175 * Enable/Disable showing of the file size of an image in the gallery.
176 * Enabled by default.
178 * @param $f Boolean: set to false to disable.
180 function setShowBytes( $f ) {
181 $this->mShowBytes
= (bool)$f;
185 * Enable/Disable showing of the filename of an image in the gallery.
186 * Enabled by default.
188 * @param $f Boolean: set to false to disable.
190 function setShowFilename( $f ) {
191 $this->mShowFilename
= (bool)$f;
195 * Set arbitrary attributes to go on the HTML gallery output element.
196 * Should be suitable for a <ul> element.
198 * Note -- if taking from user input, you should probably run through
199 * Sanitizer::validateAttributes() first.
201 * @param $attribs Array of HTML attribute pairs
203 function setAttributes( $attribs ) {
204 $this->mAttribs
= $attribs;
208 * Return a HTML representation of the image gallery
210 * For each image in the gallery, display
213 * - the additional text provided when adding the image
214 * - the size of the image
220 if ( $this->mPerRow
> 0 ) {
221 $maxwidth = $this->mPerRow
* ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING + self
::GB_BORDERS
);
222 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : "";
223 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
224 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
227 $attribs = Sanitizer
::mergeAttributes(
228 array( 'class' => 'gallery' ), $this->mAttribs
);
230 $output = Xml
::openElement( 'ul', $attribs );
231 if ( $this->mCaption
) {
232 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
235 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
236 # Output each image...
237 foreach ( $this->mImages
as $pair ) {
239 $text = $pair[1]; # "text" means "caption" here
243 if ( $nt->getNamespace() == NS_FILE
) {
245 if ( $this->mParser
instanceof Parser
) {
246 # Give extensions a chance to select the file revision for us
247 $time = $sha1 = false;
248 wfRunHooks( 'BeforeParserFetchFileAndTitle',
249 array( $this->mParser
, $nt, &$time, &$sha1, &$descQuery ) );
250 # Fetch and register the file (file title may be different via hooks)
251 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $time, $sha1 );
253 $img = wfFindFile( $nt );
260 # We're dealing with a non-image, spit out the name and be done with it.
261 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
262 . htmlspecialchars( $nt->getText() ) . '</div>';
263 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
264 # The image is blacklisted, just show it as a text link.
265 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">' .
268 htmlspecialchars( $nt->getText() ),
271 array( 'known', 'noclasses' )
274 } elseif( !( $thumb = $img->transform( $params ) ) ) {
275 # Error generating thumbnail.
276 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
277 . htmlspecialchars( $img->getLastError() ) . '</div>';
279 # We get layout problems with the margin, if the image is smaller
280 # than the line-height (17), so we add less margin in these cases.
281 $minThumbHeight = $thumb->height
> 17 ?
$thumb->height
: 17;
282 $vpad = floor(( self
::THUMB_PADDING +
$this->mHeights
- $minThumbHeight ) /2);
285 $imageParameters = array(
287 'desc-query' => $descQuery,
290 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
291 if ( $alt == '' && $text == '' ) {
292 $imageParameters['alt'] = $nt->getText();
295 # Set both fixed width and min-height.
296 $thumbhtml = "\n\t\t\t".
297 '<div class="thumb" style="width: ' .($this->mWidths + self
::THUMB_PADDING
).'px;">'
298 # Auto-margin centering for block-level elements. Needed now that we have video
299 # handlers since they may emit block-level elements as opposed to simple <img> tags.
300 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
301 . '<div style="margin:'.$vpad.'px auto;">'
302 . $thumb->toHtml( $imageParameters ) . '</div></div>';
304 // Call parser transform hook
305 if ( $this->mParser
&& $img->getHandler() ) {
306 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
311 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
312 // $ul = Linker::link( $linkTarget, $ut );
314 if( $this->mShowBytes
) {
316 $fileSize = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
317 $wgLang->formatNum( $img->getSize() ) );
319 $fileSize = wfMsgHtml( 'filemissing' );
321 $fileSize = "$fileSize<br />\n";
326 $textlink = $this->mShowFilename ?
329 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength
) ),
332 array( 'known', 'noclasses' )
336 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
337 # in version 4.8.6 generated crackpot html in its absence, see:
338 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
340 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
341 # Can be safely removed if FF2 falls completely out of existance
343 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
344 . '<div style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
346 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
347 . $textlink . $text . $fileSize
349 . "\n\t\t</div></li>";
351 $output .= "\n</ul>";
357 * @return Integer: number of images in the gallery
359 public function count() {
360 return count( $this->mImages
);
364 * Set the contextual title
366 * @param $title Title: contextual title
368 public function setContextTitle( $title ) {
369 $this->contextTitle
= $title;
373 * Get the contextual title, if applicable
375 * @return mixed Title or false
377 public function getContextTitle() {
378 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
379 ?
$this->contextTitle