Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[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 * Create a new image gallery object.
27 */
28 function __construct( ) {
29 $this->mImages = array();
30 $this->mShowBytes = true;
31 $this->mShowFilename = true;
32 $this->mParsing = false;
33 }
34
35 /**
36 * Set the "parse" bit so we know to hide "bad" images
37 */
38 function setParsing( $val = true ) {
39 $this->mParsing = $val;
40 }
41
42 /**
43 * Set the caption (as plain text)
44 *
45 * @param $caption Caption
46 */
47 function setCaption( $caption ) {
48 $this->mCaption = htmlspecialchars( $caption );
49 }
50
51 /**
52 * Set the caption (as HTML)
53 *
54 * @param $caption Caption
55 */
56 function setCaptionHtml( $caption ) {
57 $this->mCaption = $caption;
58 }
59
60 /**
61 * Instruct the class to use a specific skin for rendering
62 *
63 * @param $skin Skin object
64 */
65 function useSkin( $skin ) {
66 $this->mSkin =& $skin;
67 }
68
69 /**
70 * Return the skin that should be used
71 *
72 * @return Skin object
73 */
74 function getSkin() {
75 if( !$this->mSkin ) {
76 global $wgUser;
77 $skin =& $wgUser->getSkin();
78 } else {
79 $skin =& $this->mSkin;
80 }
81 return $skin;
82 }
83
84 /**
85 * Add an image to the gallery.
86 *
87 * @param $image Image object that is added to the gallery
88 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
89 */
90 function add( $image, $html='' ) {
91 $this->mImages[] = array( &$image, $html );
92 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
93 }
94
95 /**
96 * Add an image at the beginning of the gallery.
97 *
98 * @param $image Image object that is added to the gallery
99 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
100 */
101 function insert( $image, $html='' ) {
102 array_unshift( $this->mImages, array( &$image, $html ) );
103 }
104
105
106 /**
107 * isEmpty() returns true if the gallery contains no images
108 */
109 function isEmpty() {
110 return empty( $this->mImages );
111 }
112
113 /**
114 * Enable/Disable showing of the file size of an image in the gallery.
115 * Enabled by default.
116 *
117 * @param $f Boolean: set to false to disable.
118 */
119 function setShowBytes( $f ) {
120 $this->mShowBytes = ( $f == true);
121 }
122
123 /**
124 * Enable/Disable showing of the filename of an image in the gallery.
125 * Enabled by default.
126 *
127 * @param $f Boolean: set to false to disable.
128 */
129 function setShowFilename( $f ) {
130 $this->mShowFilename = ( $f == true);
131 }
132
133 /**
134 * Return a HTML representation of the image gallery
135 *
136 * For each image in the gallery, display
137 * - a thumbnail
138 * - the image name
139 * - the additional text provided when adding the image
140 * - the size of the image
141 *
142 */
143 function toHTML() {
144 global $wgLang, $wgGenerateThumbnailOnParse;
145
146 $sk = $this->getSkin();
147
148 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
149 if( $this->mCaption )
150 $s .= '<td class="galleryheader" colspan="4"><big>' . $this->mCaption . '</big></td>';
151
152 $i = 0;
153 foreach ( $this->mImages as $pair ) {
154 $img =& $pair[0];
155 $text = $pair[1];
156
157 $nt = $img->getTitle();
158
159 if( $nt->getNamespace() != NS_IMAGE ) {
160 # We're dealing with a non-image, spit out the name and be done with it.
161 $thumbhtml = '<div style="height: 152px;">' . htmlspecialchars( $nt->getText() ) . '</div>';
162 }
163 else if( $this->mParsing && wfIsBadImage( $nt->getDBkey() ) ) {
164 # The image is blacklisted, just show it as a text link.
165 $thumbhtml = '<div style="height: 152px;">'
166 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
167 } else if( !( $thumb = $img->getThumbnail( 120, 120, $wgGenerateThumbnailOnParse ) ) ) {
168 # Error generating thumbnail.
169 $thumbhtml = '<div style="height: 152px;">'
170 . htmlspecialchars( $img->getLastError() ) . '</div>';
171 }
172 else {
173 $vpad = floor( ( 150 - $thumb->height ) /2 ) - 2;
174 $thumbhtml = '<div class="thumb" style="padding: ' . $vpad . 'px 0;">'
175 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
176 }
177
178 //TODO
179 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
180
181 if( $this->mShowBytes ) {
182 if( $img->exists() ) {
183 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
184 $wgLang->formatNum( $img->getSize() ) );
185 } else {
186 $nb = wfMsgHtml( 'filemissing' );
187 }
188 $nb = "$nb<br />\n";
189 } else {
190 $nb = '';
191 }
192
193 $textlink = $this->mShowFilename ?
194 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
195 '' ;
196
197 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
198 # in version 4.8.6 generated crackpot html in its absence, see:
199 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
200
201 $s .= ($i%4==0) ? '<tr>' : '';
202 $s .= '<td><div class="gallerybox">' . $thumbhtml
203 . '<div class="gallerytext">' . "\n" . $textlink . $text . $nb
204 . "</div></div></td>\n";
205 $s .= ($i%4==3) ? '</tr>' : '';
206 $i++;
207 }
208 if( $i %4 != 0 ) {
209 $s .= "</tr>\n";
210 }
211 $s .= '</table>';
212
213 return $s;
214 }
215
216 /**
217 * @return int Number of images in the gallery
218 */
219 public function count() {
220 return count( $this->mImages );
221 }
222
223 } //class
224 ?>