2 if ( ! defined( 'MEDIAWIKI' ) )
8 * Add images to the gallery using add(), then render that list to HTML using toHTML().
13 var $mImages, $mShowBytes, $mShowFilename;
14 var $mCaption = false;
17 * Hide blacklisted images?
22 * Registered parser object for output callbacks
28 * Contextual title, used when images are being screened
29 * against the bad image list
31 private $contextTitle = false;
33 private $mAttribs = array();
38 const THUMB_PADDING
= 30;
40 // 2px borders on each side + 2px implied padding on each side
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 since 1.18 Not used anymore
131 function useSkin( $skin ) {
132 wfDeprecated( __METHOD__
);
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 ) );
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>";
236 'width' => $this->mWidths
,
237 'height' => $this->mHeights
239 # Output each image...
240 foreach ( $this->mImages
as $pair ) {
242 $text = $pair[1]; # "text" means "caption" here
246 if ( $nt->getNamespace() == NS_FILE
) {
248 if ( $this->mParser
instanceof Parser
) {
249 # Give extensions a chance to select the file revision for us
250 $time = $sha1 = false;
251 wfRunHooks( 'BeforeParserFetchFileAndTitle',
252 array( $this->mParser
, $nt, &$time, &$sha1, &$descQuery ) );
253 # Fetch and register the file (file title may be different via hooks)
254 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $time, $sha1 );
256 $img = wfFindFile( $nt );
263 # We're dealing with a non-image, spit out the name and be done with it.
264 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self
::THUMB_PADDING +
$this->mHeights
) . 'px;">'
265 . htmlspecialchars( $nt->getText() ) . '</div>';
266 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
267 # The image is blacklisted, just show it as a text link.
268 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self
::THUMB_PADDING +
$this->mHeights
) . 'px;">' .
271 htmlspecialchars( $nt->getText() ),
274 array( 'known', 'noclasses' )
277 } elseif( !( $thumb = $img->transform( $params ) ) ) {
278 # Error generating thumbnail.
279 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self
::THUMB_PADDING +
$this->mHeights
) . 'px;">'
280 . htmlspecialchars( $img->getLastError() ) . '</div>';
282 # We get layout problems with the margin, if the image is smaller
283 # than the line-height (17), so we add less margin in these cases.
284 $minThumbHeight = $thumb->height
> 17 ?
$thumb->height
: 17;
285 $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