8c13d255f252a5b24c25854209954b78782dc9ff
[lhc/web/wiklou.git] / includes / ImageGallery.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
4
5 /**
6 * Image gallery
7 *
8 * Add images to the gallery using add(), then render that list to HTML using toHTML().
9 *
10 * @ingroup Media
11 */
12 class ImageGallery
13 {
14 var $mImages, $mShowBytes, $mShowFilename;
15 var $mCaption = false;
16 var $mSkin = false;
17
18 /**
19 * Hide blacklisted images?
20 */
21 var $mHideBadImages;
22
23 /**
24 * Registered parser object for output callbacks
25 */
26 var $mParser;
27
28 /**
29 * Contextual title, used when images are being screened
30 * against the bad image list
31 */
32 private $contextTitle = false;
33
34 private $mAttribs = array();
35
36 /**
37 * Fixed margins
38 */
39 const THUMB_PADDING = 30;
40 const GB_PADDING = 5;
41 const GB_BORDERS = 6;
42
43 /**
44 * Create a new image gallery object.
45 */
46 function __construct( ) {
47 global $wgGalleryOptions;
48 $this->mImages = array();
49 $this->mShowBytes = $wgGalleryOptions['showBytes'];
50 $this->mShowFilename = true;
51 $this->mParser = false;
52 $this->mHideBadImages = false;
53 $this->mPerRow = $wgGalleryOptions['imagesPerRow'];
54 $this->mWidths = $wgGalleryOptions['imageWidth'];
55 $this->mHeights = $wgGalleryOptions['imageHeight'];
56 $this->mCaptionLength = $wgGalleryOptions['captionLength'];
57 }
58
59 /**
60 * Register a parser object
61 */
62 function setParser( $parser ) {
63 $this->mParser = $parser;
64 }
65
66 /**
67 * Set bad image flag
68 */
69 function setHideBadImages( $flag = true ) {
70 $this->mHideBadImages = $flag;
71 }
72
73 /**
74 * Set the caption (as plain text)
75 *
76 * @param $caption Caption
77 */
78 function setCaption( $caption ) {
79 $this->mCaption = htmlspecialchars( $caption );
80 }
81
82 /**
83 * Set the caption (as HTML)
84 *
85 * @param $caption String: Caption
86 */
87 public function setCaptionHtml( $caption ) {
88 $this->mCaption = $caption;
89 }
90
91 /**
92 * Set how many images will be displayed per row.
93 *
94 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
95 * invalid numbers will be rejected
96 */
97 public function setPerRow( $num ) {
98 if ($num >= 0) {
99 $this->mPerRow = (int)$num;
100 }
101 }
102
103 /**
104 * Set how wide each image will be, in pixels.
105 *
106 * @param $num Integer > 0; invalid numbers will be ignored
107 */
108 public function setWidths( $num ) {
109 if ($num > 0) {
110 $this->mWidths = (int)$num;
111 }
112 }
113
114 /**
115 * Set how high each image will be, in pixels.
116 *
117 * @param $num Integer > 0; invalid numbers will be ignored
118 */
119 public function setHeights( $num ) {
120 if ($num > 0) {
121 $this->mHeights = (int)$num;
122 }
123 }
124
125 /**
126 * Instruct the class to use a specific skin for rendering
127 *
128 * @param $skin Skin object
129 */
130 function useSkin( $skin ) {
131 $this->mSkin = $skin;
132 }
133
134 /**
135 * Return the skin that should be used
136 *
137 * @return Skin object
138 */
139 function getSkin() {
140 if( !$this->mSkin ) {
141 global $wgUser;
142 $skin = $wgUser->getSkin();
143 } else {
144 $skin = $this->mSkin;
145 }
146 return $skin;
147 }
148
149 /**
150 * Add an image to the gallery.
151 *
152 * @param $title Title object of the image that is added to the gallery
153 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
154 */
155 function add( $title, $html='' ) {
156 if ( $title instanceof File ) {
157 // Old calling convention
158 $title = $title->getTitle();
159 }
160 $this->mImages[] = array( $title, $html );
161 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
162 }
163
164 /**
165 * Add an image at the beginning of the gallery.
166 *
167 * @param $title Title object of the image that is added to the gallery
168 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
169 */
170 function insert( $title, $html='' ) {
171 if ( $title instanceof File ) {
172 // Old calling convention
173 $title = $title->getTitle();
174 }
175 array_unshift( $this->mImages, array( &$title, $html ) );
176 }
177
178
179 /**
180 * isEmpty() returns true if the gallery contains no images
181 */
182 function isEmpty() {
183 return empty( $this->mImages );
184 }
185
186 /**
187 * Enable/Disable showing of the file size of an image in the gallery.
188 * Enabled by default.
189 *
190 * @param $f Boolean: set to false to disable.
191 */
192 function setShowBytes( $f ) {
193 $this->mShowBytes = (bool)$f;
194 }
195
196 /**
197 * Enable/Disable showing of the filename of an image in the gallery.
198 * Enabled by default.
199 *
200 * @param $f Boolean: set to false to disable.
201 */
202 function setShowFilename( $f ) {
203 $this->mShowFilename = (bool)$f;
204 }
205
206 /**
207 * Set arbitrary attributes to go on the HTML gallery output element.
208 * Should be suitable for a <ul> element.
209 *
210 * Note -- if taking from user input, you should probably run through
211 * Sanitizer::validateAttributes() first.
212 *
213 * @param $attribs Array of HTML attribute pairs
214 */
215 function setAttributes( $attribs ) {
216 $this->mAttribs = $attribs;
217 }
218
219 /**
220 * Return a HTML representation of the image gallery
221 *
222 * For each image in the gallery, display
223 * - a thumbnail
224 * - the image name
225 * - the additional text provided when adding the image
226 * - the size of the image
227 *
228 */
229 function toHTML() {
230 global $wgLang;
231
232 $sk = $this->getSkin();
233
234 if ( $this->mPerRow > 0 ) {
235 $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS );
236 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : "";
237 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
238 }
239
240 $attribs = Sanitizer::mergeAttributes(
241 array(
242 'class' => 'gallery'),
243 $this->mAttribs );
244 $s = Xml::openElement( 'ul', $attribs );
245 if ( $this->mCaption ) {
246 $s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
247 }
248
249 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
250 $i = 0;
251 foreach ( $this->mImages as $pair ) {
252 $nt = $pair[0];
253 $text = $pair[1]; # "text" means "caption" here
254
255 if ( $nt->getNamespace() == NS_FILE ) {
256 # Give extensions a chance to select the file revision for us
257 $time = $sha1 = $descQuery = false;
258 wfRunHooks( 'BeforeGalleryFindFile',
259 array( &$this, &$nt, &$time, &$descQuery, &$sha1 ) );
260 # Get the file and register it
261 $img = $this->mParser->fetchFile( $nt, $time, $sha1 );
262 if ( $img ) {
263 $nt = $img->getTitle(); // file title may be different (via hooks)
264 }
265 } else {
266 $img = false;
267 }
268
269 if( !$img ) {
270 # We're dealing with a non-image, spit out the name and be done with it.
271 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">'
272 . htmlspecialchars( $nt->getText() ) . '</div>';
273 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
274 # The image is blacklisted, just show it as a text link.
275 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">' .
276 $sk->link(
277 $nt,
278 htmlspecialchars( $nt->getText() ),
279 array(),
280 array(),
281 array( 'known', 'noclasses' )
282 ) .
283 '</div>';
284 } elseif( !( $thumb = $img->transform( $params ) ) ) {
285 # Error generating thumbnail.
286 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">'
287 . htmlspecialchars( $img->getLastError() ) . '</div>';
288 } else {
289 //We get layout problems with the margin, if the image is smaller
290 //than the line-height, so we less margin in these cases.
291 $minThumbHeight = $thumb->height > 17 ? $thumb->height : 17;
292 $vpad = floor(( self::THUMB_PADDING + $this->mHeights - $minThumbHeight ) /2);
293
294
295 $imageParameters = array(
296 'desc-link' => true,
297 'desc-query' => $descQuery
298 );
299 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
300 if ( $text == '' ) {
301 $imageParameters['alt'] = $nt->getText();
302 }
303
304 # Set both fixed width and min-height.
305 $thumbhtml = "\n\t\t\t".
306 '<div class="thumb" style="width: ' .($this->mWidths + self::THUMB_PADDING).'px;">'
307 # Auto-margin centering for block-level elements. Needed now that we have video
308 # handlers since they may emit block-level elements as opposed to simple <img> tags.
309 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
310 . '<div style="margin:'.$vpad.'px auto;">'
311 . $thumb->toHtml( $imageParameters ) . '</div></div>';
312
313 // Call parser transform hook
314 if ( $this->mParser && $img->getHandler() ) {
315 $img->getHandler()->parserTransformHook( $this->mParser, $img );
316 }
317 }
318
319 //TODO
320 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
321 // $ul = $sk->link( $linkTarget, $ut );
322
323 if( $this->mShowBytes ) {
324 if( $img ) {
325 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
326 $wgLang->formatNum( $img->getSize() ) );
327 } else {
328 $nb = wfMsgHtml( 'filemissing' );
329 }
330 $nb = "$nb<br />\n";
331 } else {
332 $nb = '';
333 }
334
335 $textlink = $this->mShowFilename ?
336 $sk->link(
337 $nt,
338 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength ) ),
339 array(),
340 array(),
341 array( 'known', 'noclasses' )
342 ) . "<br />\n" :
343 '' ;
344
345 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
346 # in version 4.8.6 generated crackpot html in its absence, see:
347 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
348
349 # Weird double wrapping in div needed due to FF2 bug
350 # Can be safely removed if FF2 falls completely out of existance
351 $s .=
352 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
353 . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
354 . $thumbhtml
355 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
356 . $textlink . $text . $nb
357 . "\n\t\t\t</div>"
358 . "\n\t\t</div></li>";
359 ++$i;
360 }
361 $s .= "\n</ul>";
362
363 return $s;
364 }
365
366 /**
367 * @return Integer: number of images in the gallery
368 */
369 public function count() {
370 return count( $this->mImages );
371 }
372
373 /**
374 * Set the contextual title
375 *
376 * @param $title Title: contextual title
377 */
378 public function setContextTitle( $title ) {
379 $this->contextTitle = $title;
380 }
381
382 /**
383 * Get the contextual title, if applicable
384 *
385 * @return mixed Title or false
386 */
387 public function getContextTitle() {
388 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
389 ? $this->contextTitle
390 : false;
391 }
392
393 } //class