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