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.
141 function add( $title, $html='' ) {
142 if ( $title instanceof File
) {
143 // Old calling convention
144 $title = $title->getTitle();
146 $this->mImages
[] = array( $title, $html );
147 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
151 * Add an image at the beginning of the gallery.
153 * @param $title Title object of the image that is added to the gallery
154 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
156 function insert( $title, $html='' ) {
157 if ( $title instanceof File
) {
158 // Old calling convention
159 $title = $title->getTitle();
161 array_unshift( $this->mImages
, array( &$title, $html ) );
166 * isEmpty() returns true if the gallery contains no images
169 return empty( $this->mImages
);
173 * Enable/Disable showing of the file size of an image in the gallery.
174 * Enabled by default.
176 * @param $f Boolean: set to false to disable.
178 function setShowBytes( $f ) {
179 $this->mShowBytes
= (bool)$f;
183 * Enable/Disable showing of the filename of an image in the gallery.
184 * Enabled by default.
186 * @param $f Boolean: set to false to disable.
188 function setShowFilename( $f ) {
189 $this->mShowFilename
= (bool)$f;
193 * Set arbitrary attributes to go on the HTML gallery output element.
194 * Should be suitable for a <ul> element.
196 * Note -- if taking from user input, you should probably run through
197 * Sanitizer::validateAttributes() first.
199 * @param $attribs Array of HTML attribute pairs
201 function setAttributes( $attribs ) {
202 $this->mAttribs
= $attribs;
206 * Return a HTML representation of the image gallery
208 * For each image in the gallery, display
211 * - the additional text provided when adding the image
212 * - the size of the image
218 if ( $this->mPerRow
> 0 ) {
219 $maxwidth = $this->mPerRow
* ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING + self
::GB_BORDERS
);
220 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : "";
221 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
224 $attribs = Sanitizer
::mergeAttributes(
225 array( 'class' => 'gallery' ), $this->mAttribs
);
227 $s = Xml
::openElement( 'ul', $attribs );
228 if ( $this->mCaption
) {
229 $s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
232 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
233 # Output each image...
234 foreach ( $this->mImages
as $pair ) {
236 $text = $pair[1]; # "text" means "caption" here
239 if ( $nt->getNamespace() == NS_FILE
) {
241 if ( $this->mParser
instanceof Parser
) {
242 # Give extensions a chance to select the file revision for us
243 $time = $sha1 = false;
244 wfRunHooks( 'BeforeParserFetchFileAndTitle',
245 array( $this->mParser
, $nt, &$time, &$sha1, &$descQuery ) );
246 # Fetch and register the file (file title may be different via hooks)
247 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $time, $sha1 );
249 $img = wfFindFile( $nt );
256 # We're dealing with a non-image, spit out the name and be done with it.
257 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
258 . htmlspecialchars( $nt->getText() ) . '</div>';
259 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
260 # The image is blacklisted, just show it as a text link.
261 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">' .
264 htmlspecialchars( $nt->getText() ),
267 array( 'known', 'noclasses' )
270 } elseif( !( $thumb = $img->transform( $params ) ) ) {
271 # Error generating thumbnail.
272 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
273 . htmlspecialchars( $img->getLastError() ) . '</div>';
275 //We get layout problems with the margin, if the image is smaller
276 //than the line-height, so we less margin in these cases.
277 $minThumbHeight = $thumb->height
> 17 ?
$thumb->height
: 17;
278 $vpad = floor(( self
::THUMB_PADDING +
$this->mHeights
- $minThumbHeight ) /2);
281 $imageParameters = array(
283 'desc-query' => $descQuery
285 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
287 $imageParameters['alt'] = $nt->getText();
290 # Set both fixed width and min-height.
291 $thumbhtml = "\n\t\t\t".
292 '<div class="thumb" style="width: ' .($this->mWidths + self
::THUMB_PADDING
).'px;">'
293 # Auto-margin centering for block-level elements. Needed now that we have video
294 # handlers since they may emit block-level elements as opposed to simple <img> tags.
295 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
296 . '<div style="margin:'.$vpad.'px auto;">'
297 . $thumb->toHtml( $imageParameters ) . '</div></div>';
299 // Call parser transform hook
300 if ( $this->mParser
&& $img->getHandler() ) {
301 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
306 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
307 // $ul = Linker::link( $linkTarget, $ut );
309 if( $this->mShowBytes
) {
311 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
312 $wgLang->formatNum( $img->getSize() ) );
314 $nb = wfMsgHtml( 'filemissing' );
321 $textlink = $this->mShowFilename ?
324 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength
) ),
327 array( 'known', 'noclasses' )
331 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
332 # in version 4.8.6 generated crackpot html in its absence, see:
333 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
335 # Weird double wrapping in div needed due to FF2 bug
336 # Can be safely removed if FF2 falls completely out of existance
338 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
339 . '<div style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
341 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
342 . $textlink . $text . $nb
344 . "\n\t\t</div></li>";
352 * @return Integer: number of images in the gallery
354 public function count() {
355 return count( $this->mImages
);
359 * Set the contextual title
361 * @param $title Title: contextual title
363 public function setContextTitle( $title ) {
364 $this->contextTitle
= $title;
368 * Get the contextual title, if applicable
370 * @return mixed Title or false
372 public function getContextTitle() {
373 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
374 ?
$this->contextTitle