Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / includes / ImageGallery.php
blob8a38bed7289318d03a6da9f9c3e45890060a9bae
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
14 var $mImages, $mShowBytes, $mShowFilename;
15 var $mCaption = false;
16 var $mSkin = false;
17 var $mRevisionId = 0;
19 /**
20 * Hide blacklisted images?
22 var $mHideBadImages;
24 /**
25 * Registered parser object for output callbacks
27 var $mParser;
29 /**
30 * Contextual title, used when images are being screened
31 * against the bad image list
33 private $contextTitle = false;
35 private $mPerRow = 4; // How many images wide should the gallery be?
36 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
38 private $mAttribs = array();
40 /**
41 * Create a new image gallery object.
43 function __construct( ) {
44 $this->mImages = array();
45 $this->mShowBytes = true;
46 $this->mShowFilename = true;
47 $this->mParser = false;
48 $this->mHideBadImages = false;
51 /**
52 * Register a parser object
54 function setParser( $parser ) {
55 $this->mParser = $parser;
58 /**
59 * Set bad image flag
61 function setHideBadImages( $flag = true ) {
62 $this->mHideBadImages = $flag;
65 /**
66 * Set the caption (as plain text)
68 * @param $caption Caption
70 function setCaption( $caption ) {
71 $this->mCaption = htmlspecialchars( $caption );
74 /**
75 * Set the caption (as HTML)
77 * @param $caption Caption
79 public function setCaptionHtml( $caption ) {
80 $this->mCaption = $caption;
83 /**
84 * Set how many images will be displayed per row.
86 * @param int $num > 0; invalid numbers will be rejected
88 public function setPerRow( $num ) {
89 if ($num > 0) {
90 $this->mPerRow = (int)$num;
94 /**
95 * Set how wide each image will be, in pixels.
97 * @param int $num > 0; invalid numbers will be ignored
99 public function setWidths( $num ) {
100 if ($num > 0) {
101 $this->mWidths = (int)$num;
106 * Set how high each image will be, in pixels.
108 * @param int $num > 0; invalid numbers will be ignored
110 public function setHeights( $num ) {
111 if ($num > 0) {
112 $this->mHeights = (int)$num;
117 * Instruct the class to use a specific skin for rendering
119 * @param $skin Skin object
121 function useSkin( $skin ) {
122 $this->mSkin = $skin;
126 * Return the skin that should be used
128 * @return Skin object
130 function getSkin() {
131 if( !$this->mSkin ) {
132 global $wgUser;
133 $skin = $wgUser->getSkin();
134 } else {
135 $skin = $this->mSkin;
137 return $skin;
141 * Add an image to the gallery.
143 * @param $title Title object of the image that is added to the gallery
144 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
146 function add( $title, $html='' ) {
147 if ( $title instanceof File ) {
148 // Old calling convention
149 $title = $title->getTitle();
151 $this->mImages[] = array( $title, $html );
152 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
156 * Add an image at the beginning of the gallery.
158 * @param $title Title object of the image that is added to the gallery
159 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
161 function insert( $title, $html='' ) {
162 if ( $title instanceof File ) {
163 // Old calling convention
164 $title = $title->getTitle();
166 array_unshift( $this->mImages, array( &$title, $html ) );
171 * isEmpty() returns true if the gallery contains no images
173 function isEmpty() {
174 return empty( $this->mImages );
178 * Enable/Disable showing of the file size of an image in the gallery.
179 * Enabled by default.
181 * @param $f Boolean: set to false to disable.
183 function setShowBytes( $f ) {
184 $this->mShowBytes = ( $f == true);
188 * Enable/Disable showing of the filename of an image in the gallery.
189 * Enabled by default.
191 * @param $f Boolean: set to false to disable.
193 function setShowFilename( $f ) {
194 $this->mShowFilename = ( $f == true);
198 * Set arbitrary attributes to go on the HTML gallery output element.
199 * Should be suitable for a &lt;table&gt; element.
201 * Note -- if taking from user input, you should probably run through
202 * Sanitizer::validateAttributes() first.
204 * @param array of HTML attribute pairs
206 function setAttributes( $attribs ) {
207 $this->mAttribs = $attribs;
211 * Return a HTML representation of the image gallery
213 * For each image in the gallery, display
214 * - a thumbnail
215 * - the image name
216 * - the additional text provided when adding the image
217 * - the size of the image
220 function toHTML() {
221 global $wgLang;
223 $sk = $this->getSkin();
225 $attribs = Sanitizer::mergeAttributes(
226 array(
227 'class' => 'gallery',
228 'cellspacing' => '0',
229 'cellpadding' => '0' ),
230 $this->mAttribs );
231 $s = Xml::openElement( 'table', $attribs );
232 if( $this->mCaption )
233 $s .= "\n\t<caption>{$this->mCaption}</caption>";
235 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
236 $i = 0;
237 foreach ( $this->mImages as $pair ) {
238 $nt = $pair[0];
239 $text = $pair[1];
241 # Give extensions a chance to select the file revision for us
242 $time = $descQuery = false;
243 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
245 $img = wfFindFile( $nt, $time );
247 if( $nt->getNamespace() != NS_FILE || !$img ) {
248 # We're dealing with a non-image, spit out the name and be done with it.
249 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
250 . htmlspecialchars( $nt->getText() ) . '</div>';
251 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
252 # The image is blacklisted, just show it as a text link.
253 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
254 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
255 } elseif( !( $thumb = $img->transform( $params ) ) ) {
256 # Error generating thumbnail.
257 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
258 . htmlspecialchars( $img->getLastError() ) . '</div>';
259 } else {
260 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
262 $thumbhtml = "\n\t\t\t".
263 '<div class="thumb" style="padding: ' . $vpad . 'px 0; width: ' .($this->mWidths+30).'px;">'
264 # Auto-margin centering for block-level elements. Needed now that we have video
265 # handlers since they may emit block-level elements as opposed to simple <img> tags.
266 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
267 . '<div style="margin-left: auto; margin-right: auto; width: ' .$this->mWidths.'px;">'
268 . $thumb->toHtml( array( 'desc-link' => true, 'desc-query' => $descQuery ) ) . '</div></div>';
270 // Call parser transform hook
271 if ( $this->mParser && $img->getHandler() ) {
272 $img->getHandler()->parserTransformHook( $this->mParser, $img );
276 //TODO
277 //$ul = $sk->makeLink( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}", $ut );
279 if( $this->mShowBytes ) {
280 if( $img ) {
281 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
282 $wgLang->formatNum( $img->getSize() ) );
283 } else {
284 $nb = wfMsgHtml( 'filemissing' );
286 $nb = "$nb<br />\n";
287 } else {
288 $nb = '';
291 $textlink = $this->mShowFilename ?
292 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20 ) ) ) . "<br />\n" :
293 '' ;
295 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
296 # in version 4.8.6 generated crackpot html in its absence, see:
297 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
299 if ( $i % $this->mPerRow == 0 ) {
300 $s .= "\n\t<tr>";
302 $s .=
303 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths+35).'px;">'
304 . $thumbhtml
305 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
306 . $textlink . $text . $nb
307 . "\n\t\t\t</div>"
308 . "\n\t\t</div></td>";
309 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
310 $s .= "\n\t</tr>";
312 ++$i;
314 if( $i % $this->mPerRow != 0 ) {
315 $s .= "\n\t</tr>";
317 $s .= "\n</table>";
319 return $s;
323 * @return int Number of images in the gallery
325 public function count() {
326 return count( $this->mImages );
330 * Set the contextual title
332 * @param Title $title Contextual title
334 public function setContextTitle( $title ) {
335 $this->contextTitle = $title;
339 * Get the contextual title, if applicable
341 * @return mixed Title or false
343 public function getContextTitle() {
344 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
345 ? $this->contextTitle
346 : false;
349 } //class