"<p id="userloginlink"><p>Don't have an account" is illegal xhtml
[mediawiki.git] / includes / ImageGallery.php
blob3e6e3b94f7c8058670a723fd4418e5355afbbd4d
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
5 /**
6 * Image gallery
8 * Add images to the gallery using add(), then render that list to HTML using toHTML().
10 * @ingroup Media
12 class ImageGallery
14 var $mImages, $mShowBytes, $mShowFilename;
15 var $mCaption = false;
16 var $mSkin = false;
17 var $mRevisionId = 0;
19 /**
20 * Hide blacklisted images?
22 var $mHideBadImages;
24 /**
25 * Registered parser object for output callbacks
27 var $mParser;
29 /**
30 * Contextual title, used when images are being screened
31 * against the bad image list
33 private $contextTitle = false;
35 private $mAttribs = array();
37 /**
38 * Create a new image gallery object.
40 function __construct( ) {
41 global $wgGalleryOptions;
42 $this->mImages = array();
43 $this->mShowBytes = $wgGalleryOptions['showBytes'];
44 $this->mShowFilename = true;
45 $this->mParser = false;
46 $this->mHideBadImages = false;
47 $this->mPerRow = $wgGalleryOptions['imagesPerRow'];
48 $this->mWidths = $wgGalleryOptions['imageWidth'];
49 $this->mHeights = $wgGalleryOptions['imageHeight'];
50 $this->mCaptionLength = $wgGalleryOptions['captionLength'];
53 /**
54 * Register a parser object
56 function setParser( $parser ) {
57 $this->mParser = $parser;
60 /**
61 * Set bad image flag
63 function setHideBadImages( $flag = true ) {
64 $this->mHideBadImages = $flag;
67 /**
68 * Set the caption (as plain text)
70 * @param $caption Caption
72 function setCaption( $caption ) {
73 $this->mCaption = htmlspecialchars( $caption );
76 /**
77 * Set the caption (as HTML)
79 * @param $caption Caption
81 public function setCaptionHtml( $caption ) {
82 $this->mCaption = $caption;
85 /**
86 * Set how many images will be displayed per row.
88 * @param $num Integer > 0; invalid numbers will be rejected
90 public function setPerRow( $num ) {
91 if ($num > 0) {
92 $this->mPerRow = (int)$num;
96 /**
97 * Set how wide each image will be, in pixels.
99 * @param $num Integer > 0; invalid numbers will be ignored
101 public function setWidths( $num ) {
102 if ($num > 0) {
103 $this->mWidths = (int)$num;
108 * Set how high each image will be, in pixels.
110 * @param $num Integer > 0; invalid numbers will be ignored
112 public function setHeights( $num ) {
113 if ($num > 0) {
114 $this->mHeights = (int)$num;
119 * Instruct the class to use a specific skin for rendering
121 * @param $skin Skin object
123 function useSkin( $skin ) {
124 $this->mSkin = $skin;
128 * Return the skin that should be used
130 * @return Skin object
132 function getSkin() {
133 if( !$this->mSkin ) {
134 global $wgUser;
135 $skin = $wgUser->getSkin();
136 } else {
137 $skin = $this->mSkin;
139 return $skin;
143 * Add an image to the gallery.
145 * @param $title Title object of the image that is added to the gallery
146 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
148 function add( $title, $html='' ) {
149 if ( $title instanceof File ) {
150 // Old calling convention
151 $title = $title->getTitle();
153 $this->mImages[] = array( $title, $html );
154 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
158 * Add an image at the beginning of the gallery.
160 * @param $title Title object of the image that is added to the gallery
161 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
163 function insert( $title, $html='' ) {
164 if ( $title instanceof File ) {
165 // Old calling convention
166 $title = $title->getTitle();
168 array_unshift( $this->mImages, array( &$title, $html ) );
173 * isEmpty() returns true if the gallery contains no images
175 function isEmpty() {
176 return empty( $this->mImages );
180 * Enable/Disable showing of the file size of an image in the gallery.
181 * Enabled by default.
183 * @param $f Boolean: set to false to disable.
185 function setShowBytes( $f ) {
186 $this->mShowBytes = (bool)$f;
190 * Enable/Disable showing of the filename of an image in the gallery.
191 * Enabled by default.
193 * @param $f Boolean: set to false to disable.
195 function setShowFilename( $f ) {
196 $this->mShowFilename = (bool)$f;
200 * Set arbitrary attributes to go on the HTML gallery output element.
201 * Should be suitable for a &lt;table&gt; element.
203 * Note -- if taking from user input, you should probably run through
204 * Sanitizer::validateAttributes() first.
206 * @param $attribs Array of HTML attribute pairs
208 function setAttributes( $attribs ) {
209 $this->mAttribs = $attribs;
213 * Return a HTML representation of the image gallery
215 * For each image in the gallery, display
216 * - a thumbnail
217 * - the image name
218 * - the additional text provided when adding the image
219 * - the size of the image
222 function toHTML() {
223 global $wgLang;
225 $sk = $this->getSkin();
227 $attribs = Sanitizer::mergeAttributes(
228 array(
229 'class' => 'gallery',
230 'cellspacing' => '0',
231 'cellpadding' => '0' ),
232 $this->mAttribs );
233 $s = Xml::openElement( 'table', $attribs );
234 if( $this->mCaption )
235 $s .= "\n\t<caption>{$this->mCaption}</caption>";
237 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
238 $i = 0;
239 foreach ( $this->mImages as $pair ) {
240 $nt = $pair[0];
241 $text = $pair[1]; # "text" means "caption" here
243 # Give extensions a chance to select the file revision for us
244 $time = $descQuery = false;
245 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
247 $img = wfFindFile( $nt, array( 'time' => $time ) );
249 if( $nt->getNamespace() != NS_FILE || !$img ) {
250 # We're dealing with a non-image, spit out the name and be done with it.
251 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
252 . htmlspecialchars( $nt->getText() ) . '</div>';
253 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
254 # The image is blacklisted, just show it as a text link.
255 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">' .
256 $sk->link(
257 $nt,
258 htmlspecialchars( $nt->getText() ),
259 array(),
260 array(),
261 array( 'known', 'noclasses' )
263 '</div>';
264 } elseif( !( $thumb = $img->transform( $params ) ) ) {
265 # Error generating thumbnail.
266 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
267 . htmlspecialchars( $img->getLastError() ) . '</div>';
268 } else {
269 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
271 $imageParameters = array(
272 'desc-link' => true,
273 'desc-query' => $descQuery
275 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
276 if ( $text == '' ) {
277 $imageParameters['alt'] = $nt->getText();
280 $thumbhtml = "\n\t\t\t".
281 '<div class="thumb" style="padding: ' . $vpad . 'px 0; width: ' .($this->mWidths+30).'px;">'
282 # Auto-margin centering for block-level elements. Needed now that we have video
283 # handlers since they may emit block-level elements as opposed to simple <img> tags.
284 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
285 . '<div style="margin-left: auto; margin-right: auto; width: ' .$this->mWidths.'px;">'
286 . $thumb->toHtml( $imageParameters ) . '</div></div>';
288 // Call parser transform hook
289 if ( $this->mParser && $img->getHandler() ) {
290 $img->getHandler()->parserTransformHook( $this->mParser, $img );
294 //TODO
295 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
296 // $ul = $sk->link( $linkTarget, $ut );
298 if( $this->mShowBytes ) {
299 if( $img ) {
300 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
301 $wgLang->formatNum( $img->getSize() ) );
302 } else {
303 $nb = wfMsgHtml( 'filemissing' );
305 $nb = "$nb<br />\n";
306 } else {
307 $nb = '';
310 $textlink = $this->mShowFilename ?
311 $sk->link(
312 $nt,
313 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength ) ),
314 array(),
315 array(),
316 array( 'known', 'noclasses' )
317 ) . "<br />\n" :
318 '' ;
320 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
321 # in version 4.8.6 generated crackpot html in its absence, see:
322 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
324 if ( $i % $this->mPerRow == 0 ) {
325 $s .= "\n\t<tr>";
327 $s .=
328 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths+35).'px;">'
329 . $thumbhtml
330 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
331 . $textlink . $text . $nb
332 . "\n\t\t\t</div>"
333 . "\n\t\t</div></td>";
334 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
335 $s .= "\n\t</tr>";
337 ++$i;
339 if( $i % $this->mPerRow != 0 ) {
340 $s .= "\n\t</tr>";
342 $s .= "\n</table>";
344 return $s;
348 * @return Integer: number of images in the gallery
350 public function count() {
351 return count( $this->mImages );
355 * Set the contextual title
357 * @param $title Title: contextual title
359 public function setContextTitle( $title ) {
360 $this->contextTitle = $title;
364 * Get the contextual title, if applicable
366 * @return mixed Title or false
368 public function getContextTitle() {
369 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
370 ? $this->contextTitle
371 : false;
374 } //class