Reverting 17479/17480, breaks subclassing of Linker methods by skins. See my comment...
[lhc/web/wiklou.git] / includes / ImageGallery.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
4
5 /**
6 * @package MediaWiki
7 */
8
9 /**
10 * Image gallery
11 *
12 * Add images to the gallery using add(), then render that list to HTML using toHTML().
13 *
14 * @package MediaWiki
15 */
16 class ImageGallery
17 {
18 var $mImages, $mShowBytes, $mShowFilename;
19 var $mCaption = false;
20 var $mSkin = false;
21
22 /**
23 * Is the gallery on a wiki page (i.e. not a special page)
24 */
25 var $mParsing;
26
27 /**
28 * Create a new image gallery object.
29 */
30 function ImageGallery( ) {
31 $this->mImages = array();
32 $this->mShowBytes = true;
33 $this->mShowFilename = true;
34 $this->mParsing = false;
35 }
36
37 /**
38 * Set the "parse" bit so we know to hide "bad" images
39 */
40 function setParsing( $val = true ) {
41 $this->mParsing = $val;
42 }
43
44 /**
45 * Set the caption
46 *
47 * @param $caption Caption
48 */
49 function setCaption( $caption ) {
50 $this->mCaption = $caption;
51 }
52
53 /**
54 * Instruct the class to use a specific skin for rendering
55 *
56 * @param $skin Skin object
57 */
58 function useSkin( $skin ) {
59 $this->mSkin =& $skin;
60 }
61
62 /**
63 * Return the skin that should be used
64 *
65 * @return Skin object
66 */
67 function getSkin() {
68 if( !$this->mSkin ) {
69 global $wgUser;
70 $skin =& $wgUser->getSkin();
71 } else {
72 $skin =& $this->mSkin;
73 }
74 return $skin;
75 }
76
77 /**
78 * Add an image to the gallery.
79 *
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.
82 */
83 function add( $image, $html='' ) {
84 $this->mImages[] = array( &$image, $html );
85 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
86 }
87
88 /**
89 * Add an image at the beginning of the gallery.
90 *
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.
93 */
94 function insert( $image, $html='' ) {
95 array_unshift( $this->mImages, array( &$image, $html ) );
96 }
97
98
99 /**
100 * isEmpty() returns true if the gallery contains no images
101 */
102 function isEmpty() {
103 return empty( $this->mImages );
104 }
105
106 /**
107 * Enable/Disable showing of the file size of an image in the gallery.
108 * Enabled by default.
109 *
110 * @param $f Boolean: set to false to disable.
111 */
112 function setShowBytes( $f ) {
113 $this->mShowBytes = ( $f == true);
114 }
115
116 /**
117 * Enable/Disable showing of the filename of an image in the gallery.
118 * Enabled by default.
119 *
120 * @param $f Boolean: set to false to disable.
121 */
122 function setShowFilename( $f ) {
123 $this->mShowFilename = ( $f == true);
124 }
125
126 /**
127 * Return a HTML representation of the image gallery
128 *
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
134 *
135 */
136 function toHTML() {
137 global $wgLang, $wgIgnoreImageErrors, $wgGenerateThumbnailOnParse;
138
139 $sk = $this->getSkin();
140
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>';
144
145 $i = 0;
146 foreach ( $this->mImages as $pair ) {
147 $img =& $pair[0];
148 $text = $pair[1];
149
150 $name = $img->getName();
151 $nt = $img->getTitle();
152
153 if( $nt->getNamespace() != NS_IMAGE ) {
154 # We're dealing with a non-image, spit out the name and be done with it.
155 $thumbhtml = '<div style="height: 152px;">' . htmlspecialchars( $nt->getText() ) . '</div>';
156 }
157 else if( $this->mParsing && wfIsBadImage( $nt->getDBkey() ) ) {
158 # The image is blacklisted, just show it as a text link.
159 $thumbhtml = '<div style="height: 152px;">'
160 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
161 } else if( !( $thumb = $img->getThumbnail( 120, 120, $wgGenerateThumbnailOnParse ) ) ) {
162 # Error generating thumbnail.
163 $thumbhtml = '<div style="height: 152px;">'
164 . htmlspecialchars( $img->getLastError() ) . '</div>';
165 }
166 else {
167 $vpad = floor( ( 150 - $thumb->height ) /2 ) - 2;
168 $thumbhtml = '<div class="thumb" style="padding: ' . $vpad . 'px 0;">'
169 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
170 }
171
172 //TODO
173 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
174
175 if( $this->mShowBytes ) {
176 if( $img->exists() ) {
177 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
178 $wgLang->formatNum( $img->getSize() ) );
179 } else {
180 $nb = wfMsgHtml( 'filemissing' );
181 }
182 $nb = "$nb<br />\n";
183 } else {
184 $nb = '';
185 }
186
187 $textlink = $this->mShowFilename ?
188 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
189 '' ;
190
191 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
192 # in version 4.8.6 generated crackpot html in its absence, see:
193 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
194
195 $s .= ($i%4==0) ? '<tr>' : '';
196 $s .= '<td><div class="gallerybox">' . $thumbhtml
197 . '<div class="gallerytext">' . "\n" . $textlink . $text . $nb
198 . "</div></div></td>\n";
199 $s .= ($i%4==3) ? '</tr>' : '';
200 $i++;
201 }
202 if( $i %4 != 0 ) {
203 $s .= "</tr>\n";
204 }
205 $s .= '</table>';
206
207 return $s;
208 }
209
210 } //class
211 ?>