8c13d255f252a5b24c25854209954b78782dc9ff
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
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
130 function useSkin( $skin ) {
131 $this->mSkin
= $skin;
135 * Return the skin that should be used
137 * @return Skin object
140 if( !$this->mSkin
) {
142 $skin = $wgUser->getSkin();
144 $skin = $this->mSkin
;
150 * Add an image to the gallery.
152 * @param $title Title object of the image that is added to the gallery
153 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
155 function add( $title, $html='' ) {
156 if ( $title instanceof File
) {
157 // Old calling convention
158 $title = $title->getTitle();
160 $this->mImages
[] = array( $title, $html );
161 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
165 * Add an image at the beginning of the gallery.
167 * @param $title Title object of the image that is added to the gallery
168 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
170 function insert( $title, $html='' ) {
171 if ( $title instanceof File
) {
172 // Old calling convention
173 $title = $title->getTitle();
175 array_unshift( $this->mImages
, array( &$title, $html ) );
180 * isEmpty() returns true if the gallery contains no images
183 return empty( $this->mImages
);
187 * Enable/Disable showing of the file size of an image in the gallery.
188 * Enabled by default.
190 * @param $f Boolean: set to false to disable.
192 function setShowBytes( $f ) {
193 $this->mShowBytes
= (bool)$f;
197 * Enable/Disable showing of the filename of an image in the gallery.
198 * Enabled by default.
200 * @param $f Boolean: set to false to disable.
202 function setShowFilename( $f ) {
203 $this->mShowFilename
= (bool)$f;
207 * Set arbitrary attributes to go on the HTML gallery output element.
208 * Should be suitable for a <ul> element.
210 * Note -- if taking from user input, you should probably run through
211 * Sanitizer::validateAttributes() first.
213 * @param $attribs Array of HTML attribute pairs
215 function setAttributes( $attribs ) {
216 $this->mAttribs
= $attribs;
220 * Return a HTML representation of the image gallery
222 * For each image in the gallery, display
225 * - the additional text provided when adding the image
226 * - the size of the image
232 $sk = $this->getSkin();
234 if ( $this->mPerRow
> 0 ) {
235 $maxwidth = $this->mPerRow
* ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING + self
::GB_BORDERS
);
236 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : "";
237 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
240 $attribs = Sanitizer
::mergeAttributes(
242 'class' => 'gallery'),
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
);
251 foreach ( $this->mImages
as $pair ) {
253 $text = $pair[1]; # "text" means "caption" here
255 if ( $nt->getNamespace() == NS_FILE
) {
256 # Give extensions a chance to select the file revision for us
257 $time = $sha1 = $descQuery = false;
258 wfRunHooks( 'BeforeGalleryFindFile',
259 array( &$this, &$nt, &$time, &$descQuery, &$sha1 ) );
260 # Get the file and register it
261 $img = $this->mParser
->fetchFile( $nt, $time, $sha1 );
263 $nt = $img->getTitle(); // file title may be different (via hooks)
270 # We're dealing with a non-image, spit out the name and be done with it.
271 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
272 . htmlspecialchars( $nt->getText() ) . '</div>';
273 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
274 # The image is blacklisted, just show it as a text link.
275 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">' .
278 htmlspecialchars( $nt->getText() ),
281 array( 'known', 'noclasses' )
284 } elseif( !( $thumb = $img->transform( $params ) ) ) {
285 # Error generating thumbnail.
286 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self
::THUMB_PADDING +
$this->mHeights
).'px;">'
287 . htmlspecialchars( $img->getLastError() ) . '</div>';
289 //We get layout problems with the margin, if the image is smaller
290 //than the line-height, so we less margin in these cases.
291 $minThumbHeight = $thumb->height
> 17 ?
$thumb->height
: 17;
292 $vpad = floor(( self
::THUMB_PADDING +
$this->mHeights
- $minThumbHeight ) /2);
295 $imageParameters = array(
297 'desc-query' => $descQuery
299 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
301 $imageParameters['alt'] = $nt->getText();
304 # Set both fixed width and min-height.
305 $thumbhtml = "\n\t\t\t".
306 '<div class="thumb" style="width: ' .($this->mWidths + self
::THUMB_PADDING
).'px;">'
307 # Auto-margin centering for block-level elements. Needed now that we have video
308 # handlers since they may emit block-level elements as opposed to simple <img> tags.
309 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
310 . '<div style="margin:'.$vpad.'px auto;">'
311 . $thumb->toHtml( $imageParameters ) . '</div></div>';
313 // Call parser transform hook
314 if ( $this->mParser
&& $img->getHandler() ) {
315 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
320 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
321 // $ul = $sk->link( $linkTarget, $ut );
323 if( $this->mShowBytes
) {
325 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
326 $wgLang->formatNum( $img->getSize() ) );
328 $nb = wfMsgHtml( 'filemissing' );
335 $textlink = $this->mShowFilename ?
338 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength
) ),
341 array( 'known', 'noclasses' )
345 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
346 # in version 4.8.6 generated crackpot html in its absence, see:
347 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
349 # Weird double wrapping in div needed due to FF2 bug
350 # Can be safely removed if FF2 falls completely out of existance
352 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
353 . '<div style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
355 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
356 . $textlink . $text . $nb
358 . "\n\t\t</div></li>";
367 * @return Integer: number of images in the gallery
369 public function count() {
370 return count( $this->mImages
);
374 * Set the contextual title
376 * @param $title Title: contextual title
378 public function setContextTitle( $title ) {
379 $this->contextTitle
= $title;
383 * Get the contextual title, if applicable
385 * @return mixed Title or false
387 public function getContextTitle() {
388 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
389 ?
$this->contextTitle