Merged filerepo-work branch:
[lhc/web/wiklou.git] / includes / ImageGallery.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
4
5 /**
6 */
7
8 /**
9 * Image gallery
10 *
11 * Add images to the gallery using add(), then render that list to HTML using toHTML().
12 *
13 * @addtogroup Media
14 */
15 class ImageGallery
16 {
17 var $mImages, $mShowBytes, $mShowFilename;
18 var $mCaption = false;
19 var $mSkin = false;
20
21 /**
22 * Is the gallery on a wiki page (i.e. not a special page)
23 */
24 var $mParsing;
25
26 /**
27 * Contextual title, used when images are being screened
28 * against the bad image list
29 */
30 private $contextTitle = false;
31
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
34
35 /**
36 * Create a new image gallery object.
37 */
38 function __construct( ) {
39 $this->mImages = array();
40 $this->mShowBytes = true;
41 $this->mShowFilename = true;
42 $this->mParsing = false;
43 }
44
45 /**
46 * Set the "parse" bit so we know to hide "bad" images
47 */
48 function setParsing( $val = true ) {
49 $this->mParsing = $val;
50 }
51
52 /**
53 * Set the caption (as plain text)
54 *
55 * @param $caption Caption
56 */
57 function setCaption( $caption ) {
58 $this->mCaption = htmlspecialchars( $caption );
59 }
60
61 /**
62 * Set the caption (as HTML)
63 *
64 * @param $caption Caption
65 */
66 public function setCaptionHtml( $caption ) {
67 $this->mCaption = $caption;
68 }
69
70 /**
71 * Set how many images will be displayed per row.
72 *
73 * @param int $num > 0; invalid numbers will be rejected
74 */
75 public function setPerRow( $num ) {
76 if ($num > 0) {
77 $this->mPerRow = (int)$num;
78 }
79 }
80
81 /**
82 * Set how wide each image will be, in pixels.
83 *
84 * @param int $num > 0; invalid numbers will be ignored
85 */
86 public function setWidths( $num ) {
87 if ($num > 0) {
88 $this->mWidths = (int)$num;
89 }
90 }
91
92 /**
93 * Set how high each image will be, in pixels.
94 *
95 * @param int $num > 0; invalid numbers will be ignored
96 */
97 public function setHeights( $num ) {
98 if ($num > 0) {
99 $this->mHeights = (int)$num;
100 }
101 }
102
103 /**
104 * Instruct the class to use a specific skin for rendering
105 *
106 * @param $skin Skin object
107 */
108 function useSkin( $skin ) {
109 $this->mSkin = $skin;
110 }
111
112 /**
113 * Return the skin that should be used
114 *
115 * @return Skin object
116 */
117 function getSkin() {
118 if( !$this->mSkin ) {
119 global $wgUser;
120 $skin = $wgUser->getSkin();
121 } else {
122 $skin = $this->mSkin;
123 }
124 return $skin;
125 }
126
127 /**
128 * Add an image to the gallery.
129 *
130 * @param $title Title object of the image 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.
132 */
133 function add( $title, $html='' ) {
134 if ( $title instanceof File ) {
135 // Old calling convention
136 $title = $title->getTitle();
137 }
138 $this->mImages[] = array( $title, $html );
139 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
140 }
141
142 /**
143 * Add an image at the beginning of the gallery.
144 *
145 * @param $title Title object of the image that is added to the gallery
146 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
147 */
148 function insert( $title, $html='' ) {
149 array_unshift( $this->mImages, array( &$title, $html ) );
150 }
151
152
153 /**
154 * isEmpty() returns true if the gallery contains no images
155 */
156 function isEmpty() {
157 return empty( $this->mImages );
158 }
159
160 /**
161 * Enable/Disable showing of the file size of an image in the gallery.
162 * Enabled by default.
163 *
164 * @param $f Boolean: set to false to disable.
165 */
166 function setShowBytes( $f ) {
167 $this->mShowBytes = ( $f == true);
168 }
169
170 /**
171 * Enable/Disable showing of the filename of an image in the gallery.
172 * Enabled by default.
173 *
174 * @param $f Boolean: set to false to disable.
175 */
176 function setShowFilename( $f ) {
177 $this->mShowFilename = ( $f == true);
178 }
179
180 /**
181 * Return a HTML representation of the image gallery
182 *
183 * For each image in the gallery, display
184 * - a thumbnail
185 * - the image name
186 * - the additional text provided when adding the image
187 * - the size of the image
188 *
189 */
190 function toHTML() {
191 global $wgLang;
192
193 $sk = $this->getSkin();
194
195 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
196 if( $this->mCaption )
197 $s .= "\n\t<caption>{$this->mCaption}</caption>";
198
199 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
200 $i = 0;
201 foreach ( $this->mImages as $pair ) {
202 $nt = $pair[0];
203 $text = $pair[1];
204
205 $img = wfFindFile( $nt );
206
207 if( $nt->getNamespace() != NS_IMAGE || !$img ) {
208 # We're dealing with a non-image, spit out the name and be done with it.
209 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
210 . htmlspecialchars( $nt->getText() ) . '</div>';
211 } elseif( $this->mParsing && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
212 # The image is blacklisted, just show it as a text link.
213 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
214 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
215 } elseif( !( $thumb = $img->transform( $params ) ) ) {
216 # Error generating thumbnail.
217 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
218 . htmlspecialchars( $img->getLastError() ) . '</div>';
219 } else {
220 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
221 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+30).'px;">'
222 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
223 }
224
225 //TODO
226 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
227
228 if( $this->mShowBytes ) {
229 if( $img ) {
230 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
231 $wgLang->formatNum( $img->getSize() ) );
232 } else {
233 $nb = wfMsgHtml( 'filemissing' );
234 }
235 $nb = "$nb<br />\n";
236 } else {
237 $nb = '';
238 }
239
240 $textlink = $this->mShowFilename ?
241 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
242 '' ;
243
244 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
245 # in version 4.8.6 generated crackpot html in its absence, see:
246 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
247
248 if ( $i % $this->mPerRow == 0 ) {
249 $s .= "\n\t<tr>";
250 }
251 $s .=
252 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths*1.25).'px;">'
253 . $thumbhtml
254 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
255 . $textlink . $text . $nb
256 . "\n\t\t\t</div>"
257 . "\n\t\t</div></td>";
258 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
259 $s .= "\n\t</tr>";
260 }
261 ++$i;
262 }
263 if( $i % $this->mPerRow != 0 ) {
264 $s .= "\n\t</tr>";
265 }
266 $s .= "\n</table>";
267
268 return $s;
269 }
270
271 /**
272 * @return int Number of images in the gallery
273 */
274 public function count() {
275 return count( $this->mImages );
276 }
277
278 /**
279 * Set the contextual title
280 *
281 * @param Title $title Contextual title
282 */
283 public function setContextTitle( $title ) {
284 $this->contextTitle = $title;
285 }
286
287 /**
288 * Get the contextual title, if applicable
289 *
290 * @return mixed Title or false
291 */
292 public function getContextTitle() {
293 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
294 ? $this->contextTitle
295 : false;
296 }
297
298 } //class
299 ?>