* (bug 10265) Fix regression in category image gallery paging
[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 if ( $title instanceof File ) {
151 // Old calling convention
152 $title = $title->getTitle();
153 }
154 array_unshift( $this->mImages, array( &$title, $html ) );
155 }
156
157
158 /**
159 * isEmpty() returns true if the gallery contains no images
160 */
161 function isEmpty() {
162 return empty( $this->mImages );
163 }
164
165 /**
166 * Enable/Disable showing of the file size of an image in the gallery.
167 * Enabled by default.
168 *
169 * @param $f Boolean: set to false to disable.
170 */
171 function setShowBytes( $f ) {
172 $this->mShowBytes = ( $f == true);
173 }
174
175 /**
176 * Enable/Disable showing of the filename of an image in the gallery.
177 * Enabled by default.
178 *
179 * @param $f Boolean: set to false to disable.
180 */
181 function setShowFilename( $f ) {
182 $this->mShowFilename = ( $f == true);
183 }
184
185 /**
186 * Return a HTML representation of the image gallery
187 *
188 * For each image in the gallery, display
189 * - a thumbnail
190 * - the image name
191 * - the additional text provided when adding the image
192 * - the size of the image
193 *
194 */
195 function toHTML() {
196 global $wgLang;
197
198 $sk = $this->getSkin();
199
200 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
201 if( $this->mCaption )
202 $s .= "\n\t<caption>{$this->mCaption}</caption>";
203
204 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
205 $i = 0;
206 foreach ( $this->mImages as $pair ) {
207 $nt = $pair[0];
208 $text = $pair[1];
209
210 # Give extensions a chance to select the file revision for us
211 $time = false;
212 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time ) );
213
214 $img = wfFindFile( $nt, $time );
215
216 if( $nt->getNamespace() != NS_IMAGE || !$img ) {
217 # We're dealing with a non-image, spit out the name and be done with it.
218 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
219 . htmlspecialchars( $nt->getText() ) . '</div>';
220 } elseif( $this->mParsing && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
221 # The image is blacklisted, just show it as a text link.
222 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
223 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
224 } elseif( !( $thumb = $img->transform( $params ) ) ) {
225 # Error generating thumbnail.
226 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
227 . htmlspecialchars( $img->getLastError() ) . '</div>';
228 } else {
229 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
230 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+30).'px;">'
231 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
232 }
233
234 //TODO
235 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
236
237 if( $this->mShowBytes ) {
238 if( $img ) {
239 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
240 $wgLang->formatNum( $img->getSize() ) );
241 } else {
242 $nb = wfMsgHtml( 'filemissing' );
243 }
244 $nb = "$nb<br />\n";
245 } else {
246 $nb = '';
247 }
248
249 $textlink = $this->mShowFilename ?
250 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
251 '' ;
252
253 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
254 # in version 4.8.6 generated crackpot html in its absence, see:
255 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
256
257 if ( $i % $this->mPerRow == 0 ) {
258 $s .= "\n\t<tr>";
259 }
260 $s .=
261 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths*1.25).'px;">'
262 . $thumbhtml
263 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
264 . $textlink . $text . $nb
265 . "\n\t\t\t</div>"
266 . "\n\t\t</div></td>";
267 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
268 $s .= "\n\t</tr>";
269 }
270 ++$i;
271 }
272 if( $i % $this->mPerRow != 0 ) {
273 $s .= "\n\t</tr>";
274 }
275 $s .= "\n</table>";
276
277 return $s;
278 }
279
280 /**
281 * @return int Number of images in the gallery
282 */
283 public function count() {
284 return count( $this->mImages );
285 }
286
287 /**
288 * Set the contextual title
289 *
290 * @param Title $title Contextual title
291 */
292 public function setContextTitle( $title ) {
293 $this->contextTitle = $title;
294 }
295
296 /**
297 * Get the contextual title, if applicable
298 *
299 * @return mixed Title or false
300 */
301 public function getContextTitle() {
302 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
303 ? $this->contextTitle
304 : false;
305 }
306
307 } //class
308 ?>