(bug 33321. Sort of) Adding a line to MediaWiki:Sidebar that contains a pipe, but...
[mediawiki.git] / includes / ImageGallery.php
blob09f9ba883cb05382ba038eac000da9ebc956d89f
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
5 /**
6 * Image gallery
8 * Add images to the gallery using add(), then render that list to HTML using toHTML().
10 * @ingroup Media
12 class ImageGallery {
13 var $mImages, $mShowBytes, $mShowFilename;
14 var $mCaption = false;
16 /**
17 * Hide blacklisted images?
19 var $mHideBadImages;
21 /**
22 * Registered parser object for output callbacks
23 * @var Parser
25 var $mParser;
27 /**
28 * Contextual title, used when images are being screened
29 * against the bad image list
31 protected $contextTitle = false;
33 protected $mAttribs = array();
35 /**
36 * Fixed margins
38 const THUMB_PADDING = 30;
39 const GB_PADDING = 5;
40 // 2px borders on each side + 2px implied padding on each side
41 const GB_BORDERS = 8;
43 /**
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'];
59 /**
60 * Register a parser object
62 * @param $parser Parser
64 function setParser( $parser ) {
65 $this->mParser = $parser;
68 /**
69 * Set bad image flag
71 function setHideBadImages( $flag = true ) {
72 $this->mHideBadImages = $flag;
75 /**
76 * Set the caption (as plain text)
78 * @param $caption Caption
80 function setCaption( $caption ) {
81 $this->mCaption = htmlspecialchars( $caption );
84 /**
85 * Set the caption (as HTML)
87 * @param $caption String: Caption
89 public function setCaptionHtml( $caption ) {
90 $this->mCaption = $caption;
93 /**
94 * Set how many images will be displayed per row.
96 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
97 * invalid numbers will be rejected
99 public function setPerRow( $num ) {
100 if ( $num >= 0 ) {
101 $this->mPerRow = (int)$num;
106 * Set how wide each image will be, in pixels.
108 * @param $num Integer > 0; invalid numbers will be ignored
110 public function setWidths( $num ) {
111 if ( $num > 0 ) {
112 $this->mWidths = (int)$num;
117 * Set how high each image will be, in pixels.
119 * @param $num Integer > 0; invalid numbers will be ignored
121 public function setHeights( $num ) {
122 if ( $num > 0 ) {
123 $this->mHeights = (int)$num;
128 * Instruct the class to use a specific skin for rendering
130 * @param $skin Skin object
131 * @deprecated since 1.18 Not used anymore
133 function useSkin( $skin ) {
134 wfDeprecated( __METHOD__, '1.18' );
135 /* no op */
139 * Add an image to the gallery.
141 * @param $title Title object of the image that is added to the gallery
142 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
143 * @param $alt String: Alt text for the image
145 function add( $title, $html = '', $alt = '' ) {
146 if ( $title instanceof File ) {
147 // Old calling convention
148 $title = $title->getTitle();
150 $this->mImages[] = array( $title, $html, $alt );
151 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
155 * Add an image at the beginning of the gallery.
157 * @param $title Title object of the image that is added to the gallery
158 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
159 * @param $alt String: Alt text for the image
161 function insert( $title, $html = '', $alt = '' ) {
162 if ( $title instanceof File ) {
163 // Old calling convention
164 $title = $title->getTitle();
166 array_unshift( $this->mImages, array( &$title, $html, $alt ) );
170 * isEmpty() returns true if the gallery contains no images
172 function isEmpty() {
173 return empty( $this->mImages );
177 * Enable/Disable showing of the file size of an image in the gallery.
178 * Enabled by default.
180 * @param $f Boolean: set to false to disable.
182 function setShowBytes( $f ) {
183 $this->mShowBytes = (bool)$f;
187 * Enable/Disable showing of the filename of an image in the gallery.
188 * Enabled by default.
190 * @param $f Boolean: set to false to disable.
192 function setShowFilename( $f ) {
193 $this->mShowFilename = (bool)$f;
197 * Set arbitrary attributes to go on the HTML gallery output element.
198 * Should be suitable for a <ul> element.
200 * Note -- if taking from user input, you should probably run through
201 * Sanitizer::validateAttributes() first.
203 * @param $attribs Array of HTML attribute pairs
205 function setAttributes( $attribs ) {
206 $this->mAttribs = $attribs;
210 * Return a HTML representation of the image gallery
212 * For each image in the gallery, display
213 * - a thumbnail
214 * - the image name
215 * - the additional text provided when adding the image
216 * - the size of the image
219 function toHTML() {
220 global $wgLang;
222 if ( $this->mPerRow > 0 ) {
223 $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS );
224 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : '';
225 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
226 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
229 $attribs = Sanitizer::mergeAttributes(
230 array( 'class' => 'gallery' ), $this->mAttribs );
232 $output = Xml::openElement( 'ul', $attribs );
233 if ( $this->mCaption ) {
234 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
237 $params = array(
238 'width' => $this->mWidths,
239 'height' => $this->mHeights
241 # Output each image...
242 foreach ( $this->mImages as $pair ) {
243 $nt = $pair[0];
244 $text = $pair[1]; # "text" means "caption" here
245 $alt = $pair[2];
247 $descQuery = false;
248 if ( $nt->getNamespace() == NS_FILE ) {
249 # Get the file...
250 if ( $this->mParser instanceof Parser ) {
251 # Give extensions a chance to select the file revision for us
252 $options = array();
253 wfRunHooks( 'BeforeParserFetchFileAndTitle',
254 array( $this->mParser, $nt, &$options, &$descQuery ) );
255 # Fetch and register the file (file title may be different via hooks)
256 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
257 } else {
258 $img = wfFindFile( $nt );
260 } else {
261 $img = false;
264 if( !$img ) {
265 # We're dealing with a non-image, spit out the name and be done with it.
266 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">'
267 . htmlspecialchars( $nt->getText() ) . '</div>';
268 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
269 # The image is blacklisted, just show it as a text link.
270 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' .
271 Linker::link(
272 $nt,
273 htmlspecialchars( $nt->getText() ),
274 array(),
275 array(),
276 array( 'known', 'noclasses' )
278 '</div>';
279 } elseif( !( $thumb = $img->transform( $params ) ) ) {
280 # Error generating thumbnail.
281 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">'
282 . htmlspecialchars( $img->getLastError() ) . '</div>';
283 } else {
284 $vpad = ( self::THUMB_PADDING + $this->mHeights - $thumb->height ) /2;
286 $imageParameters = array(
287 'desc-link' => true,
288 'desc-query' => $descQuery,
289 'alt' => $alt,
291 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
292 if ( $alt == '' && $text == '' ) {
293 $imageParameters['alt'] = $nt->getText();
296 # Set both fixed width and min-height.
297 $thumbhtml = "\n\t\t\t" .
298 '<div class="thumb" style="width: ' . ( $this->mWidths + self::THUMB_PADDING ) . 'px;">'
299 # Auto-margin centering for block-level elements. Needed now that we have video
300 # handlers since they may emit block-level elements as opposed to simple <img> tags.
301 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
302 . '<div style="margin:' . $vpad . 'px auto;">'
303 . $thumb->toHtml( $imageParameters ) . '</div></div>';
305 // Call parser transform hook
306 if ( $this->mParser && $img->getHandler() ) {
307 $img->getHandler()->parserTransformHook( $this->mParser, $img );
311 //TODO
312 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
313 // $ul = Linker::link( $linkTarget, $ut );
315 if( $this->mShowBytes ) {
316 if( $img ) {
317 $fileSize = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
318 $wgLang->formatNum( $img->getSize() ) );
319 } else {
320 $fileSize = wfMsgHtml( 'filemissing' );
322 $fileSize = "$fileSize<br />\n";
323 } else {
324 $fileSize = '';
327 $textlink = $this->mShowFilename ?
328 Linker::link(
329 $nt,
330 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength ) ),
331 array(),
332 array(),
333 array( 'known', 'noclasses' )
334 ) . "<br />\n" :
335 '' ;
337 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
338 # in version 4.8.6 generated crackpot html in its absence, see:
339 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
341 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
342 # Can be safely removed if FF2 falls completely out of existance
343 $output .=
344 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
345 . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
346 . $thumbhtml
347 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
348 . $textlink . $text . $fileSize
349 . "\n\t\t\t</div>"
350 . "\n\t\t</div></li>";
352 $output .= "\n</ul>";
354 return $output;
358 * @return Integer: number of images in the gallery
360 public function count() {
361 return count( $this->mImages );
365 * Set the contextual title
367 * @param $title Title: contextual title
369 public function setContextTitle( $title ) {
370 $this->contextTitle = $title;
374 * Get the contextual title, if applicable
376 * @return mixed Title or false
378 public function getContextTitle() {
379 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
380 ? $this->contextTitle
381 : false;
384 } //class