Use actual arrows, not entities.
[mediawiki.git] / includes / ImageGallery.php
blob0fe58bda09e2fe4fcc449d2fac576ccf35e30d30
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
5 /**
6 */
8 /**
9 * Image gallery
11 * Add images to the gallery using add(), then render that list to HTML using toHTML().
13 * @addtogroup Media
15 class ImageGallery
17 var $mImages, $mShowBytes, $mShowFilename;
18 var $mCaption = false;
19 var $mSkin = false;
20 var $mRevisionId = 0;
22 /**
23 * Hide blacklisted images?
25 var $mHideBadImages;
27 /**
28 * Registered parser object for output callbacks
30 var $mParser;
32 /**
33 * Contextual title, used when images are being screened
34 * against the bad image list
36 private $contextTitle = false;
38 private $mPerRow = 4; // How many images wide should the gallery be?
39 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
41 private $mAttribs = array();
43 /**
44 * Create a new image gallery object.
46 function __construct( ) {
47 $this->mImages = array();
48 $this->mShowBytes = true;
49 $this->mShowFilename = true;
50 $this->mParser = false;
51 $this->mHideBadImages = false;
54 /**
55 * Register a parser object
57 function setParser( $parser ) {
58 $this->mParser = $parser;
61 /**
62 * Set bad image flag
64 function setHideBadImages( $flag = true ) {
65 $this->mHideBadImages = $flag;
68 /**
69 * Set the caption (as plain text)
71 * @param $caption Caption
73 function setCaption( $caption ) {
74 $this->mCaption = htmlspecialchars( $caption );
77 /**
78 * Set the caption (as HTML)
80 * @param $caption Caption
82 public function setCaptionHtml( $caption ) {
83 $this->mCaption = $caption;
86 /**
87 * Set how many images will be displayed per row.
89 * @param int $num > 0; invalid numbers will be rejected
91 public function setPerRow( $num ) {
92 if ($num > 0) {
93 $this->mPerRow = (int)$num;
97 /**
98 * Set how wide each image will be, in pixels.
100 * @param int $num > 0; invalid numbers will be ignored
102 public function setWidths( $num ) {
103 if ($num > 0) {
104 $this->mWidths = (int)$num;
109 * Set how high each image will be, in pixels.
111 * @param int $num > 0; invalid numbers will be ignored
113 public function setHeights( $num ) {
114 if ($num > 0) {
115 $this->mHeights = (int)$num;
120 * Instruct the class to use a specific skin for rendering
122 * @param $skin Skin object
124 function useSkin( $skin ) {
125 $this->mSkin = $skin;
129 * Return the skin that should be used
131 * @return Skin object
133 function getSkin() {
134 if( !$this->mSkin ) {
135 global $wgUser;
136 $skin = $wgUser->getSkin();
137 } else {
138 $skin = $this->mSkin;
140 return $skin;
144 * Add an image to the gallery.
146 * @param $title Title object of the image that is added to the gallery
147 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
149 function add( $title, $html='' ) {
150 if ( $title instanceof File ) {
151 // Old calling convention
152 $title = $title->getTitle();
154 $this->mImages[] = array( $title, $html );
155 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
159 * Add an image at the beginning of the gallery.
161 * @param $title Title object of the image that is added to the gallery
162 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
164 function insert( $title, $html='' ) {
165 if ( $title instanceof File ) {
166 // Old calling convention
167 $title = $title->getTitle();
169 array_unshift( $this->mImages, array( &$title, $html ) );
174 * isEmpty() returns true if the gallery contains no images
176 function isEmpty() {
177 return empty( $this->mImages );
181 * Enable/Disable showing of the file size of an image in the gallery.
182 * Enabled by default.
184 * @param $f Boolean: set to false to disable.
186 function setShowBytes( $f ) {
187 $this->mShowBytes = ( $f == true);
191 * Enable/Disable showing of the filename of an image in the gallery.
192 * Enabled by default.
194 * @param $f Boolean: set to false to disable.
196 function setShowFilename( $f ) {
197 $this->mShowFilename = ( $f == true);
201 * Set arbitrary attributes to go on the HTML gallery output element.
202 * Should be suitable for a &lt;table&gt; element.
204 * Note -- if taking from user input, you should probably run through
205 * Sanitizer::validateAttributes() first.
207 * @param array of HTML attribute pairs
209 function setAttributes( $attribs ) {
210 $this->mAttribs = $attribs;
214 * Return a HTML representation of the image gallery
216 * For each image in the gallery, display
217 * - a thumbnail
218 * - the image name
219 * - the additional text provided when adding the image
220 * - the size of the image
223 function toHTML() {
224 global $wgLang;
226 $sk = $this->getSkin();
228 $attribs = Sanitizer::mergeAttributes(
229 array(
230 'class' => 'gallery',
231 'cellspacing' => '0',
232 'cellpadding' => '0' ),
233 $this->mAttribs );
234 $s = Xml::openElement( 'table', $attribs );
235 if( $this->mCaption )
236 $s .= "\n\t<caption>{$this->mCaption}</caption>";
238 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
239 $i = 0;
240 foreach ( $this->mImages as $pair ) {
241 $nt = $pair[0];
242 $text = $pair[1];
244 # Give extensions a chance to select the file revision for us
245 $time = false;
246 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time ) );
248 $img = wfFindFile( $nt, $time );
250 if( $nt->getNamespace() != NS_IMAGE || !$img ) {
251 # We're dealing with a non-image, spit out the name and be done with it.
252 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
253 . htmlspecialchars( $nt->getText() ) . '</div>';
254 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
255 # The image is blacklisted, just show it as a text link.
256 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
257 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
258 } elseif( !( $thumb = $img->transform( $params ) ) ) {
259 # Error generating thumbnail.
260 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
261 . htmlspecialchars( $img->getLastError() ) . '</div>';
262 } else {
263 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
264 $linkAttribs = array(
265 'title' => $nt->getPrefixedText(),
266 'href' => $nt->getLocalURL(),
269 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+30).'px;">'
270 . $thumb->toHtml( array(), $linkAttribs ) . '</div>';
272 // Call parser transform hook
273 if ( $this->mParser && $img->getHandler() ) {
274 $img->getHandler()->parserTransformHook( $this->mParser, $img );
278 //TODO
279 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
281 if( $this->mShowBytes ) {
282 if( $img ) {
283 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
284 $wgLang->formatNum( $img->getSize() ) );
285 } else {
286 $nb = wfMsgHtml( 'filemissing' );
288 $nb = "$nb<br />\n";
289 } else {
290 $nb = '';
293 $textlink = $this->mShowFilename ?
294 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
295 '' ;
297 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
298 # in version 4.8.6 generated crackpot html in its absence, see:
299 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
301 if ( $i % $this->mPerRow == 0 ) {
302 $s .= "\n\t<tr>";
304 $s .=
305 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths*1.25).'px;">'
306 . $thumbhtml
307 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
308 . $textlink . $text . $nb
309 . "\n\t\t\t</div>"
310 . "\n\t\t</div></td>";
311 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
312 $s .= "\n\t</tr>";
314 ++$i;
316 if( $i % $this->mPerRow != 0 ) {
317 $s .= "\n\t</tr>";
319 $s .= "\n</table>";
321 return $s;
325 * @return int Number of images in the gallery
327 public function count() {
328 return count( $this->mImages );
332 * Set the contextual title
334 * @param Title $title Contextual title
336 public function setContextTitle( $title ) {
337 $this->contextTitle = $title;
341 * Get the contextual title, if applicable
343 * @return mixed Title or false
345 public function getContextTitle() {
346 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
347 ? $this->contextTitle
348 : false;
351 } //class