]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/ImageGallery.php
MediaWiki 1.15.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 $mPerRow = 4; // How many images wide should the gallery be?
36         private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
37
38         private $mAttribs = array();
39
40         /**
41          * Create a new image gallery object.
42          */
43         function __construct( ) {
44                 $this->mImages = array();
45                 $this->mShowBytes = true;
46                 $this->mShowFilename = true;
47                 $this->mParser = false;
48                 $this->mHideBadImages = false;
49         }
50
51         /**
52          * Register a parser object
53          */
54         function setParser( $parser ) {
55                 $this->mParser = $parser;
56         }
57
58         /**
59          * Set bad image flag
60          */
61         function setHideBadImages( $flag = true ) {
62                 $this->mHideBadImages = $flag;
63         }
64
65         /**
66          * Set the caption (as plain text)
67          *
68          * @param $caption Caption
69          */
70         function setCaption( $caption ) {
71                 $this->mCaption = htmlspecialchars( $caption );
72         }
73
74         /**
75          * Set the caption (as HTML)
76          *
77          * @param $caption Caption
78          */
79         public function setCaptionHtml( $caption ) {
80                 $this->mCaption = $caption;
81         }
82
83         /**
84          * Set how many images will be displayed per row.
85          *
86          * @param int $num > 0; invalid numbers will be rejected
87          */
88         public function setPerRow( $num ) {
89                 if ($num > 0) {
90                         $this->mPerRow = (int)$num;
91                 }
92         }
93
94         /**
95          * Set how wide each image will be, in pixels.
96          *
97          * @param int $num > 0; invalid numbers will be ignored
98          */
99         public function setWidths( $num ) {
100                 if ($num > 0) {
101                         $this->mWidths = (int)$num;
102                 }
103         }
104
105         /**
106          * Set how high each image will be, in pixels.
107          *
108          * @param int $num > 0; invalid numbers will be ignored
109          */
110         public function setHeights( $num ) {
111                 if ($num > 0) {
112                         $this->mHeights = (int)$num;
113                 }
114         }
115
116         /**
117          * Instruct the class to use a specific skin for rendering
118          *
119          * @param $skin Skin object
120          */
121         function useSkin( $skin ) {
122                 $this->mSkin = $skin;
123         }
124
125         /**
126          * Return the skin that should be used
127          *
128          * @return Skin object
129          */
130         function getSkin() {
131                 if( !$this->mSkin ) {
132                         global $wgUser;
133                         $skin = $wgUser->getSkin();
134                 } else {
135                         $skin = $this->mSkin;
136                 }
137                 return $skin;
138         }
139
140         /**
141          * Add an image to the gallery.
142          *
143          * @param $title Title object of the image that is added to the gallery
144          * @param $html  String: additional HTML text to be shown. The name and size of the image are always shown.
145          */
146         function add( $title, $html='' ) {
147                 if ( $title instanceof File ) {
148                         // Old calling convention
149                         $title = $title->getTitle();
150                 }
151                 $this->mImages[] = array( $title, $html );
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         */
161         function insert( $title, $html='' ) {
162                 if ( $title instanceof File ) {
163                         // Old calling convention
164                         $title = $title->getTitle();
165                 }
166                 array_unshift( $this->mImages, array( &$title, $html ) );
167         }
168
169
170         /**
171          * isEmpty() returns true if the gallery contains no images
172          */
173         function isEmpty() {
174                 return empty( $this->mImages );
175         }
176
177         /**
178          * Enable/Disable showing of the file size of an image in the gallery.
179          * Enabled by default.
180          *
181          * @param $f Boolean: set to false to disable.
182          */
183         function setShowBytes( $f ) {
184                 $this->mShowBytes = ( $f == true);
185         }
186
187         /**
188          * Enable/Disable showing of the filename of an image in the gallery.
189          * Enabled by default.
190          *
191          * @param $f Boolean: set to false to disable.
192          */
193         function setShowFilename( $f ) {
194                 $this->mShowFilename = ( $f == true);
195         }
196
197         /**
198          * Set arbitrary attributes to go on the HTML gallery output element.
199          * Should be suitable for a &lt;table&gt; element.
200          *
201          * Note -- if taking from user input, you should probably run through
202          * Sanitizer::validateAttributes() first.
203          *
204          * @param array of HTML attribute pairs
205          */
206         function setAttributes( $attribs ) {
207                 $this->mAttribs = $attribs;
208         }
209
210         /**
211          * Return a HTML representation of the image gallery
212          *
213          * For each image in the gallery, display
214          * - a thumbnail
215          * - the image name
216          * - the additional text provided when adding the image
217          * - the size of the image
218          *
219          */
220         function toHTML() {
221                 global $wgLang;
222
223                 $sk = $this->getSkin();
224
225                 $attribs = Sanitizer::mergeAttributes(
226                         array(
227                                 'class' => 'gallery',
228                                 'cellspacing' => '0',
229                                 'cellpadding' => '0' ),
230                         $this->mAttribs );
231                 $s = Xml::openElement( 'table', $attribs );
232                 if( $this->mCaption )
233                         $s .= "\n\t<caption>{$this->mCaption}</caption>";
234
235                 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
236                 $i = 0;
237                 foreach ( $this->mImages as $pair ) {
238                         $nt = $pair[0];
239                         $text = $pair[1];
240
241                         # Give extensions a chance to select the file revision for us
242                         $time = $descQuery = false;
243                         wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
244
245                         $img = wfFindFile( $nt, $time );
246
247                         if( $nt->getNamespace() != NS_FILE || !$img ) {
248                                 # We're dealing with a non-image, spit out the name and be done with it.
249                                 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
250                                         . htmlspecialchars( $nt->getText() ) . '</div>';
251                         } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
252                                 # The image is blacklisted, just show it as a text link.
253                                 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
254                                         . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
255                         } elseif( !( $thumb = $img->transform( $params ) ) ) {
256                                 # Error generating thumbnail.
257                                 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
258                                         . htmlspecialchars( $img->getLastError() ) . '</div>';
259                         } else {
260                                 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
261
262                                 $thumbhtml = "\n\t\t\t".
263                                         '<div class="thumb" style="padding: ' . $vpad . 'px 0; width: ' .($this->mWidths+30).'px;">'
264                                         # Auto-margin centering for block-level elements. Needed now that we have video
265                                         # handlers since they may emit block-level elements as opposed to simple <img> tags.
266                                         # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
267                                         . '<div style="margin-left: auto; margin-right: auto; width: ' .$this->mWidths.'px;">'
268                                         . $thumb->toHtml( array( 'desc-link' => true, 'desc-query' => $descQuery ) ) . '</div></div>';
269
270                                 // Call parser transform hook
271                                 if ( $this->mParser && $img->getHandler() ) {
272                                         $img->getHandler()->parserTransformHook( $this->mParser, $img );
273                                 }
274                         }
275
276                         //TODO
277                         //$ul = $sk->makeLink( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}", $ut );
278
279                         if( $this->mShowBytes ) {
280                                 if( $img ) {
281                                         $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
282                                                 $wgLang->formatNum( $img->getSize() ) );
283                                 } else {
284                                         $nb = wfMsgHtml( 'filemissing' );
285                                 }
286                                 $nb = "$nb<br />\n";
287                         } else {
288                                 $nb = '';
289                         }
290
291                         $textlink = $this->mShowFilename ?
292                                 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20 ) ) ) . "<br />\n" :
293                                 '' ;
294
295                         # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
296                         # in version 4.8.6 generated crackpot html in its absence, see:
297                         # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
298
299                         if ( $i % $this->mPerRow == 0 ) {
300                                 $s .= "\n\t<tr>";
301                         }
302                         $s .=
303                                 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths+35).'px;">'
304                                         . $thumbhtml
305                                         . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
306                                                 . $textlink . $text . $nb
307                                         . "\n\t\t\t</div>"
308                                 . "\n\t\t</div></td>";
309                         if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
310                                 $s .= "\n\t</tr>";
311                         }
312                         ++$i;
313                 }
314                 if( $i % $this->mPerRow != 0 ) {
315                         $s .= "\n\t</tr>";
316                 }
317                 $s .= "\n</table>";
318
319                 return $s;
320         }
321
322         /**
323          * @return int Number of images in the gallery
324          */
325         public function count() {
326                 return count( $this->mImages );
327         }
328
329         /**
330          * Set the contextual title
331          *
332          * @param Title $title Contextual title
333          */
334         public function setContextTitle( $title ) {
335                 $this->contextTitle = $title;
336         }
337
338         /**
339          * Get the contextual title, if applicable
340          *
341          * @return mixed Title or false
342          */
343         public function getContextTitle() {
344                 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
345                                 ? $this->contextTitle
346                                 : false;
347         }
348
349 } //class