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