* (bug 6072) Add a 'border' keyword to the image syntax
[mediawiki.git] / includes / ImageGallery.php
blobfba7714ce4453ab8bc0707e040945ec4848b1e38
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;
21 /**
22 * Is the gallery on a wiki page (i.e. not a special page)
24 var $mParsing;
26 /**
27 * Contextual title, used when images are being screened
28 * against the bad image list
30 private $contextTitle = false;
32 private $mPerRow = 4; // How many images wide should the gallery be?
33 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
35 /**
36 * Create a new image gallery object.
38 function __construct( ) {
39 $this->mImages = array();
40 $this->mShowBytes = true;
41 $this->mShowFilename = true;
42 $this->mParsing = false;
45 /**
46 * Set the "parse" bit so we know to hide "bad" images
48 function setParsing( $val = true ) {
49 $this->mParsing = $val;
52 /**
53 * Set the caption (as plain text)
55 * @param $caption Caption
57 function setCaption( $caption ) {
58 $this->mCaption = htmlspecialchars( $caption );
61 /**
62 * Set the caption (as HTML)
64 * @param $caption Caption
66 public function setCaptionHtml( $caption ) {
67 $this->mCaption = $caption;
70 /**
71 * Set how many images will be displayed per row.
73 * @param int $num > 0; invalid numbers will be rejected
75 public function setPerRow( $num ) {
76 if ($num > 0) {
77 $this->mPerRow = (int)$num;
81 /**
82 * Set how wide each image will be, in pixels.
84 * @param int $num > 0; invalid numbers will be ignored
86 public function setWidths( $num ) {
87 if ($num > 0) {
88 $this->mWidths = (int)$num;
92 /**
93 * Set how high each image will be, in pixels.
95 * @param int $num > 0; invalid numbers will be ignored
97 public function setHeights( $num ) {
98 if ($num > 0) {
99 $this->mHeights = (int)$num;
104 * Instruct the class to use a specific skin for rendering
106 * @param $skin Skin object
108 function useSkin( $skin ) {
109 $this->mSkin = $skin;
113 * Return the skin that should be used
115 * @return Skin object
117 function getSkin() {
118 if( !$this->mSkin ) {
119 global $wgUser;
120 $skin = $wgUser->getSkin();
121 } else {
122 $skin = $this->mSkin;
124 return $skin;
128 * Add an image to the gallery.
130 * @param $image Image object that is added to the gallery
131 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
133 function add( $image, $html='' ) {
134 $this->mImages[] = array( &$image, $html );
135 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
139 * Add an image at the beginning of the gallery.
141 * @param $image Image object 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.
144 function insert( $image, $html='' ) {
145 array_unshift( $this->mImages, array( &$image, $html ) );
150 * isEmpty() returns true if the gallery contains no images
152 function isEmpty() {
153 return empty( $this->mImages );
157 * Enable/Disable showing of the file size of an image in the gallery.
158 * Enabled by default.
160 * @param $f Boolean: set to false to disable.
162 function setShowBytes( $f ) {
163 $this->mShowBytes = ( $f == true);
167 * Enable/Disable showing of the filename of an image in the gallery.
168 * Enabled by default.
170 * @param $f Boolean: set to false to disable.
172 function setShowFilename( $f ) {
173 $this->mShowFilename = ( $f == true);
177 * Return a HTML representation of the image gallery
179 * For each image in the gallery, display
180 * - a thumbnail
181 * - the image name
182 * - the additional text provided when adding the image
183 * - the size of the image
186 function toHTML() {
187 global $wgLang;
189 $sk = $this->getSkin();
191 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
192 if( $this->mCaption )
193 $s .= "\n\t<caption>{$this->mCaption}</caption>";
195 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
196 $i = 0;
197 foreach ( $this->mImages as $pair ) {
198 $img =& $pair[0];
199 $text = $pair[1];
201 $nt = $img->getTitle();
203 if( $nt->getNamespace() != NS_IMAGE ) {
204 # We're dealing with a non-image, spit out the name and be done with it.
205 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
206 . htmlspecialchars( $nt->getText() ) . '</div>';
207 } elseif( $this->mParsing && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
208 # The image is blacklisted, just show it as a text link.
209 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
210 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
211 } elseif( !( $thumb = $img->transform( $params ) ) ) {
212 # Error generating thumbnail.
213 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
214 . htmlspecialchars( $img->getLastError() ) . '</div>';
215 } else {
216 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
217 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+30).'px;">'
218 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
221 //TODO
222 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
224 if( $this->mShowBytes ) {
225 if( $img->exists() ) {
226 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
227 $wgLang->formatNum( $img->getSize() ) );
228 } else {
229 $nb = wfMsgHtml( 'filemissing' );
231 $nb = "$nb<br />\n";
232 } else {
233 $nb = '';
236 $textlink = $this->mShowFilename ?
237 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
238 '' ;
240 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
241 # in version 4.8.6 generated crackpot html in its absence, see:
242 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
244 if ( $i % $this->mPerRow == 0 ) {
245 $s .= "\n\t<tr>";
247 $s .=
248 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths*1.25).'px;">'
249 . $thumbhtml
250 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
251 . $textlink . $text . $nb
252 . "\n\t\t\t</div>"
253 . "\n\t\t</div></td>";
254 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
255 $s .= "\n\t</tr>";
257 ++$i;
259 if( $i % $this->mPerRow != 0 ) {
260 $s .= "\n\t</tr>";
262 $s .= "\n</table>";
264 return $s;
268 * @return int Number of images in the gallery
270 public function count() {
271 return count( $this->mImages );
275 * Set the contextual title
277 * @param Title $title Contextual title
279 public function setContextTitle( $title ) {
280 $this->contextTitle = $title;
284 * Get the contextual title, if applicable
286 * @return mixed Title or false
288 public function getContextTitle() {
289 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
290 ? $this->contextTitle
291 : false;
294 } //class