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