*Add BeforeGalleryFindFile, TitleLinkUpdatesAfterCompletion, BeforeParserFetchTemplat...
[lhc/web/wiklou.git] / includes / ImageGallery.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
4
5 /**
6 */
7
8 /**
9 * Image gallery
10 *
11 * Add images to the gallery using add(), then render that list to HTML using toHTML().
12 *
13 * @addtogroup Media
14 */
15 class ImageGallery
16 {
17 var $mImages, $mShowBytes, $mShowFilename;
18 var $mCaption = false;
19 var $mSkin = false;
20 var $mRevisionId = 0;
21
22 /**
23 * Is the gallery on a wiki page (i.e. not a special page)
24 */
25 var $mParsing;
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 $mPerRow = 4; // How many images wide should the gallery be?
34 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
35
36 /**
37 * Create a new image gallery object.
38 */
39 function __construct( ) {
40 $this->mImages = array();
41 $this->mShowBytes = true;
42 $this->mShowFilename = true;
43 $this->mParsing = false;
44 }
45
46 /**
47 * Set the "parse" bit so we know to hide "bad" images
48 */
49 function setParsing( $val = true ) {
50 $this->mParsing = $val;
51 }
52
53 /**
54 * Set the caption (as plain text)
55 *
56 * @param $caption Caption
57 */
58 function setCaption( $caption ) {
59 $this->mCaption = htmlspecialchars( $caption );
60 }
61
62 /**
63 * Set the caption (as HTML)
64 *
65 * @param $caption Caption
66 */
67 public function setCaptionHtml( $caption ) {
68 $this->mCaption = $caption;
69 }
70
71 /**
72 * Set how many images will be displayed per row.
73 *
74 * @param int $num > 0; invalid numbers will be rejected
75 */
76 public function setPerRow( $num ) {
77 if ($num > 0) {
78 $this->mPerRow = (int)$num;
79 }
80 }
81
82 /**
83 * Set how wide each image will be, in pixels.
84 *
85 * @param int $num > 0; invalid numbers will be ignored
86 */
87 public function setWidths( $num ) {
88 if ($num > 0) {
89 $this->mWidths = (int)$num;
90 }
91 }
92
93 /**
94 * Set how high each image will be, in pixels.
95 *
96 * @param int $num > 0; invalid numbers will be ignored
97 */
98 public function setHeights( $num ) {
99 if ($num > 0) {
100 $this->mHeights = (int)$num;
101 }
102 }
103
104 /**
105 * Instruct the class to use a specific skin for rendering
106 *
107 * @param $skin Skin object
108 */
109 function useSkin( $skin ) {
110 $this->mSkin = $skin;
111 }
112
113 /**
114 * Return the skin that should be used
115 *
116 * @return Skin object
117 */
118 function getSkin() {
119 if( !$this->mSkin ) {
120 global $wgUser;
121 $skin = $wgUser->getSkin();
122 } else {
123 $skin = $this->mSkin;
124 }
125 return $skin;
126 }
127
128 /**
129 * Add an image to the gallery.
130 *
131 * @param $title Title object of the image that is added to the gallery
132 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
133 */
134 function add( $title, $html='' ) {
135 if ( $title instanceof File ) {
136 // Old calling convention
137 $title = $title->getTitle();
138 }
139 $this->mImages[] = array( $title, $html );
140 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
141 }
142
143 /**
144 * Add an image at the beginning of the gallery.
145 *
146 * @param $title Title object of the image that is added to the gallery
147 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
148 */
149 function insert( $title, $html='' ) {
150 array_unshift( $this->mImages, array( &$title, $html ) );
151 }
152
153
154 /**
155 * isEmpty() returns true if the gallery contains no images
156 */
157 function isEmpty() {
158 return empty( $this->mImages );
159 }
160
161 /**
162 * Enable/Disable showing of the file size of an image in the gallery.
163 * Enabled by default.
164 *
165 * @param $f Boolean: set to false to disable.
166 */
167 function setShowBytes( $f ) {
168 $this->mShowBytes = ( $f == true);
169 }
170
171 /**
172 * Enable/Disable showing of the filename of an image in the gallery.
173 * Enabled by default.
174 *
175 * @param $f Boolean: set to false to disable.
176 */
177 function setShowFilename( $f ) {
178 $this->mShowFilename = ( $f == true);
179 }
180
181 /**
182 * Return a HTML representation of the image gallery
183 *
184 * For each image in the gallery, display
185 * - a thumbnail
186 * - the image name
187 * - the additional text provided when adding the image
188 * - the size of the image
189 *
190 */
191 function toHTML() {
192 global $wgLang;
193
194 $sk = $this->getSkin();
195
196 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
197 if( $this->mCaption )
198 $s .= "\n\t<caption>{$this->mCaption}</caption>";
199
200 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
201 $i = 0;
202 foreach ( $this->mImages as $pair ) {
203 $nt = $pair[0];
204 $text = $pair[1];
205
206 # Give extensions a chance to select the file revision for us
207 $time = false;
208 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time ) );
209
210 $img = wfFindFile( $nt, $time );
211
212 if( $nt->getNamespace() != NS_IMAGE || !$img ) {
213 # We're dealing with a non-image, spit out the name and be done with it.
214 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
215 . htmlspecialchars( $nt->getText() ) . '</div>';
216 } elseif( $this->mParsing && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
217 # The image is blacklisted, just show it as a text link.
218 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
219 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
220 } elseif( !( $thumb = $img->transform( $params ) ) ) {
221 # Error generating thumbnail.
222 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
223 . htmlspecialchars( $img->getLastError() ) . '</div>';
224 } else {
225 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
226 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+30).'px;">'
227 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
228 }
229
230 //TODO
231 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
232
233 if( $this->mShowBytes ) {
234 if( $img ) {
235 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
236 $wgLang->formatNum( $img->getSize() ) );
237 } else {
238 $nb = wfMsgHtml( 'filemissing' );
239 }
240 $nb = "$nb<br />\n";
241 } else {
242 $nb = '';
243 }
244
245 $textlink = $this->mShowFilename ?
246 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
247 '' ;
248
249 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
250 # in version 4.8.6 generated crackpot html in its absence, see:
251 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
252
253 if ( $i % $this->mPerRow == 0 ) {
254 $s .= "\n\t<tr>";
255 }
256 $s .=
257 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths*1.25).'px;">'
258 . $thumbhtml
259 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
260 . $textlink . $text . $nb
261 . "\n\t\t\t</div>"
262 . "\n\t\t</div></td>";
263 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
264 $s .= "\n\t</tr>";
265 }
266 ++$i;
267 }
268 if( $i % $this->mPerRow != 0 ) {
269 $s .= "\n\t</tr>";
270 }
271 $s .= "\n</table>";
272
273 return $s;
274 }
275
276 /**
277 * @return int Number of images in the gallery
278 */
279 public function count() {
280 return count( $this->mImages );
281 }
282
283 /**
284 * Set the contextual title
285 *
286 * @param Title $title Contextual title
287 */
288 public function setContextTitle( $title ) {
289 $this->contextTitle = $title;
290 }
291
292 /**
293 * Get the contextual title, if applicable
294 *
295 * @return mixed Title or false
296 */
297 public function getContextTitle() {
298 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
299 ? $this->contextTitle
300 : false;
301 }
302
303 } //class
304 ?>