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;
20 * Hide blacklisted images?
25 * 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();
38 * Create a new image gallery object.
40 function __construct( ) {
41 global $wgGalleryOptions;
42 $this->mImages
= array();
43 $this->mShowBytes
= $wgGalleryOptions['showBytes'];
44 $this->mShowFilename
= true;
45 $this->mParser
= false;
46 $this->mHideBadImages
= false;
47 $this->mPerRow
= $wgGalleryOptions['imagesPerRow'];
48 $this->mWidths
= $wgGalleryOptions['imageWidth'];
49 $this->mHeights
= $wgGalleryOptions['imageHeight'];
50 $this->mCaptionLength
= $wgGalleryOptions['captionLength'];
54 * Register a parser object
56 function setParser( $parser ) {
57 $this->mParser
= $parser;
63 function setHideBadImages( $flag = true ) {
64 $this->mHideBadImages
= $flag;
68 * Set the caption (as plain text)
70 * @param $caption Caption
72 function setCaption( $caption ) {
73 $this->mCaption
= htmlspecialchars( $caption );
77 * Set the caption (as HTML)
79 * @param $caption String: Caption
81 public function setCaptionHtml( $caption ) {
82 $this->mCaption
= $caption;
86 * Set how many images will be displayed per row.
88 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
89 * invalid numbers will be rejected
91 public function setPerRow( $num ) {
93 $this->mPerRow
= (int)$num;
98 * Set how wide each image will be, in pixels.
100 * @param $num Integer > 0; invalid numbers will be ignored
102 public function setWidths( $num ) {
104 $this->mWidths
= (int)$num;
109 * Set how high each image will be, in pixels.
111 * @param $num Integer > 0; invalid numbers will be ignored
113 public function setHeights( $num ) {
115 $this->mHeights
= (int)$num;
120 * Instruct the class to use a specific skin for rendering
122 * @param $skin Skin object
124 function useSkin( $skin ) {
125 $this->mSkin
= $skin;
129 * Return the skin that should be used
131 * @return Skin object
134 if( !$this->mSkin
) {
136 $skin = $wgUser->getSkin();
138 $skin = $this->mSkin
;
144 * Add an image to the gallery.
146 * @param $title Title object of the image that is added to the gallery
147 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
149 function add( $title, $html='' ) {
150 if ( $title instanceof File
) {
151 // Old calling convention
152 $title = $title->getTitle();
154 $this->mImages
[] = array( $title, $html );
155 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
159 * Add an image at the beginning of the gallery.
161 * @param $title Title object of the image that is added to the gallery
162 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
164 function insert( $title, $html='' ) {
165 if ( $title instanceof File
) {
166 // Old calling convention
167 $title = $title->getTitle();
169 array_unshift( $this->mImages
, array( &$title, $html ) );
174 * isEmpty() returns true if the gallery contains no images
177 return empty( $this->mImages
);
181 * Enable/Disable showing of the file size of an image in the gallery.
182 * Enabled by default.
184 * @param $f Boolean: set to false to disable.
186 function setShowBytes( $f ) {
187 $this->mShowBytes
= (bool)$f;
191 * Enable/Disable showing of the filename of an image in the gallery.
192 * Enabled by default.
194 * @param $f Boolean: set to false to disable.
196 function setShowFilename( $f ) {
197 $this->mShowFilename
= (bool)$f;
201 * Set arbitrary attributes to go on the HTML gallery output element.
202 * Should be suitable for a <ul> element.
204 * Note -- if taking from user input, you should probably run through
205 * Sanitizer::validateAttributes() first.
207 * @param $attribs Array of HTML attribute pairs
209 function setAttributes( $attribs ) {
210 $this->mAttribs
= $attribs;
214 * Return a HTML representation of the image gallery
216 * For each image in the gallery, display
219 * - the additional text provided when adding the image
220 * - the size of the image
226 $sk = $this->getSkin();
228 if ( $this->mPerRow
> 0 ) {
229 $maxwidth = $this->mPerRow
* ( $this->mWidths +
50 );
230 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;";
233 $attribs = Sanitizer
::mergeAttributes(
235 'class' => 'gallery'),
237 $s = Xml
::openElement( 'ul', $attribs );
238 if ( $this->mCaption
) {
239 $s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
242 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
244 foreach ( $this->mImages
as $pair ) {
246 $text = $pair[1]; # "text" means "caption" here
248 # Give extensions a chance to select the file revision for us
249 $time = $descQuery = false;
250 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
252 if ( $nt->getNamespace() == NS_FILE
) {
253 $img = wfFindFile( $nt, array( 'time' => $time ) );
259 # We're dealing with a non-image, spit out the name and be done with it.
260 $thumbhtml = "\n\t\t\t".'<div style="height: '.(30 +
$this->mHeights
).'px;">'
261 . htmlspecialchars( $nt->getText() ) . '</div>';
262 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
263 # The image is blacklisted, just show it as a text link.
264 $thumbhtml = "\n\t\t\t".'<div style="height: '.(30 +
$this->mHeights
).'px;">' .
267 htmlspecialchars( $nt->getText() ),
270 array( 'known', 'noclasses' )
273 } elseif( !( $thumb = $img->transform( $params ) ) ) {
274 # Error generating thumbnail.
275 $thumbhtml = "\n\t\t\t".'<div style="height: '.(30 +
$this->mHeights
).'px;">'
276 . htmlspecialchars( $img->getLastError() ) . '</div>';
278 $vpad = floor(( 30 +
$this->mHeights
- $thumb->height
) /2);
280 $imageParameters = array(
282 'desc-query' => $descQuery
284 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
286 $imageParameters['alt'] = $nt->getText();
289 # Set both fixed width and height. Otherwise we might have problems
290 # with the vertical centering of images where height<line-size
291 $thumbhtml = "\n\t\t\t".
292 '<div class="thumb" style="width: ' .($this->mWidths+
30).'px; height: ' .($this->mHeights+
30).'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 = $sk->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 +
35 ) . 'px">'
339 . '<div style="width: ' . ( $this->mWidths +
35 ) . 'px">'
341 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
342 . $textlink . $text . $nb
344 . "\n\t\t</div></li>";
353 * @return Integer: number of images in the gallery
355 public function count() {
356 return count( $this->mImages
);
360 * Set the contextual title
362 * @param $title Title: contextual title
364 public function setContextTitle( $title ) {
365 $this->contextTitle
= $title;
369 * Get the contextual title, if applicable
371 * @return mixed Title or false
373 public function getContextTitle() {
374 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
375 ?
$this->contextTitle