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