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