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;
19 * Hide blacklisted images?
24 * Registered parser object for output callbacks
30 * Contextual title, used when images are being screened
31 * against the bad image list
33 private $contextTitle = false;
35 private $mAttribs = array();
40 const THUMB_PADDING
= 30;
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
131 function useSkin( $skin ) {
132 $this->mSkin
= $skin;
136 * Return the skin that should be used
138 * @return Skin object
141 if( !$this->mSkin
) {
143 $skin = $wgUser->getSkin();
145 $skin = $this->mSkin
;
151 * Add an image to 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 add( $title, $html='' ) {
157 if ( $title instanceof File
) {
158 // Old calling convention
159 $title = $title->getTitle();
161 $this->mImages
[] = array( $title, $html );
162 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
166 * Add an image at the beginning of the gallery.
168 * @param $title Title object of the image that is added to the gallery
169 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
171 function insert( $title, $html='' ) {
172 if ( $title instanceof File
) {
173 // Old calling convention
174 $title = $title->getTitle();
176 array_unshift( $this->mImages
, array( &$title, $html ) );
181 * isEmpty() returns true if the gallery contains no images
184 return empty( $this->mImages
);
188 * Enable/Disable showing of the file size of an image in the gallery.
189 * Enabled by default.
191 * @param $f Boolean: set to false to disable.
193 function setShowBytes( $f ) {
194 $this->mShowBytes
= (bool)$f;
198 * Enable/Disable showing of the filename of an image in the gallery.
199 * Enabled by default.
201 * @param $f Boolean: set to false to disable.
203 function setShowFilename( $f ) {
204 $this->mShowFilename
= (bool)$f;
208 * Set arbitrary attributes to go on the HTML gallery output element.
209 * Should be suitable for a <ul> element.
211 * Note -- if taking from user input, you should probably run through
212 * Sanitizer::validateAttributes() first.
214 * @param $attribs Array of HTML attribute pairs
216 function setAttributes( $attribs ) {
217 $this->mAttribs
= $attribs;
221 * Return a HTML representation of the image gallery
223 * For each image in the gallery, display
226 * - the additional text provided when adding the image
227 * - the size of the image
233 $sk = $this->getSkin();
235 if ( $this->mPerRow
> 0 ) {
236 $maxwidth = $this->mPerRow
* ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING + self
::GB_BORDERS
);
237 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : "";
238 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
241 $attribs = Sanitizer
::mergeAttributes(
242 array( 'class' => 'gallery' ), $this->mAttribs
);
244 $s = Xml
::openElement( 'ul', $attribs );
245 if ( $this->mCaption
) {
246 $s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
249 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
250 # Output each image...
251 foreach ( $this->mImages
as $pair ) {
253 $text = $pair[1]; # "text" means "caption" here
256 if ( $nt->getNamespace() == NS_FILE
) {
258 if ( $this->mParser
instanceof Parser
) {
259 # Give extensions a chance to select the file revision for us
260 $time = $sha1 = false;
261 wfRunHooks( 'BeforeParserFetchFileAndTitle',
262 array( &$this->mParser
, &$nt, &$time, &$sha1, &$descQuery ) );
263 # Fetch and register the file (file title may be different via hooks)
264 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $time, $sha1 );
266 $img = wfFindFile( $nt );
273 # We're dealing with a non-image, spit out the name and be done with it.
274 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
275 . htmlspecialchars( $nt->getText() ) . '</div>';
276 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
277 # The image is blacklisted, just show it as a text link.
278 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">' .
281 htmlspecialchars( $nt->getText() ),
284 array( 'known', 'noclasses' )
287 } elseif( !( $thumb = $img->transform( $params ) ) ) {
288 # Error generating thumbnail.
289 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
290 . htmlspecialchars( $img->getLastError() ) . '</div>';
292 //We get layout problems with the margin, if the image is smaller
293 //than the line-height, so we less margin in these cases.
294 $minThumbHeight = $thumb->height
> 17 ?
$thumb->height
: 17;
295 $vpad = floor(( self
::THUMB_PADDING +
$this->mHeights
- $minThumbHeight ) /2);
298 $imageParameters = array(
300 'desc-query' => $descQuery
302 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
304 $imageParameters['alt'] = $nt->getText();
307 # Set both fixed width and min-height.
308 $thumbhtml = "\n\t\t\t".
309 '<div class="thumb" style="width: ' .($this->mWidths + self
::THUMB_PADDING
).'px;">'
310 # Auto-margin centering for block-level elements. Needed now that we have video
311 # handlers since they may emit block-level elements as opposed to simple <img> tags.
312 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
313 . '<div style="margin:'.$vpad.'px auto;">'
314 . $thumb->toHtml( $imageParameters ) . '</div></div>';
316 // Call parser transform hook
317 if ( $this->mParser
&& $img->getHandler() ) {
318 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
323 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
324 // $ul = $sk->link( $linkTarget, $ut );
326 if( $this->mShowBytes
) {
328 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
329 $wgLang->formatNum( $img->getSize() ) );
331 $nb = wfMsgHtml( 'filemissing' );
338 $textlink = $this->mShowFilename ?
341 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength
) ),
344 array( 'known', 'noclasses' )
348 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
349 # in version 4.8.6 generated crackpot html in its absence, see:
350 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
352 # Weird double wrapping in div needed due to FF2 bug
353 # Can be safely removed if FF2 falls completely out of existance
355 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
356 . '<div style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
358 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
359 . $textlink . $text . $nb
361 . "\n\t\t</div></li>";
369 * @return Integer: number of images in the gallery
371 public function count() {
372 return count( $this->mImages
);
376 * Set the contextual title
378 * @param $title Title: contextual title
380 public function setContextTitle( $title ) {
381 $this->contextTitle
= $title;
385 * Get the contextual title, if applicable
387 * @return mixed Title or false
389 public function getContextTitle() {
390 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
391 ?
$this->contextTitle