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 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : "";
231 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
234 $attribs = Sanitizer
::mergeAttributes(
236 'class' => 'gallery'),
238 $s = Xml
::openElement( 'ul', $attribs );
239 if ( $this->mCaption
) {
240 $s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
243 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
245 foreach ( $this->mImages
as $pair ) {
247 $text = $pair[1]; # "text" means "caption" here
249 # Give extensions a chance to select the file revision for us
250 $time = $descQuery = false;
251 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
253 if ( $nt->getNamespace() == NS_FILE
) {
254 $img = wfFindFile( $nt, array( 'time' => $time ) );
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: '.(30 +
$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: '.(30 +
$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: '.(30 +
$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, so we less margin in these cases.
281 $minThumbHeight = $thumb->height
> 17 ?
$thumb->height
: 17;
282 $vpad = floor(( 30 +
$this->mHeights
- $minThumbHeight ) /2);
285 $imageParameters = array(
287 'desc-query' => $descQuery
289 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
291 $imageParameters['alt'] = $nt->getText();
294 # Set both fixed width and min-height.
295 $thumbhtml = "\n\t\t\t".
296 '<div class="thumb" style="width: ' .($this->mWidths+
30).'px;">'
297 # Auto-margin centering for block-level elements. Needed now that we have video
298 # handlers since they may emit block-level elements as opposed to simple <img> tags.
299 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
300 . '<div style="margin:'.$vpad.'px auto;">'
301 . $thumb->toHtml( $imageParameters ) . '</div></div>';
303 // Call parser transform hook
304 if ( $this->mParser
&& $img->getHandler() ) {
305 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
310 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
311 // $ul = $sk->link( $linkTarget, $ut );
313 if( $this->mShowBytes
) {
315 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
316 $wgLang->formatNum( $img->getSize() ) );
318 $nb = wfMsgHtml( 'filemissing' );
325 $textlink = $this->mShowFilename ?
328 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength
) ),
331 array( 'known', 'noclasses' )
335 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
336 # in version 4.8.6 generated crackpot html in its absence, see:
337 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
339 # Weird double wrapping in div needed due to FF2 bug
340 # Can be safely removed if FF2 falls completely out of existance
342 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths +
35 ) . 'px">'
343 . '<div style="width: ' . ( $this->mWidths +
35 ) . 'px">'
345 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
346 . $textlink . $text . $nb
348 . "\n\t\t</div></li>";
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