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
23 if ( ! defined( 'MEDIAWIKI' ) )
29 * Add images to the gallery using add(), then render that list to HTML using toHTML().
34 var $mImages, $mShowBytes, $mShowFilename;
35 var $mCaption = false;
38 * Hide blacklisted images?
43 * Registered parser object for output callbacks
49 * Contextual title, used when images are being screened
50 * against the bad image list
52 protected $contextTitle = false;
54 protected $mAttribs = array();
59 const THUMB_PADDING
= 30;
61 // 2px borders on each side + 2px implied padding on each side
65 * Create a new image gallery object.
67 function __construct() {
68 global $wgGalleryOptions;
69 $this->mImages
= array();
70 $this->mShowBytes
= $wgGalleryOptions['showBytes'];
71 $this->mShowFilename
= true;
72 $this->mParser
= false;
73 $this->mHideBadImages
= false;
74 $this->mPerRow
= $wgGalleryOptions['imagesPerRow'];
75 $this->mWidths
= $wgGalleryOptions['imageWidth'];
76 $this->mHeights
= $wgGalleryOptions['imageHeight'];
77 $this->mCaptionLength
= $wgGalleryOptions['captionLength'];
81 * Register a parser object
83 * @param $parser Parser
85 function setParser( $parser ) {
86 $this->mParser
= $parser;
92 function setHideBadImages( $flag = true ) {
93 $this->mHideBadImages
= $flag;
97 * Set the caption (as plain text)
99 * @param $caption string Caption
101 function setCaption( $caption ) {
102 $this->mCaption
= htmlspecialchars( $caption );
106 * Set the caption (as HTML)
108 * @param $caption String: Caption
110 public function setCaptionHtml( $caption ) {
111 $this->mCaption
= $caption;
115 * Set how many images will be displayed per row.
117 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
118 * invalid numbers will be rejected
120 public function setPerRow( $num ) {
122 $this->mPerRow
= (int)$num;
127 * Set how wide each image will be, in pixels.
129 * @param $num Integer > 0; invalid numbers will be ignored
131 public function setWidths( $num ) {
133 $this->mWidths
= (int)$num;
138 * Set how high each image will be, in pixels.
140 * @param $num Integer > 0; invalid numbers will be ignored
142 public function setHeights( $num ) {
144 $this->mHeights
= (int)$num;
149 * Instruct the class to use a specific skin for rendering
151 * @param $skin Skin object
152 * @deprecated since 1.18 Not used anymore
154 function useSkin( $skin ) {
155 wfDeprecated( __METHOD__
, '1.18' );
160 * Add an image to the gallery.
162 * @param $title Title object of the image that is added to the gallery
163 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
164 * @param $alt String: Alt text for the image
165 * @param $link String: Override image link (optional)
167 function add( $title, $html = '', $alt = '', $link = '') {
168 if ( $title instanceof File
) {
169 // Old calling convention
170 $title = $title->getTitle();
172 $this->mImages
[] = array( $title, $html, $alt, $link );
173 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
177 * Add an image at the beginning of the gallery.
179 * @param $title Title object of the image that is added to the gallery
180 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
181 * @param $alt String: Alt text for the image
183 function insert( $title, $html = '', $alt = '' ) {
184 if ( $title instanceof File
) {
185 // Old calling convention
186 $title = $title->getTitle();
188 array_unshift( $this->mImages
, array( &$title, $html, $alt ) );
192 * isEmpty() returns true if the gallery contains no images
196 return empty( $this->mImages
);
200 * Enable/Disable showing of the file size of an image in the gallery.
201 * Enabled by default.
203 * @param $f Boolean: set to false to disable.
205 function setShowBytes( $f ) {
206 $this->mShowBytes
= (bool)$f;
210 * Enable/Disable showing of the filename of an image in the gallery.
211 * Enabled by default.
213 * @param $f Boolean: set to false to disable.
215 function setShowFilename( $f ) {
216 $this->mShowFilename
= (bool)$f;
220 * Set arbitrary attributes to go on the HTML gallery output element.
221 * Should be suitable for a <ul> element.
223 * Note -- if taking from user input, you should probably run through
224 * Sanitizer::validateAttributes() first.
226 * @param $attribs Array of HTML attribute pairs
228 function setAttributes( $attribs ) {
229 $this->mAttribs
= $attribs;
233 * Return a HTML representation of the image gallery
235 * For each image in the gallery, display
238 * - the additional text provided when adding the image
239 * - the size of the image
246 if ( $this->mPerRow
> 0 ) {
247 $maxwidth = $this->mPerRow
* ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING + self
::GB_BORDERS
);
248 $oldStyle = isset( $this->mAttribs
['style'] ) ?
$this->mAttribs
['style'] : '';
249 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
250 $this->mAttribs
['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
253 $attribs = Sanitizer
::mergeAttributes(
254 array( 'class' => 'gallery' ), $this->mAttribs
);
256 $output = Xml
::openElement( 'ul', $attribs );
257 if ( $this->mCaption
) {
258 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
262 'width' => $this->mWidths
,
263 'height' => $this->mHeights
265 # Output each image...
266 foreach ( $this->mImages
as $pair ) {
268 $text = $pair[1]; # "text" means "caption" here
273 if ( $nt->getNamespace() == NS_FILE
) {
275 if ( $this->mParser
instanceof Parser
) {
276 # Give extensions a chance to select the file revision for us
278 wfRunHooks( 'BeforeParserFetchFileAndTitle',
279 array( $this->mParser
, $nt, &$options, &$descQuery ) );
280 # Fetch and register the file (file title may be different via hooks)
281 list( $img, $nt ) = $this->mParser
->fetchFileAndTitle( $nt, $options );
283 $img = wfFindFile( $nt );
290 # We're dealing with a non-image, spit out the name and be done with it.
291 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self
::THUMB_PADDING +
$this->mHeights
) . 'px;">'
292 . htmlspecialchars( $nt->getText() ) . '</div>';
293 } elseif( $this->mHideBadImages
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
294 # The image is blacklisted, just show it as a text link.
295 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self
::THUMB_PADDING +
$this->mHeights
) . 'px;">' .
298 htmlspecialchars( $nt->getText() ),
301 array( 'known', 'noclasses' )
304 } elseif( !( $thumb = $img->transform( $params ) ) ) {
305 # Error generating thumbnail.
306 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self
::THUMB_PADDING +
$this->mHeights
) . 'px;">'
307 . htmlspecialchars( $img->getLastError() ) . '</div>';
309 $vpad = ( self
::THUMB_PADDING +
$this->mHeights
- $thumb->height
) /2;
311 $imageParameters = array(
313 'desc-query' => $descQuery,
315 'custom-url-link' => $link
317 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
318 if ( $alt == '' && $text == '' ) {
319 $imageParameters['alt'] = $nt->getText();
322 # Set both fixed width and min-height.
323 $thumbhtml = "\n\t\t\t" .
324 '<div class="thumb" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING
) . 'px;">'
325 # Auto-margin centering for block-level elements. Needed now that we have video
326 # handlers since they may emit block-level elements as opposed to simple <img> tags.
327 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
328 . '<div style="margin:' . $vpad . 'px auto;">'
329 . $thumb->toHtml( $imageParameters ) . '</div></div>';
331 // Call parser transform hook
332 if ( $this->mParser
&& $img->getHandler() ) {
333 $img->getHandler()->parserTransformHook( $this->mParser
, $img );
338 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
339 // $ul = Linker::link( $linkTarget, $ut );
341 if( $this->mShowBytes
) {
343 $fileSize = htmlspecialchars( $wgLang->formatSize( $img->getSize() ) );
345 $fileSize = wfMsgHtml( 'filemissing' );
347 $fileSize = "$fileSize<br />\n";
352 $textlink = $this->mShowFilename ?
355 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength
) ),
358 array( 'known', 'noclasses' )
362 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
363 # in version 4.8.6 generated crackpot html in its absence, see:
364 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
366 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
367 # Can be safely removed if FF2 falls completely out of existance
369 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
370 . '<div style="width: ' . ( $this->mWidths + self
::THUMB_PADDING + self
::GB_PADDING
) . 'px">'
372 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
373 . $textlink . $text . $fileSize
375 . "\n\t\t</div></li>";
377 $output .= "\n</ul>";
383 * @return Integer: number of images in the gallery
385 public function count() {
386 return count( $this->mImages
);
390 * Set the contextual title
392 * @param $title Title: contextual title
394 public function setContextTitle( $title ) {
395 $this->contextTitle
= $title;
399 * Get the contextual title, if applicable
401 * @return mixed Title or false
403 public function getContextTitle() {
404 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
405 ?
$this->contextTitle