Follow-up changes to r84610:
[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
17 /**
18 * Hide blacklisted images?
19 */
20 var $mHideBadImages;
21
22 /**
23 * Registered parser object for output callbacks
24 * @var Parser
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 * @deprecated Not used anymore
130 */
131 function useSkin( $skin ) {
132 /* no op */
133 }
134
135 /**
136 * Add an image to the gallery.
137 *
138 * @param $title Title object of the image that is added to the gallery
139 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
140 */
141 function add( $title, $html='' ) {
142 if ( $title instanceof File ) {
143 // Old calling convention
144 $title = $title->getTitle();
145 }
146 $this->mImages[] = array( $title, $html );
147 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
148 }
149
150 /**
151 * Add an image at the beginning of 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 insert( $title, $html='' ) {
157 if ( $title instanceof File ) {
158 // Old calling convention
159 $title = $title->getTitle();
160 }
161 array_unshift( $this->mImages, array( &$title, $html ) );
162 }
163
164
165 /**
166 * isEmpty() returns true if the gallery contains no images
167 */
168 function isEmpty() {
169 return empty( $this->mImages );
170 }
171
172 /**
173 * Enable/Disable showing of the file size of an image in the gallery.
174 * Enabled by default.
175 *
176 * @param $f Boolean: set to false to disable.
177 */
178 function setShowBytes( $f ) {
179 $this->mShowBytes = (bool)$f;
180 }
181
182 /**
183 * Enable/Disable showing of the filename of an image in the gallery.
184 * Enabled by default.
185 *
186 * @param $f Boolean: set to false to disable.
187 */
188 function setShowFilename( $f ) {
189 $this->mShowFilename = (bool)$f;
190 }
191
192 /**
193 * Set arbitrary attributes to go on the HTML gallery output element.
194 * Should be suitable for a <ul> element.
195 *
196 * Note -- if taking from user input, you should probably run through
197 * Sanitizer::validateAttributes() first.
198 *
199 * @param $attribs Array of HTML attribute pairs
200 */
201 function setAttributes( $attribs ) {
202 $this->mAttribs = $attribs;
203 }
204
205 /**
206 * Return a HTML representation of the image gallery
207 *
208 * For each image in the gallery, display
209 * - a thumbnail
210 * - the image name
211 * - the additional text provided when adding the image
212 * - the size of the image
213 *
214 */
215 function toHTML() {
216 global $wgLang;
217
218 if ( $this->mPerRow > 0 ) {
219 $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS );
220 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : "";
221 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
222 }
223
224 $attribs = Sanitizer::mergeAttributes(
225 array( 'class' => 'gallery' ), $this->mAttribs );
226
227 $s = Xml::openElement( 'ul', $attribs );
228 if ( $this->mCaption ) {
229 $s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
230 }
231
232 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
233 # Output each image...
234 foreach ( $this->mImages as $pair ) {
235 $nt = $pair[0];
236 $text = $pair[1]; # "text" means "caption" here
237
238 $descQuery = false;
239 if ( $nt->getNamespace() == NS_FILE ) {
240 # Get the file...
241 if ( $this->mParser instanceof Parser ) {
242 # Give extensions a chance to select the file revision for us
243 $time = $sha1 = false;
244 wfRunHooks( 'BeforeParserFetchFileAndTitle',
245 array( $this->mParser, $nt, &$time, &$sha1, &$descQuery ) );
246 # Fetch and register the file (file title may be different via hooks)
247 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $time, $sha1 );
248 } else {
249 $img = wfFindFile( $nt );
250 }
251 } else {
252 $img = false;
253 }
254
255 if( !$img ) {
256 # We're dealing with a non-image, spit out the name and be done with it.
257 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">'
258 . htmlspecialchars( $nt->getText() ) . '</div>';
259 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
260 # The image is blacklisted, just show it as a text link.
261 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">' .
262 Linker::link(
263 $nt,
264 htmlspecialchars( $nt->getText() ),
265 array(),
266 array(),
267 array( 'known', 'noclasses' )
268 ) .
269 '</div>';
270 } elseif( !( $thumb = $img->transform( $params ) ) ) {
271 # Error generating thumbnail.
272 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">'
273 . htmlspecialchars( $img->getLastError() ) . '</div>';
274 } else {
275 //We get layout problems with the margin, if the image is smaller
276 //than the line-height, so we less margin in these cases.
277 $minThumbHeight = $thumb->height > 17 ? $thumb->height : 17;
278 $vpad = floor(( self::THUMB_PADDING + $this->mHeights - $minThumbHeight ) /2);
279
280
281 $imageParameters = array(
282 'desc-link' => true,
283 'desc-query' => $descQuery
284 );
285 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
286 if ( $text == '' ) {
287 $imageParameters['alt'] = $nt->getText();
288 }
289
290 # Set both fixed width and min-height.
291 $thumbhtml = "\n\t\t\t".
292 '<div class="thumb" style="width: ' .($this->mWidths + self::THUMB_PADDING).'px;">'
293 # Auto-margin centering for block-level elements. Needed now that we have video
294 # handlers since they may emit block-level elements as opposed to simple <img> tags.
295 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
296 . '<div style="margin:'.$vpad.'px auto;">'
297 . $thumb->toHtml( $imageParameters ) . '</div></div>';
298
299 // Call parser transform hook
300 if ( $this->mParser && $img->getHandler() ) {
301 $img->getHandler()->parserTransformHook( $this->mParser, $img );
302 }
303 }
304
305 //TODO
306 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
307 // $ul = Linker::link( $linkTarget, $ut );
308
309 if( $this->mShowBytes ) {
310 if( $img ) {
311 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
312 $wgLang->formatNum( $img->getSize() ) );
313 } else {
314 $nb = wfMsgHtml( 'filemissing' );
315 }
316 $nb = "$nb<br />\n";
317 } else {
318 $nb = '';
319 }
320
321 $textlink = $this->mShowFilename ?
322 Linker::link(
323 $nt,
324 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength ) ),
325 array(),
326 array(),
327 array( 'known', 'noclasses' )
328 ) . "<br />\n" :
329 '' ;
330
331 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
332 # in version 4.8.6 generated crackpot html in its absence, see:
333 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
334
335 # Weird double wrapping in div needed due to FF2 bug
336 # Can be safely removed if FF2 falls completely out of existance
337 $s .=
338 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
339 . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
340 . $thumbhtml
341 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
342 . $textlink . $text . $nb
343 . "\n\t\t\t</div>"
344 . "\n\t\t</div></li>";
345 }
346 $s .= "\n</ul>";
347
348 return $s;
349 }
350
351 /**
352 * @return Integer: number of images in the gallery
353 */
354 public function count() {
355 return count( $this->mImages );
356 }
357
358 /**
359 * Set the contextual title
360 *
361 * @param $title Title: contextual title
362 */
363 public function setContextTitle( $title ) {
364 $this->contextTitle = $title;
365 }
366
367 /**
368 * Get the contextual title, if applicable
369 *
370 * @return mixed Title or false
371 */
372 public function getContextTitle() {
373 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
374 ? $this->contextTitle
375 : false;
376 }
377
378 } //class