5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 * Add images to the gallery using add(), then render that list to HTML using toHTML().
31 var $mImages, $mShowBytes, $mShowFilename;
32 var $mCaption = false;
35 * Hide blacklisted images?
40 * Registered parser object for output callbacks
46 * Contextual title, used when images are being screened
47 * against the bad image list
49 protected $contextTitle = false;
51 protected $mAttribs = array();
56 const THUMB_PADDING
= 30;
58 // 2px borders on each side + 2px implied padding on each side
62 * Create a new image gallery object.
64 function __construct() {
65 global $wgGalleryOptions;
66 $this->mImages
= array();
67 $this->mShowBytes
= $wgGalleryOptions['showBytes'];
68 $this->mShowFilename
= true;
69 $this->mParser
= false;
70 $this->mHideBadImages
= false;
71 $this->mPerRow
= $wgGalleryOptions['imagesPerRow'];
72 $this->mWidths
= $wgGalleryOptions['imageWidth'];
73 $this->mHeights
= $wgGalleryOptions['imageHeight'];
74 $this->mCaptionLength
= $wgGalleryOptions['captionLength'];
78 * Register a parser object
80 * @param $parser Parser
82 function setParser( $parser ) {
83 $this->mParser
= $parser;
89 function setHideBadImages( $flag = true ) {
90 $this->mHideBadImages
= $flag;
94 * Set the caption (as plain text)
96 * @param $caption string Caption
98 function setCaption( $caption ) {
99 $this->mCaption
= htmlspecialchars( $caption );
103 * Set the caption (as HTML)
105 * @param $caption String: Caption
107 public function setCaptionHtml( $caption ) {
108 $this->mCaption
= $caption;
112 * Set how many images will be displayed per row.
114 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
115 * invalid numbers will be rejected
117 public function setPerRow( $num ) {
119 $this->mPerRow
= (int)$num;
124 * Set how wide each image will be, in pixels.
126 * @param $num Integer > 0; invalid numbers will be ignored
128 public function setWidths( $num ) {
130 $this->mWidths
= (int)$num;
135 * Set how high each image will be, in pixels.
137 * @param $num Integer > 0; invalid numbers will be ignored
139 public function setHeights( $num ) {
141 $this->mHeights
= (int)$num;
146 * Instruct the class to use a specific skin for rendering
148 * @param $skin Skin object
149 * @deprecated since 1.18 Not used anymore
151 function useSkin( $skin ) {
152 wfDeprecated( __METHOD__
, '1.18' );
157 * Add an image to the gallery.
159 * @param $title Title object of the image that is added to the gallery
160 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
161 * @param $alt String: Alt text for the image
162 * @param $link String: Override image link (optional)
164 function add( $title, $html = '', $alt = '', $link = '') {
165 if ( $title instanceof File
) {
166 // Old calling convention
167 $title = $title->getTitle();
169 $this->mImages
[] = array( $title, $html, $alt, $link );
170 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
174 * Add an image at the beginning of the gallery.
176 * @param $title Title object of the image that is added to the gallery
177 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
178 * @param $alt String: Alt text for the image
180 function insert( $title, $html = '', $alt = '' ) {
181 if ( $title instanceof File
) {
182 // Old calling convention
183 $title = $title->getTitle();
185 array_unshift( $this->mImages
, array( &$title, $html, $alt ) );
189 * isEmpty() returns true if the gallery contains no images
193 return empty( $this->mImages
);
197 * Enable/Disable showing of the file size of an image in the gallery.
198 * Enabled by default.
200 * @param $f Boolean: set to false to disable.
202 function setShowBytes( $f ) {
203 $this->mShowBytes
= (bool)$f;
207 * Enable/Disable showing of the filename of an image in the gallery.
208 * Enabled by default.
210 * @param $f Boolean: set to false to disable.
212 function setShowFilename( $f ) {
213 $this->mShowFilename
= (bool)$f;
217 * Set arbitrary attributes to go on the HTML gallery output element.
218 * Should be suitable for a <ul> element.
220 * Note -- if taking from user input, you should probably run through
221 * Sanitizer::validateAttributes() first.
223 * @param $attribs Array of HTML attribute pairs
225 function setAttributes( $attribs ) {
226 $this->mAttribs
= $attribs;
230 * Return a HTML representation of the image gallery
232 * For each image in the gallery, display
235 * - the additional text provided when adding the image
236 * - the size of the image
243 if ( $this->mPerRow
> 0 ) {
244 $maxwidth = $this->mPerRow
* ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING + self
::GB_BORDERS
);
245 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : '';
246 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
247 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
250 $attribs = Sanitizer
::mergeAttributes(
251 array( 'class' => 'gallery' ), $this->mAttribs
);
253 $output = Xml
::openElement( 'ul', $attribs );
254 if ( $this->mCaption
) {
255 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
259 'width' => $this->mWidths
,
260 'height' => $this->mHeights
262 # Output each image...
263 foreach ( $this->mImages
as $pair ) {
265 $text = $pair[1]; # "text" means "caption" here
270 if ( $nt->getNamespace() == NS_FILE
) {
272 if ( $this->mParser
instanceof Parser
) {
273 # Give extensions a chance to select the file revision for us
275 wfRunHooks( 'BeforeParserFetchFileAndTitle',
276 array( $this->mParser
, $nt, &$options, &$descQuery ) );
277 # Fetch and register the file (file title may be different via hooks)
278 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $options );
280 $img = wfFindFile( $nt );
287 # We're dealing with a non-image, spit out the name and be done with it.
288 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self
::THUMB_PADDING +
$this->mHeights
) . 'px;">'
289 . htmlspecialchars( $nt->getText() ) . '</div>';
290 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
291 # The image is blacklisted, just show it as a text link.
292 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self
::THUMB_PADDING +
$this->mHeights
) . 'px;">' .
295 htmlspecialchars( $nt->getText() ),
298 array( 'known', 'noclasses' )
301 } elseif( !( $thumb = $img->transform( $params ) ) ) {
302 # Error generating thumbnail.
303 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self
::THUMB_PADDING +
$this->mHeights
) . 'px;">'
304 . htmlspecialchars( $img->getLastError() ) . '</div>';
306 $vpad = ( self
::THUMB_PADDING +
$this->mHeights
- $thumb->height
) /2;
308 $imageParameters = array(
310 'desc-query' => $descQuery,
312 'custom-url-link' => $link
314 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
315 if ( $alt == '' && $text == '' ) {
316 $imageParameters['alt'] = $nt->getText();
319 # Set both fixed width and min-height.
320 $thumbhtml = "\n\t\t\t" .
321 '<div class="thumb" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING
) . 'px;">'
322 # Auto-margin centering for block-level elements. Needed now that we have video
323 # handlers since they may emit block-level elements as opposed to simple <img> tags.
324 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
325 . '<div style="margin:' . $vpad . 'px auto;">'
326 . $thumb->toHtml( $imageParameters ) . '</div></div>';
328 // Call parser transform hook
329 if ( $this->mParser
&& $img->getHandler() ) {
330 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
335 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
336 // $ul = Linker::link( $linkTarget, $ut );
338 if( $this->mShowBytes
) {
340 $fileSize = htmlspecialchars( $wgLang->formatSize( $img->getSize() ) );
342 $fileSize = wfMessage( 'filemissing' )->escaped();
344 $fileSize = "$fileSize<br />\n";
349 $textlink = $this->mShowFilename ?
352 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength
) ),
355 array( 'known', 'noclasses' )
359 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
360 # in version 4.8.6 generated crackpot html in its absence, see:
361 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
363 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
364 # Can be safely removed if FF2 falls completely out of existance
366 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
367 . '<div style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
369 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
370 . $textlink . $text . $fileSize
372 . "\n\t\t</div></li>";
374 $output .= "\n</ul>";
380 * @return Integer: number of images in the gallery
382 public function count() {
383 return count( $this->mImages
);
387 * Set the contextual title
389 * @param $title Title: contextual title
391 public function setContextTitle( $title ) {
392 $this->contextTitle
= $title;
396 * Get the contextual title, if applicable
398 * @return mixed Title or false
400 public function getContextTitle() {
401 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
402 ?
$this->contextTitle