* Move escaping of image gallery captions to ImageGallery::setCaption()
[lhc/web/wiklou.git] / includes / ImageGallery.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
4
5 /**
6 * @package MediaWiki
7 */
8
9 /**
10 * Image gallery
11 *
12 * Add images to the gallery using add(), then render that list to HTML using toHTML().
13 *
14 * @package MediaWiki
15 */
16 class ImageGallery
17 {
18 var $mImages, $mShowBytes, $mShowFilename;
19 var $mCaption = false;
20 var $mSkin = false;
21
22 /**
23 * Is the gallery on a wiki page (i.e. not a special page)
24 */
25 var $mParsing;
26
27 /**
28 * Create a new image gallery object.
29 */
30 function ImageGallery( ) {
31 $this->mImages = array();
32 $this->mShowBytes = true;
33 $this->mShowFilename = true;
34 $this->mParsing = false;
35 }
36
37 /**
38 * Set the "parse" bit so we know to hide "bad" images
39 */
40 function setParsing( $val = true ) {
41 $this->mParsing = $val;
42 }
43
44 /**
45 * Set the caption (as plain text)
46 *
47 * @param $caption Caption
48 */
49 function setCaption( $caption ) {
50 $this->mCaption = htmlspecialchars( $caption );
51 }
52
53 /**
54 * Set the caption (as HTML)
55 *
56 * @param $caption Caption
57 */
58 function setCaptionSafe( $caption ) {
59 $this->mCaption = $caption;
60 }
61
62 /**
63 * Instruct the class to use a specific skin for rendering
64 *
65 * @param $skin Skin object
66 */
67 function useSkin( $skin ) {
68 $this->mSkin =& $skin;
69 }
70
71 /**
72 * Return the skin that should be used
73 *
74 * @return Skin object
75 */
76 function getSkin() {
77 if( !$this->mSkin ) {
78 global $wgUser;
79 $skin =& $wgUser->getSkin();
80 } else {
81 $skin =& $this->mSkin;
82 }
83 return $skin;
84 }
85
86 /**
87 * Add an image to the gallery.
88 *
89 * @param $image Image object that is added to the gallery
90 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
91 */
92 function add( $image, $html='' ) {
93 $this->mImages[] = array( &$image, $html );
94 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
95 }
96
97 /**
98 * Add an image at the beginning of the gallery.
99 *
100 * @param $image Image object that is added to the gallery
101 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
102 */
103 function insert( $image, $html='' ) {
104 array_unshift( $this->mImages, array( &$image, $html ) );
105 }
106
107
108 /**
109 * isEmpty() returns true if the gallery contains no images
110 */
111 function isEmpty() {
112 return empty( $this->mImages );
113 }
114
115 /**
116 * Enable/Disable showing of the file size of an image in the gallery.
117 * Enabled by default.
118 *
119 * @param $f Boolean: set to false to disable.
120 */
121 function setShowBytes( $f ) {
122 $this->mShowBytes = ( $f == true);
123 }
124
125 /**
126 * Enable/Disable showing of the filename of an image in the gallery.
127 * Enabled by default.
128 *
129 * @param $f Boolean: set to false to disable.
130 */
131 function setShowFilename( $f ) {
132 $this->mShowFilename = ( $f == true);
133 }
134
135 /**
136 * Return a HTML representation of the image gallery
137 *
138 * For each image in the gallery, display
139 * - a thumbnail
140 * - the image name
141 * - the additional text provided when adding the image
142 * - the size of the image
143 *
144 */
145 function toHTML() {
146 global $wgLang, $wgGenerateThumbnailOnParse;
147
148 $sk = $this->getSkin();
149
150 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
151 if( $this->mCaption )
152 $s .= '<td class="galleryheader" colspan="4"><big>' . $this->mCaption . '</big></td>';
153
154 $i = 0;
155 foreach ( $this->mImages as $pair ) {
156 $img =& $pair[0];
157 $text = $pair[1];
158
159 $nt = $img->getTitle();
160
161 if( $nt->getNamespace() != NS_IMAGE ) {
162 # We're dealing with a non-image, spit out the name and be done with it.
163 $thumbhtml = '<div style="height: 152px;">' . htmlspecialchars( $nt->getText() ) . '</div>';
164 }
165 else if( $this->mParsing && wfIsBadImage( $nt->getDBkey() ) ) {
166 # The image is blacklisted, just show it as a text link.
167 $thumbhtml = '<div style="height: 152px;">'
168 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
169 } else if( !( $thumb = $img->getThumbnail( 120, 120, $wgGenerateThumbnailOnParse ) ) ) {
170 # Error generating thumbnail.
171 $thumbhtml = '<div style="height: 152px;">'
172 . htmlspecialchars( $img->getLastError() ) . '</div>';
173 }
174 else {
175 $vpad = floor( ( 150 - $thumb->height ) /2 ) - 2;
176 $thumbhtml = '<div class="thumb" style="padding: ' . $vpad . 'px 0;">'
177 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
178 }
179
180 //TODO
181 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
182
183 if( $this->mShowBytes ) {
184 if( $img->exists() ) {
185 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
186 $wgLang->formatNum( $img->getSize() ) );
187 } else {
188 $nb = wfMsgHtml( 'filemissing' );
189 }
190 $nb = "$nb<br />\n";
191 } else {
192 $nb = '';
193 }
194
195 $textlink = $this->mShowFilename ?
196 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
197 '' ;
198
199 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
200 # in version 4.8.6 generated crackpot html in its absence, see:
201 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
202
203 $s .= ($i%4==0) ? '<tr>' : '';
204 $s .= '<td><div class="gallerybox">' . $thumbhtml
205 . '<div class="gallerytext">' . "\n" . $textlink . $text . $nb
206 . "</div></div></td>\n";
207 $s .= ($i%4==3) ? '</tr>' : '';
208 $i++;
209 }
210 if( $i %4 != 0 ) {
211 $s .= "</tr>\n";
212 }
213 $s .= '</table>';
214
215 return $s;
216 }
217
218 /**
219 * @return int Number of images in the gallery
220 */
221 public function count() {
222 return count( $this->mImages );
223 }
224
225 } //class
226 ?>