Make it show email as required if you choose to email a random password.
[lhc/web/wiklou.git] / includes / ImageGallery.php
1 <?php
2 /**
3 * Image gallery.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Image gallery
25 *
26 * Add images to the gallery using add(), then render that list to HTML using toHTML().
27 *
28 * @ingroup Media
29 */
30 class ImageGallery {
31 var $mImages, $mShowBytes, $mShowFilename;
32 var $mCaption = false;
33
34 /**
35 * Hide blacklisted images?
36 */
37 var $mHideBadImages;
38
39 /**
40 * Registered parser object for output callbacks
41 * @var Parser
42 */
43 var $mParser;
44
45 /**
46 * Contextual title, used when images are being screened
47 * against the bad image list
48 */
49 protected $contextTitle = false;
50
51 protected $mAttribs = array();
52
53 /**
54 * Fixed margins
55 */
56 const THUMB_PADDING = 30;
57 const GB_PADDING = 5;
58 // 2px borders on each side + 2px implied padding on each side
59 const GB_BORDERS = 8;
60
61 /**
62 * Create a new image gallery object.
63 */
64 function __construct() {
65 global $wgGalleryOptions;
66 $this->mImages = array();
67 $this->mShowBytes = $wgGalleryOptions['showBytes'];
68 $this->mShowFilename = true;
69 $this->mParser = false;
70 $this->mHideBadImages = false;
71 $this->mPerRow = $wgGalleryOptions['imagesPerRow'];
72 $this->mWidths = $wgGalleryOptions['imageWidth'];
73 $this->mHeights = $wgGalleryOptions['imageHeight'];
74 $this->mCaptionLength = $wgGalleryOptions['captionLength'];
75 }
76
77 /**
78 * Register a parser object
79 *
80 * @param $parser Parser
81 */
82 function setParser( $parser ) {
83 $this->mParser = $parser;
84 }
85
86 /**
87 * Set bad image flag
88 */
89 function setHideBadImages( $flag = true ) {
90 $this->mHideBadImages = $flag;
91 }
92
93 /**
94 * Set the caption (as plain text)
95 *
96 * @param string $caption Caption
97 */
98 function setCaption( $caption ) {
99 $this->mCaption = htmlspecialchars( $caption );
100 }
101
102 /**
103 * Set the caption (as HTML)
104 *
105 * @param string $caption Caption
106 */
107 public function setCaptionHtml( $caption ) {
108 $this->mCaption = $caption;
109 }
110
111 /**
112 * Set how many images will be displayed per row.
113 *
114 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
115 * invalid numbers will be rejected
116 */
117 public function setPerRow( $num ) {
118 if ( $num >= 0 ) {
119 $this->mPerRow = (int)$num;
120 }
121 }
122
123 /**
124 * Set how wide each image will be, in pixels.
125 *
126 * @param $num Integer > 0; invalid numbers will be ignored
127 */
128 public function setWidths( $num ) {
129 if ( $num > 0 ) {
130 $this->mWidths = (int)$num;
131 }
132 }
133
134 /**
135 * Set how high each image will be, in pixels.
136 *
137 * @param $num Integer > 0; invalid numbers will be ignored
138 */
139 public function setHeights( $num ) {
140 if ( $num > 0 ) {
141 $this->mHeights = (int)$num;
142 }
143 }
144
145 /**
146 * Instruct the class to use a specific skin for rendering
147 *
148 * @param $skin Skin object
149 * @deprecated since 1.18 Not used anymore
150 */
151 function useSkin( $skin ) {
152 wfDeprecated( __METHOD__, '1.18' );
153 /* no op */
154 }
155
156 /**
157 * Add an image to the gallery.
158 *
159 * @param $title Title object of the image that is added to the gallery
160 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
161 * @param $alt String: Alt text for the image
162 * @param $link String: Override image link (optional)
163 */
164 function add( $title, $html = '', $alt = '', $link = '' ) {
165 if ( $title instanceof File ) {
166 // Old calling convention
167 $title = $title->getTitle();
168 }
169 $this->mImages[] = array( $title, $html, $alt, $link );
170 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
171 }
172
173 /**
174 * Add an image at the beginning of the gallery.
175 *
176 * @param $title Title object of the image that is added to the gallery
177 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
178 * @param $alt String: Alt text for the image
179 */
180 function insert( $title, $html = '', $alt = '' ) {
181 if ( $title instanceof File ) {
182 // Old calling convention
183 $title = $title->getTitle();
184 }
185 array_unshift( $this->mImages, array( &$title, $html, $alt ) );
186 }
187
188 /**
189 * isEmpty() returns true if the gallery contains no images
190 * @return bool
191 */
192 function isEmpty() {
193 return empty( $this->mImages );
194 }
195
196 /**
197 * Enable/Disable showing of the file size of an image in the gallery.
198 * Enabled by default.
199 *
200 * @param $f Boolean: set to false to disable.
201 */
202 function setShowBytes( $f ) {
203 $this->mShowBytes = (bool)$f;
204 }
205
206 /**
207 * Enable/Disable showing of the filename of an image in the gallery.
208 * Enabled by default.
209 *
210 * @param $f Boolean: set to false to disable.
211 */
212 function setShowFilename( $f ) {
213 $this->mShowFilename = (bool)$f;
214 }
215
216 /**
217 * Set arbitrary attributes to go on the HTML gallery output element.
218 * Should be suitable for a <ul> element.
219 *
220 * Note -- if taking from user input, you should probably run through
221 * Sanitizer::validateAttributes() first.
222 *
223 * @param array $attribs of HTML attribute pairs
224 */
225 function setAttributes( $attribs ) {
226 $this->mAttribs = $attribs;
227 }
228
229 /**
230 * Return a HTML representation of the image gallery
231 *
232 * For each image in the gallery, display
233 * - a thumbnail
234 * - the image name
235 * - the additional text provided when adding the image
236 * - the size of the image
237 *
238 * @return string
239 */
240 function toHTML() {
241 if ( $this->mPerRow > 0 ) {
242 $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS );
243 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : '';
244 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
245 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
246 }
247
248 $attribs = Sanitizer::mergeAttributes(
249 array( 'class' => 'gallery' ), $this->mAttribs );
250
251 $output = Xml::openElement( 'ul', $attribs );
252 if ( $this->mCaption ) {
253 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
254 }
255
256 $lang = $this->getLang();
257 $params = array(
258 'width' => $this->mWidths,
259 'height' => $this->mHeights
260 );
261 # Output each image...
262 foreach ( $this->mImages as $pair ) {
263 $nt = $pair[0];
264 $text = $pair[1]; # "text" means "caption" here
265 $alt = $pair[2];
266 $link = $pair[3];
267
268 $descQuery = false;
269 if ( $nt->getNamespace() == NS_FILE ) {
270 # Get the file...
271 if ( $this->mParser instanceof Parser ) {
272 # Give extensions a chance to select the file revision for us
273 $options = array();
274 wfRunHooks( 'BeforeParserFetchFileAndTitle',
275 array( $this->mParser, $nt, &$options, &$descQuery ) );
276 # Fetch and register the file (file title may be different via hooks)
277 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
278 } else {
279 $img = wfFindFile( $nt );
280 }
281 } else {
282 $img = false;
283 }
284
285 if ( !$img ) {
286 # We're dealing with a non-image, spit out the name and be done with it.
287 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">'
288 . htmlspecialchars( $nt->getText() ) . '</div>';
289 } elseif ( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
290 # The image is blacklisted, just show it as a text link.
291 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' .
292 Linker::link(
293 $nt,
294 htmlspecialchars( $nt->getText() ),
295 array(),
296 array(),
297 array( 'known', 'noclasses' )
298 ) .
299 '</div>';
300 } elseif ( !( $thumb = $img->transform( $params ) ) ) {
301 # Error generating thumbnail.
302 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">'
303 . htmlspecialchars( $img->getLastError() ) . '</div>';
304 } else {
305 $vpad = ( self::THUMB_PADDING + $this->mHeights - $thumb->height ) / 2;
306
307 $imageParameters = array(
308 'desc-link' => true,
309 'desc-query' => $descQuery,
310 'alt' => $alt,
311 'custom-url-link' => $link
312 );
313 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
314 if ( $alt == '' && $text == '' ) {
315 $imageParameters['alt'] = $nt->getText();
316 }
317
318 # Set both fixed width and min-height.
319 $thumbhtml = "\n\t\t\t" .
320 '<div class="thumb" style="width: ' . ( $this->mWidths + self::THUMB_PADDING ) . 'px;">'
321 # Auto-margin centering for block-level elements. Needed now that we have video
322 # handlers since they may emit block-level elements as opposed to simple <img> tags.
323 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
324 . '<div style="margin:' . $vpad . 'px auto;">'
325 . $thumb->toHtml( $imageParameters ) . '</div></div>';
326
327 // Call parser transform hook
328 if ( $this->mParser && $img->getHandler() ) {
329 $img->getHandler()->parserTransformHook( $this->mParser, $img );
330 }
331 }
332
333 //TODO
334 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
335 // $ul = Linker::link( $linkTarget, $ut );
336
337 if ( $this->mShowBytes ) {
338 if ( $img ) {
339 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
340 } else {
341 $fileSize = wfMessage( 'filemissing' )->escaped();
342 }
343 $fileSize = "$fileSize<br />\n";
344 } else {
345 $fileSize = '';
346 }
347
348 $textlink = $this->mShowFilename ?
349 Linker::link(
350 $nt,
351 htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) ),
352 array(),
353 array(),
354 array( 'known', 'noclasses' )
355 ) . "<br />\n" :
356 '';
357
358 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
359 # in version 4.8.6 generated crackpot html in its absence, see:
360 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
361
362 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
363 # Can be safely removed if FF2 falls completely out of existence
364 $output .=
365 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
366 . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
367 . $thumbhtml
368 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
369 . $textlink . $text . $fileSize
370 . "\n\t\t\t</div>"
371 . "\n\t\t</div></li>";
372 }
373 $output .= "\n</ul>";
374
375 return $output;
376 }
377
378 /**
379 * @return Integer: number of images in the gallery
380 */
381 public function count() {
382 return count( $this->mImages );
383 }
384
385 /**
386 * Set the contextual title
387 *
388 * @param $title Title: contextual title
389 */
390 public function setContextTitle( $title ) {
391 $this->contextTitle = $title;
392 }
393
394 /**
395 * Get the contextual title, if applicable
396 *
397 * @return mixed Title or false
398 */
399 public function getContextTitle() {
400 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
401 ? $this->contextTitle
402 : false;
403 }
404
405 /**
406 * Determines the correct language to be used for this image gallery
407 * @return Language object
408 */
409 private function getLang() {
410 global $wgLang;
411 return $this->mParser
412 ? $this->mParser->getTargetLanguage()
413 : $wgLang;
414 }
415
416 } //class