MediaWiki  REL1_19
ImageGallery.php
Go to the documentation of this file.
00001 <?php
00002 if ( ! defined( 'MEDIAWIKI' ) )
00003         die( 1 );
00004 
00012 class ImageGallery {
00013         var $mImages, $mShowBytes, $mShowFilename;
00014         var $mCaption = false;
00015 
00019         var $mHideBadImages;
00020 
00025         var $mParser;
00026 
00031         protected $contextTitle = false;
00032 
00033         protected $mAttribs = array();
00034 
00038         const THUMB_PADDING = 30;
00039         const GB_PADDING = 5;
00040         // 2px borders on each side + 2px implied padding on each side
00041         const GB_BORDERS = 8;
00042 
00046         function __construct() {
00047                 global $wgGalleryOptions;
00048                 $this->mImages = array();
00049                 $this->mShowBytes = $wgGalleryOptions['showBytes'];
00050                 $this->mShowFilename = true;
00051                 $this->mParser = false;
00052                 $this->mHideBadImages = false;
00053                 $this->mPerRow = $wgGalleryOptions['imagesPerRow'];
00054                 $this->mWidths = $wgGalleryOptions['imageWidth'];
00055                 $this->mHeights = $wgGalleryOptions['imageHeight'];
00056                 $this->mCaptionLength = $wgGalleryOptions['captionLength'];
00057         }
00058 
00064         function setParser( $parser ) {
00065                 $this->mParser = $parser;
00066         }
00067 
00071         function setHideBadImages( $flag = true ) {
00072                 $this->mHideBadImages = $flag;
00073         }
00074 
00080         function setCaption( $caption ) {
00081                 $this->mCaption = htmlspecialchars( $caption );
00082         }
00083 
00089         public function setCaptionHtml( $caption ) {
00090                 $this->mCaption = $caption;
00091         }
00092 
00099         public function setPerRow( $num ) {
00100                 if ( $num >= 0 ) {
00101                         $this->mPerRow = (int)$num;
00102                 }
00103         }
00104 
00110         public function setWidths( $num ) {
00111                 if ( $num > 0 ) {
00112                         $this->mWidths = (int)$num;
00113                 }
00114         }
00115 
00121         public function setHeights( $num ) {
00122                 if ( $num > 0 ) {
00123                         $this->mHeights = (int)$num;
00124                 }
00125         }
00126 
00133         function useSkin( $skin ) {
00134                 wfDeprecated( __METHOD__, '1.18' );
00135                 /* no op */
00136         }
00137 
00145         function add( $title, $html = '', $alt = '' ) {
00146                 if ( $title instanceof File ) {
00147                         // Old calling convention
00148                         $title = $title->getTitle();
00149                 }
00150                 $this->mImages[] = array( $title, $html, $alt );
00151                 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
00152         }
00153 
00161         function insert( $title, $html = '', $alt = '' ) {
00162                 if ( $title instanceof File ) {
00163                         // Old calling convention
00164                         $title = $title->getTitle();
00165                 }
00166                 array_unshift( $this->mImages, array( &$title, $html, $alt ) );
00167         }
00168 
00172         function isEmpty() {
00173                 return empty( $this->mImages );
00174         }
00175 
00182         function setShowBytes( $f ) {
00183                 $this->mShowBytes = (bool)$f;
00184         }
00185 
00192         function setShowFilename( $f ) {
00193                 $this->mShowFilename = (bool)$f;
00194         }
00195 
00205         function setAttributes( $attribs ) {
00206                 $this->mAttribs = $attribs;
00207         }
00208 
00219         function toHTML() {
00220                 global $wgLang;
00221 
00222                 if ( $this->mPerRow > 0 ) {
00223                         $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS );
00224                         $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : '';
00225                         # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
00226                         $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
00227                 }
00228 
00229                 $attribs = Sanitizer::mergeAttributes(
00230                         array( 'class' => 'gallery' ), $this->mAttribs );
00231 
00232                 $output = Xml::openElement( 'ul', $attribs );
00233                 if ( $this->mCaption ) {
00234                         $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
00235                 }
00236 
00237                 $params = array(
00238                         'width' => $this->mWidths,
00239                         'height' => $this->mHeights
00240                 );
00241                 # Output each image...
00242                 foreach ( $this->mImages as $pair ) {
00243                         $nt = $pair[0];
00244                         $text = $pair[1]; # "text" means "caption" here
00245                         $alt = $pair[2];
00246 
00247                         $descQuery = false;
00248                         if ( $nt->getNamespace() == NS_FILE ) {
00249                                 # Get the file...
00250                                 if ( $this->mParser instanceof Parser ) {
00251                                         # Give extensions a chance to select the file revision for us
00252                                         $options = array();
00253                                         wfRunHooks( 'BeforeParserFetchFileAndTitle',
00254                                                 array( $this->mParser, $nt, &$options, &$descQuery ) );
00255                                         # Fetch and register the file (file title may be different via hooks)
00256                                         list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
00257                                 } else {
00258                                         $img = wfFindFile( $nt );
00259                                 }
00260                         } else {
00261                                 $img = false;
00262                         }
00263 
00264                         if( !$img ) {
00265                                 # We're dealing with a non-image, spit out the name and be done with it.
00266                                 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">'
00267                                         . htmlspecialchars( $nt->getText() ) . '</div>';
00268                         } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
00269                                 # The image is blacklisted, just show it as a text link.
00270                                 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' .
00271                                         Linker::link(
00272                                                 $nt,
00273                                                 htmlspecialchars( $nt->getText() ),
00274                                                 array(),
00275                                                 array(),
00276                                                 array( 'known', 'noclasses' )
00277                                         ) .
00278                                         '</div>';
00279                         } elseif( !( $thumb = $img->transform( $params ) ) ) {
00280                                 # Error generating thumbnail.
00281                                 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">'
00282                                         . htmlspecialchars( $img->getLastError() ) . '</div>';
00283                         } else {
00284                                 $vpad = ( self::THUMB_PADDING + $this->mHeights - $thumb->height ) /2;
00285 
00286                                 $imageParameters = array(
00287                                         'desc-link' => true,
00288                                         'desc-query' => $descQuery,
00289                                         'alt' => $alt,
00290                                 );
00291                                 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
00292                                 if ( $alt == '' && $text == '' ) {
00293                                         $imageParameters['alt'] = $nt->getText();
00294                                 }
00295 
00296                                 # Set both fixed width and min-height.
00297                                 $thumbhtml = "\n\t\t\t" .
00298                                         '<div class="thumb" style="width: ' . ( $this->mWidths + self::THUMB_PADDING ) . 'px;">'
00299                                         # Auto-margin centering for block-level elements. Needed now that we have video
00300                                         # handlers since they may emit block-level elements as opposed to simple <img> tags.
00301                                         # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
00302                                         . '<div style="margin:' . $vpad . 'px auto;">'
00303                                         . $thumb->toHtml( $imageParameters ) . '</div></div>';
00304 
00305                                 // Call parser transform hook
00306                                 if ( $this->mParser && $img->getHandler() ) {
00307                                         $img->getHandler()->parserTransformHook( $this->mParser, $img );
00308                                 }
00309                         }
00310 
00311                         //TODO
00312                         // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
00313                         // $ul = Linker::link( $linkTarget, $ut );
00314 
00315                         if( $this->mShowBytes ) {
00316                                 if( $img ) {
00317                                         $fileSize = htmlspecialchars( $wgLang->formatSize( $img->getSize() ) );
00318                                 } else {
00319                                         $fileSize = wfMsgHtml( 'filemissing' );
00320                                 }
00321                                 $fileSize = "$fileSize<br />\n";
00322                         } else {
00323                                 $fileSize = '';
00324                         }
00325 
00326                         $textlink = $this->mShowFilename ?
00327                                 Linker::link(
00328                                         $nt,
00329                                         htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength ) ),
00330                                         array(),
00331                                         array(),
00332                                         array( 'known', 'noclasses' )
00333                                 ) . "<br />\n" :
00334                                 '' ;
00335 
00336                         # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
00337                         # in version 4.8.6 generated crackpot html in its absence, see:
00338                         # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
00339 
00340                         # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
00341                         # Can be safely removed if FF2 falls completely out of existance
00342                         $output .=
00343                                 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
00344                                         . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
00345                                         . $thumbhtml
00346                                         . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
00347                                                 . $textlink . $text . $fileSize
00348                                         . "\n\t\t\t</div>"
00349                                 . "\n\t\t</div></li>";
00350                 }
00351                 $output .= "\n</ul>";
00352 
00353                 return $output;
00354         }
00355 
00359         public function count() {
00360                 return count( $this->mImages );
00361         }
00362 
00368         public function setContextTitle( $title ) {
00369                 $this->contextTitle = $title;
00370         }
00371 
00377         public function getContextTitle() {
00378                 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
00379                                 ? $this->contextTitle
00380                                 : false;
00381         }
00382 
00383 } //class