* (bug 8042) Make miser mode caching limits settable via $wgQueryCacheLimit
[mediawiki.git] / includes / ImageGallery.php
blobe88ab8be99d024ee4b706b557d095c933f0362f1
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
5 /**
6 * @package MediaWiki
7 */
9 /**
10 * Image gallery
12 * Add images to the gallery using add(), then render that list to HTML using toHTML().
14 * @package MediaWiki
16 class ImageGallery
18 var $mImages, $mShowBytes, $mShowFilename;
19 var $mCaption = false;
20 var $mSkin = false;
22 /**
23 * Is the gallery on a wiki page (i.e. not a special page)
25 var $mParsing;
27 /**
28 * Create a new image gallery object.
30 function ImageGallery( ) {
31 $this->mImages = array();
32 $this->mShowBytes = true;
33 $this->mShowFilename = true;
34 $this->mParsing = false;
37 /**
38 * Set the "parse" bit so we know to hide "bad" images
40 function setParsing( $val = true ) {
41 $this->mParsing = $val;
44 /**
45 * Set the caption
47 * @param $caption Caption
49 function setCaption( $caption ) {
50 $this->mCaption = $caption;
53 /**
54 * Instruct the class to use a specific skin for rendering
56 * @param $skin Skin object
58 function useSkin( $skin ) {
59 $this->mSkin =& $skin;
62 /**
63 * Return the skin that should be used
65 * @return Skin object
67 function getSkin() {
68 if( !$this->mSkin ) {
69 global $wgUser;
70 $skin =& $wgUser->getSkin();
71 } else {
72 $skin =& $this->mSkin;
74 return $skin;
77 /**
78 * Add an image to the gallery.
80 * @param $image Image object that is added to the gallery
81 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
83 function add( $image, $html='' ) {
84 $this->mImages[] = array( &$image, $html );
85 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
88 /**
89 * Add an image at the beginning of the gallery.
91 * @param $image Image object that is added to the gallery
92 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
94 function insert( $image, $html='' ) {
95 array_unshift( $this->mImages, array( &$image, $html ) );
99 /**
100 * isEmpty() returns true if the gallery contains no images
102 function isEmpty() {
103 return empty( $this->mImages );
107 * Enable/Disable showing of the file size of an image in the gallery.
108 * Enabled by default.
110 * @param $f Boolean: set to false to disable.
112 function setShowBytes( $f ) {
113 $this->mShowBytes = ( $f == true);
117 * Enable/Disable showing of the filename of an image in the gallery.
118 * Enabled by default.
120 * @param $f Boolean: set to false to disable.
122 function setShowFilename( $f ) {
123 $this->mShowFilename = ( $f == true);
127 * Return a HTML representation of the image gallery
129 * For each image in the gallery, display
130 * - a thumbnail
131 * - the image name
132 * - the additional text provided when adding the image
133 * - the size of the image
136 function toHTML() {
137 global $wgLang, $wgGenerateThumbnailOnParse;
139 $sk = $this->getSkin();
141 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
142 if( $this->mCaption )
143 $s .= '<td class="galleryheader" colspan="4"><big>' . htmlspecialchars( $this->mCaption ) . '</big></td>';
145 $i = 0;
146 foreach ( $this->mImages as $pair ) {
147 $img =& $pair[0];
148 $text = $pair[1];
150 $nt = $img->getTitle();
152 if( $nt->getNamespace() != NS_IMAGE ) {
153 # We're dealing with a non-image, spit out the name and be done with it.
154 $thumbhtml = '<div style="height: 152px;">' . htmlspecialchars( $nt->getText() ) . '</div>';
156 else if( $this->mParsing && wfIsBadImage( $nt->getDBkey() ) ) {
157 # The image is blacklisted, just show it as a text link.
158 $thumbhtml = '<div style="height: 152px;">'
159 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
160 } else if( !( $thumb = $img->getThumbnail( 120, 120, $wgGenerateThumbnailOnParse ) ) ) {
161 # Error generating thumbnail.
162 $thumbhtml = '<div style="height: 152px;">'
163 . htmlspecialchars( $img->getLastError() ) . '</div>';
165 else {
166 $vpad = floor( ( 150 - $thumb->height ) /2 ) - 2;
167 $thumbhtml = '<div class="thumb" style="padding: ' . $vpad . 'px 0;">'
168 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
171 //TODO
172 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
174 if( $this->mShowBytes ) {
175 if( $img->exists() ) {
176 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
177 $wgLang->formatNum( $img->getSize() ) );
178 } else {
179 $nb = wfMsgHtml( 'filemissing' );
181 $nb = "$nb<br />\n";
182 } else {
183 $nb = '';
186 $textlink = $this->mShowFilename ?
187 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
188 '' ;
190 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
191 # in version 4.8.6 generated crackpot html in its absence, see:
192 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
194 $s .= ($i%4==0) ? '<tr>' : '';
195 $s .= '<td><div class="gallerybox">' . $thumbhtml
196 . '<div class="gallerytext">' . "\n" . $textlink . $text . $nb
197 . "</div></div></td>\n";
198 $s .= ($i%4==3) ? '</tr>' : '';
199 $i++;
201 if( $i %4 != 0 ) {
202 $s .= "</tr>\n";
204 $s .= '</table>';
206 return $s;
210 * @return int Number of images in the gallery
212 public function count() {
213 return count( $this->mImages );
216 } //class