036d71783c1aafbbda57b9fab7812d32362e02e9
2 if ( ! defined( 'MEDIAWIKI' ) )
11 * Add images to the gallery using add(), then render that list to HTML using toHTML().
16 var $mImages, $mShowBytes, $mShowFilename;
17 var $mCaption = false;
21 * Is the gallery on a wiki page (i.e. not a special page)
26 * Contextual title, used when images are being screened
27 * against the bad image list
29 private $contextTitle = false;
31 private $mPerRow = 4; // How many images wide should the gallery be?
32 private $mWidths = 120, $mHeights = 120; // How wide/tall each thumbnail should be
35 * Create a new image gallery object.
37 function __construct( ) {
38 $this->mImages
= array();
39 $this->mShowBytes
= true;
40 $this->mShowFilename
= true;
41 $this->mParsing
= false;
45 * Set the "parse" bit so we know to hide "bad" images
47 function setParsing( $val = true ) {
48 $this->mParsing
= $val;
52 * Set the caption (as plain text)
54 * @param $caption Caption
56 function setCaption( $caption ) {
57 $this->mCaption
= htmlspecialchars( $caption );
61 * Set the caption (as HTML)
63 * @param $caption Caption
65 public function setCaptionHtml( $caption ) {
66 $this->mCaption
= $caption;
70 * Set how many images will be displayed per row.
72 * @param int $num > 0; invalid numbers will be rejected
74 public function setPerRow( $num ) {
76 $this->mPerRow
= (int)$num;
81 * Set how wide each image will be, in pixels.
83 * @param int $num > 0; invalid numbers will be ignored
85 public function setWidths( $num ) {
87 $this->mWidths
= (int)$num;
92 * Set how high each image will be, in pixels.
94 * @param int $num > 0; invalid numbers will be ignored
96 public function setHeights( $num ) {
98 $this->mHeights
= (int)$num;
103 * Instruct the class to use a specific skin for rendering
105 * @param $skin Skin object
107 function useSkin( $skin ) {
108 $this->mSkin
= $skin;
112 * Return the skin that should be used
114 * @return Skin object
117 if( !$this->mSkin
) {
119 $skin = $wgUser->getSkin();
121 $skin = $this->mSkin
;
127 * Add an image to the gallery.
129 * @param $image Image object that is added to the gallery
130 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
132 function add( $image, $html='' ) {
133 $this->mImages
[] = array( &$image, $html );
134 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
138 * Add an image at the beginning of the gallery.
140 * @param $image Image object that is added to the gallery
141 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
143 function insert( $image, $html='' ) {
144 array_unshift( $this->mImages
, array( &$image, $html ) );
149 * isEmpty() returns true if the gallery contains no images
152 return empty( $this->mImages
);
156 * Enable/Disable showing of the file size of an image in the gallery.
157 * Enabled by default.
159 * @param $f Boolean: set to false to disable.
161 function setShowBytes( $f ) {
162 $this->mShowBytes
= ( $f == true);
166 * Enable/Disable showing of the filename of an image in the gallery.
167 * Enabled by default.
169 * @param $f Boolean: set to false to disable.
171 function setShowFilename( $f ) {
172 $this->mShowFilename
= ( $f == true);
176 * Return a HTML representation of the image gallery
178 * For each image in the gallery, display
181 * - the additional text provided when adding the image
182 * - the size of the image
188 $sk = $this->getSkin();
190 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
191 if( $this->mCaption
)
192 $s .= "\n\t<caption>{$this->mCaption}</caption>";
194 $params = array( 'width' => $this->mWidths
, 'height' => $this->mHeights
);
196 foreach ( $this->mImages
as $pair ) {
200 $nt = $img->getTitle();
202 if( $nt->getNamespace() != NS_IMAGE
) {
203 # We're dealing with a non-image, spit out the name and be done with it.
204 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
205 . htmlspecialchars( $nt->getText() ) . '</div>';
206 } elseif( $this->mParsing
&& wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
207 # The image is blacklisted, just show it as a text link.
208 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
209 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
210 } elseif( !( $thumb = $img->transform( $params ) ) ) {
211 # Error generating thumbnail.
212 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights
*1.25+
2).'px;">'
213 . htmlspecialchars( $img->getLastError() ) . '</div>';
215 $vpad = floor( ( 1.25*$this->mHeights
- $thumb->height
) /2 ) - 2;
216 $thumbhtml = "\n\t\t\t".'<div class="thumb" style="padding: ' . $vpad . 'px 0; width: '.($this->mWidths+
30).'px;">'
217 . $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
221 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
223 if( $this->mShowBytes
) {
224 if( $img->exists() ) {
225 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
226 $wgLang->formatNum( $img->getSize() ) );
228 $nb = wfMsgHtml( 'filemissing' );
235 $textlink = $this->mShowFilename ?
236 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
239 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
240 # in version 4.8.6 generated crackpot html in its absence, see:
241 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
243 if ( $i %
$this->mPerRow
== 0 ) {
247 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths
*1.25).'px;">'
249 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
250 . $textlink . $text . $nb
252 . "\n\t\t</div></td>";
253 if ( $i %
$this->mPerRow
== $this->mPerRow
- 1 ) {
258 if( $i %
$this->mPerRow
!= 0 ) {
267 * @return int Number of images in the gallery
269 public function count() {
270 return count( $this->mImages
);
274 * Set the contextual title
276 * @param Title $title Contextual title
278 public function setContextTitle( $title ) {
279 $this->contextTitle
= $title;
283 * Get the contextual title, if applicable
285 * @return mixed Title or false
287 public function getContextTitle() {
288 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
289 ?
$this->contextTitle