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