00001 <?php
00002 if ( ! defined( 'MEDIAWIKI' ) )
00003 die( 1 );
00004
00012 class ImageGallery
00013 {
00014 var $mImages, $mShowBytes, $mShowFilename;
00015 var $mCaption = false;
00016 var $mSkin = false;
00017 var $mRevisionId = 0;
00018
00022 var $mHideBadImages;
00023
00027 var $mParser;
00028
00033 private $contextTitle = false;
00034
00035 private $mPerRow = 4;
00036 private $mWidths = 120, $mHeights = 120;
00037
00038 private $mAttribs = array();
00039
00043 function __construct( ) {
00044 $this->mImages = array();
00045 $this->mShowBytes = true;
00046 $this->mShowFilename = true;
00047 $this->mParser = false;
00048 $this->mHideBadImages = false;
00049 }
00050
00054 function setParser( $parser ) {
00055 $this->mParser = $parser;
00056 }
00057
00061 function setHideBadImages( $flag = true ) {
00062 $this->mHideBadImages = $flag;
00063 }
00064
00070 function setCaption( $caption ) {
00071 $this->mCaption = htmlspecialchars( $caption );
00072 }
00073
00079 public function setCaptionHtml( $caption ) {
00080 $this->mCaption = $caption;
00081 }
00082
00088 public function setPerRow( $num ) {
00089 if ($num > 0) {
00090 $this->mPerRow = (int)$num;
00091 }
00092 }
00093
00099 public function setWidths( $num ) {
00100 if ($num > 0) {
00101 $this->mWidths = (int)$num;
00102 }
00103 }
00104
00110 public function setHeights( $num ) {
00111 if ($num > 0) {
00112 $this->mHeights = (int)$num;
00113 }
00114 }
00115
00121 function useSkin( $skin ) {
00122 $this->mSkin = $skin;
00123 }
00124
00130 function getSkin() {
00131 if( !$this->mSkin ) {
00132 global $wgUser;
00133 $skin = $wgUser->getSkin();
00134 } else {
00135 $skin = $this->mSkin;
00136 }
00137 return $skin;
00138 }
00139
00146 function add( $title, $html='' ) {
00147 if ( $title instanceof File ) {
00148
00149 $title = $title->getTitle();
00150 }
00151 $this->mImages[] = array( $title, $html );
00152 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
00153 }
00154
00161 function insert( $title, $html='' ) {
00162 if ( $title instanceof File ) {
00163
00164 $title = $title->getTitle();
00165 }
00166 array_unshift( $this->mImages, array( &$title, $html ) );
00167 }
00168
00169
00173 function isEmpty() {
00174 return empty( $this->mImages );
00175 }
00176
00183 function setShowBytes( $f ) {
00184 $this->mShowBytes = ( $f == true);
00185 }
00186
00193 function setShowFilename( $f ) {
00194 $this->mShowFilename = ( $f == true);
00195 }
00196
00206 function setAttributes( $attribs ) {
00207 $this->mAttribs = $attribs;
00208 }
00209
00220 function toHTML() {
00221 global $wgLang;
00222
00223 $sk = $this->getSkin();
00224
00225 $attribs = Sanitizer::mergeAttributes(
00226 array(
00227 'class' => 'gallery',
00228 'cellspacing' => '0',
00229 'cellpadding' => '0' ),
00230 $this->mAttribs );
00231 $s = Xml::openElement( 'table', $attribs );
00232 if( $this->mCaption )
00233 $s .= "\n\t<caption>{$this->mCaption}</caption>";
00234
00235 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
00236 $i = 0;
00237 foreach ( $this->mImages as $pair ) {
00238 $nt = $pair[0];
00239 $text = $pair[1];
00240
00241 # Give extensions a chance to select the file revision for us
00242 $time = $descQuery = false;
00243 wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time, &$descQuery ) );
00244
00245 $img = wfFindFile( $nt, $time );
00246
00247 if( $nt->getNamespace() != NS_FILE || !$img ) {
00248 # We're dealing with a non-image, spit out the name and be done with it.
00249 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
00250 . htmlspecialchars( $nt->getText() ) . '</div>';
00251 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
00252 # The image is blacklisted, just show it as a text link.
00253 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
00254 . $sk->makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
00255 } elseif( !( $thumb = $img->transform( $params ) ) ) {
00256 # Error generating thumbnail.
00257 $thumbhtml = "\n\t\t\t".'<div style="height: '.($this->mHeights*1.25+2).'px;">'
00258 . htmlspecialchars( $img->getLastError() ) . '</div>';
00259 } else {
00260 $vpad = floor( ( 1.25*$this->mHeights - $thumb->height ) /2 ) - 2;
00261
00262 $thumbhtml = "\n\t\t\t".
00263 '<div class="thumb" style="padding: ' . $vpad . 'px 0; width: ' .($this->mWidths+30).'px;">'
00264 # Auto-margin centering for block-level elements. Needed now that we have video
00265 # handlers since they may emit block-level elements as opposed to simple <img> tags.
00266 # ref http:
00267 . '<div style="margin-left: auto; margin-right: auto; width: ' .$this->mWidths.'px;">'
00268 . $thumb->toHtml( array( 'desc-link' => true, 'desc-query' => $descQuery ) ) . '</div></div>';
00269
00270
00271 if ( $this->mParser && $img->getHandler() ) {
00272 $img->getHandler()->parserTransformHook( $this->mParser, $img );
00273 }
00274 }
00275
00276
00277
00278
00279 if( $this->mShowBytes ) {
00280 if( $img ) {
00281 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
00282 $wgLang->formatNum( $img->getSize() ) );
00283 } else {
00284 $nb = wfMsgHtml( 'filemissing' );
00285 }
00286 $nb = "$nb<br />\n";
00287 } else {
00288 $nb = '';
00289 }
00290
00291 $textlink = $this->mShowFilename ?
00292 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20 ) ) ) . "<br />\n" :
00293 '' ;
00294
00295 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
00296 # in version 4.8.6 generated crackpot html in its absence, see:
00297 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
00298
00299 if ( $i % $this->mPerRow == 0 ) {
00300 $s .= "\n\t<tr>";
00301 }
00302 $s .=
00303 "\n\t\t" . '<td><div class="gallerybox" style="width: '.($this->mWidths+35).'px;">'
00304 . $thumbhtml
00305 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
00306 . $textlink . $text . $nb
00307 . "\n\t\t\t</div>"
00308 . "\n\t\t</div></td>";
00309 if ( $i % $this->mPerRow == $this->mPerRow - 1 ) {
00310 $s .= "\n\t</tr>";
00311 }
00312 ++$i;
00313 }
00314 if( $i % $this->mPerRow != 0 ) {
00315 $s .= "\n\t</tr>";
00316 }
00317 $s .= "\n</table>";
00318
00319 return $s;
00320 }
00321
00325 public function count() {
00326 return count( $this->mImages );
00327 }
00328
00334 public function setContextTitle( $title ) {
00335 $this->contextTitle = $title;
00336 }
00337
00343 public function getContextTitle() {
00344 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
00345 ? $this->contextTitle
00346 : false;
00347 }
00348
00349 }