Restore hooks UploadForm:initial and UploadForm:BeforeProcessing removed in r57868.
[mediawiki.git] / includes / ImageGallery.php
blob5bff0ae3352c5bac3f7be1134e4cb81f2e2bb996
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 $mPerRow = 4; // How many images wide should the gallery be?
36 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
38 private $mAttribs = array();
40 /**
41 * Create a new image gallery object.
43 function __construct( ) {
44 $this->mImages = array();
45 $this->mShowBytes = true;
46 $this->mShowFilename = true;
47 $this->mParser = false;
48 $this->mHideBadImages = false;
51 /**
52 * Register a parser object
54 function setParser( $parser ) {
55 $this->mParser = $parser;
58 /**
59 * Set bad image flag
61 function setHideBadImages( $flag = true ) {
62 $this->mHideBadImages = $flag;
65 /**
66 * Set the caption (as plain text)
68 * @param $caption Caption
70 function setCaption( $caption ) {
71 $this->mCaption = htmlspecialchars( $caption );
74 /**
75 * Set the caption (as HTML)
77 * @param $caption Caption
79 public function setCaptionHtml( $caption ) {
80 $this->mCaption = $caption;
83 /**
84 * Set how many images will be displayed per row.
86 * @param int $num > 0; invalid numbers will be rejected
88 public function setPerRow( $num ) {
89 if ($num > 0) {
90 $this->mPerRow = (int)$num;
94 /**
95 * Set how wide each image will be, in pixels.
97 * @param int $num > 0; invalid numbers will be ignored
99 public function setWidths( $num ) {
100 if ($num > 0) {
101 $this->mWidths = (int)$num;
106 * Set how high each image will be, in pixels.
108 * @param int $num > 0; invalid numbers will be ignored
110 public function setHeights( $num ) {
111 if ($num > 0) {
112 $this->mHeights = (int)$num;
117 * Instruct the class to use a specific skin for rendering
119 * @param $skin Skin object
121 function useSkin( $skin ) {
122 $this->mSkin = $skin;
126 * Return the skin that should be used
128 * @return Skin object
130 function getSkin() {
131 if( !$this->mSkin ) {
132 global $wgUser;
133 $skin = $wgUser->getSkin();
134 } else {
135 $skin = $this->mSkin;
137 return $skin;
141 * Add an image to the gallery.
143 * @param $title Title object of the image that is added to the gallery
144 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
146 function add( $title, $html='' ) {
147 if ( $title instanceof File ) {
148 // Old calling convention
149 $title = $title->getTitle();
151 $this->mImages[] = array( $title, $html );
152 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
156 * Add an image at the beginning of the gallery.
158 * @param $title Title object of the image that is added to the gallery
159 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
161 function insert( $title, $html='' ) {
162 if ( $title instanceof File ) {
163 // Old calling convention
164 $title = $title->getTitle();
166 array_unshift( $this->mImages, array( &$title, $html ) );
171 * isEmpty() returns true if the gallery contains no images
173 function isEmpty() {
174 return empty( $this->mImages );
178 * Enable/Disable showing of the file size of an image in the gallery.
179 * Enabled by default.
181 * @param $f Boolean: set to false to disable.
183 function setShowBytes( $f ) {
184 $this->mShowBytes = ( $f == true);
188 * Enable/Disable showing of the filename of an image in the gallery.
189 * Enabled by default.
191 * @param $f Boolean: set to false to disable.
193 function setShowFilename( $f ) {
194 $this->mShowFilename = ( $f == true);
198 * Set arbitrary attributes to go on the HTML gallery output element.
199 * Should be suitable for a &lt;table&gt; element.
201 * Note -- if taking from user input, you should probably run through
202 * Sanitizer::validateAttributes() first.
204 * @param array of HTML attribute pairs
206 function setAttributes( $attribs ) {
207 $this->mAttribs = $attribs;
211 * Return a HTML representation of the image gallery
213 * For each image in the gallery, display
214 * - a thumbnail
215 * - the image name
216 * - the additional text provided when adding the image
217 * - the size of the image
220 function toHTML() {
221 global $wgLang;
223 $sk = $this->getSkin();
225 $attribs = Sanitizer::mergeAttributes(
226 array(
227 'class' => 'gallery',
228 'cellspacing' => '0',
229 'cellpadding' => '0' ),
230 $this->mAttribs );
231 $s = Xml::openElement( 'table', $attribs );
232 if( $this->mCaption )
233 $s .= "\n\t<caption>{$this->mCaption}</caption>";
235 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
236 $i = 0;
237 foreach ( $this->mImages as $pair ) {
238 $nt = $pair[0];
239 $text = $pair[1]; # "text" means "caption" here
241 # Give extensions a chance to select the file revision for us
242 $time = $descQuery = false;
243 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
245 $img = wfFindFile( $nt, array( 'time' => $time ) );
247 if( $nt->getNamespace() != NS_FILE || !$img ) {
248 # We're dealing with a non-image, spit out the name and be done with it.
249 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
250 . htmlspecialchars( $nt->getText() ) . '</div>';
251 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
252 # The image is blacklisted, just show it as a text link.
253 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">' .
254 $sk->link(
255 $nt,
256 htmlspecialchars( $nt->getText() ),
257 array(),
258 array(),
259 array( 'known', 'noclasses' )
261 '</div>';
262 } elseif( !( $thumb = $img->transform( $params ) ) ) {
263 # Error generating thumbnail.
264 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
265 . htmlspecialchars( $img->getLastError() ) . '</div>';
266 } else {
267 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
269 $imageParameters = array(
270 'desc-link' => true,
271 'desc-query' => $descQuery
273 # In the absence of a caption, fall back on providing screen readers with the filename as alt text
274 if ( $text == '' ) {
275 $imageParameters['alt'] = $nt->getText();
278 $thumbhtml = "\n\t\t\t".
279 '<div class="thumb" style="padding: ' . $vpad . 'px 0; width: ' .($this->mWidths+30).'px;">'
280 # Auto-margin centering for block-level elements. Needed now that we have video
281 # handlers since they may emit block-level elements as opposed to simple <img> tags.
282 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
283 . '<div style="margin-left: auto; margin-right: auto; width: ' .$this->mWidths.'px;">'
284 . $thumb->toHtml( $imageParameters ) . '</div></div>';
286 // Call parser transform hook
287 if ( $this->mParser && $img->getHandler() ) {
288 $img->getHandler()->parserTransformHook( $this->mParser, $img );
292 //TODO
293 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
294 // $ul = $sk->link( $linkTarget, $ut );
296 if( $this->mShowBytes ) {
297 if( $img ) {
298 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
299 $wgLang->formatNum( $img->getSize() ) );
300 } else {
301 $nb = wfMsgHtml( 'filemissing' );
303 $nb = "$nb<br />\n";
304 } else {
305 $nb = '';
308 $textlink = $this->mShowFilename ?
309 $sk->link(
310 $nt,
311 htmlspecialchars( $wgLang->truncate( $nt->getText(), 20 ) ),
312 array(),
313 array(),
314 array( 'known', 'noclasses' )
315 ) . "<br />\n" :
316 '' ;
318 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
319 # in version 4.8.6 generated crackpot html in its absence, see:
320 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
322 if ( $i % $this->mPerRow == 0 ) {
323 $s .= "\n\t<tr>";
325 $s .=
326 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths+35).'px;">'
327 . $thumbhtml
328 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
329 . $textlink . $text . $nb
330 . "\n\t\t\t</div>"
331 . "\n\t\t</div></td>";
332 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
333 $s .= "\n\t</tr>";
335 ++$i;
337 if( $i % $this->mPerRow != 0 ) {
338 $s .= "\n\t</tr>";
340 $s .= "\n</table>";
342 return $s;
346 * @return int Number of images in the gallery
348 public function count() {
349 return count( $this->mImages );
353 * Set the contextual title
355 * @param Title $title Contextual title
357 public function setContextTitle( $title ) {
358 $this->contextTitle = $title;
362 * Get the contextual title, if applicable
364 * @return mixed Title or false
366 public function getContextTitle() {
367 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
368 ? $this->contextTitle
369 : false;
372 } //class