MediaWiki
REL1_20
|
00001 <?php 00030 class ImageGallery { 00031 var $mImages, $mShowBytes, $mShowFilename; 00032 var $mCaption = false; 00033 00037 var $mHideBadImages; 00038 00043 var $mParser; 00044 00049 protected $contextTitle = false; 00050 00051 protected $mAttribs = array(); 00052 00056 const THUMB_PADDING = 30; 00057 const GB_PADDING = 5; 00058 // 2px borders on each side + 2px implied padding on each side 00059 const GB_BORDERS = 8; 00060 00064 function __construct() { 00065 global $wgGalleryOptions; 00066 $this->mImages = array(); 00067 $this->mShowBytes = $wgGalleryOptions['showBytes']; 00068 $this->mShowFilename = true; 00069 $this->mParser = false; 00070 $this->mHideBadImages = false; 00071 $this->mPerRow = $wgGalleryOptions['imagesPerRow']; 00072 $this->mWidths = $wgGalleryOptions['imageWidth']; 00073 $this->mHeights = $wgGalleryOptions['imageHeight']; 00074 $this->mCaptionLength = $wgGalleryOptions['captionLength']; 00075 } 00076 00082 function setParser( $parser ) { 00083 $this->mParser = $parser; 00084 } 00085 00089 function setHideBadImages( $flag = true ) { 00090 $this->mHideBadImages = $flag; 00091 } 00092 00098 function setCaption( $caption ) { 00099 $this->mCaption = htmlspecialchars( $caption ); 00100 } 00101 00107 public function setCaptionHtml( $caption ) { 00108 $this->mCaption = $caption; 00109 } 00110 00117 public function setPerRow( $num ) { 00118 if ( $num >= 0 ) { 00119 $this->mPerRow = (int)$num; 00120 } 00121 } 00122 00128 public function setWidths( $num ) { 00129 if ( $num > 0 ) { 00130 $this->mWidths = (int)$num; 00131 } 00132 } 00133 00139 public function setHeights( $num ) { 00140 if ( $num > 0 ) { 00141 $this->mHeights = (int)$num; 00142 } 00143 } 00144 00151 function useSkin( $skin ) { 00152 wfDeprecated( __METHOD__, '1.18' ); 00153 /* no op */ 00154 } 00155 00164 function add( $title, $html = '', $alt = '', $link = '') { 00165 if ( $title instanceof File ) { 00166 // Old calling convention 00167 $title = $title->getTitle(); 00168 } 00169 $this->mImages[] = array( $title, $html, $alt, $link ); 00170 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" ); 00171 } 00172 00180 function insert( $title, $html = '', $alt = '' ) { 00181 if ( $title instanceof File ) { 00182 // Old calling convention 00183 $title = $title->getTitle(); 00184 } 00185 array_unshift( $this->mImages, array( &$title, $html, $alt ) ); 00186 } 00187 00192 function isEmpty() { 00193 return empty( $this->mImages ); 00194 } 00195 00202 function setShowBytes( $f ) { 00203 $this->mShowBytes = (bool)$f; 00204 } 00205 00212 function setShowFilename( $f ) { 00213 $this->mShowFilename = (bool)$f; 00214 } 00215 00225 function setAttributes( $attribs ) { 00226 $this->mAttribs = $attribs; 00227 } 00228 00240 function toHTML() { 00241 global $wgLang; 00242 00243 if ( $this->mPerRow > 0 ) { 00244 $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS ); 00245 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : ''; 00246 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead 00247 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle; 00248 } 00249 00250 $attribs = Sanitizer::mergeAttributes( 00251 array( 'class' => 'gallery' ), $this->mAttribs ); 00252 00253 $output = Xml::openElement( 'ul', $attribs ); 00254 if ( $this->mCaption ) { 00255 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>"; 00256 } 00257 00258 $params = array( 00259 'width' => $this->mWidths, 00260 'height' => $this->mHeights 00261 ); 00262 # Output each image... 00263 foreach ( $this->mImages as $pair ) { 00264 $nt = $pair[0]; 00265 $text = $pair[1]; # "text" means "caption" here 00266 $alt = $pair[2]; 00267 $link = $pair[3]; 00268 00269 $descQuery = false; 00270 if ( $nt->getNamespace() == NS_FILE ) { 00271 # Get the file... 00272 if ( $this->mParser instanceof Parser ) { 00273 # Give extensions a chance to select the file revision for us 00274 $options = array(); 00275 wfRunHooks( 'BeforeParserFetchFileAndTitle', 00276 array( $this->mParser, $nt, &$options, &$descQuery ) ); 00277 # Fetch and register the file (file title may be different via hooks) 00278 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options ); 00279 } else { 00280 $img = wfFindFile( $nt ); 00281 } 00282 } else { 00283 $img = false; 00284 } 00285 00286 if( !$img ) { 00287 # We're dealing with a non-image, spit out the name and be done with it. 00288 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' 00289 . htmlspecialchars( $nt->getText() ) . '</div>'; 00290 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) { 00291 # The image is blacklisted, just show it as a text link. 00292 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' . 00293 Linker::link( 00294 $nt, 00295 htmlspecialchars( $nt->getText() ), 00296 array(), 00297 array(), 00298 array( 'known', 'noclasses' ) 00299 ) . 00300 '</div>'; 00301 } elseif( !( $thumb = $img->transform( $params ) ) ) { 00302 # Error generating thumbnail. 00303 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' 00304 . htmlspecialchars( $img->getLastError() ) . '</div>'; 00305 } else { 00306 $vpad = ( self::THUMB_PADDING + $this->mHeights - $thumb->height ) /2; 00307 00308 $imageParameters = array( 00309 'desc-link' => true, 00310 'desc-query' => $descQuery, 00311 'alt' => $alt, 00312 'custom-url-link' => $link 00313 ); 00314 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text 00315 if ( $alt == '' && $text == '' ) { 00316 $imageParameters['alt'] = $nt->getText(); 00317 } 00318 00319 # Set both fixed width and min-height. 00320 $thumbhtml = "\n\t\t\t" . 00321 '<div class="thumb" style="width: ' . ( $this->mWidths + self::THUMB_PADDING ) . 'px;">' 00322 # Auto-margin centering for block-level elements. Needed now that we have video 00323 # handlers since they may emit block-level elements as opposed to simple <img> tags. 00324 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement 00325 . '<div style="margin:' . $vpad . 'px auto;">' 00326 . $thumb->toHtml( $imageParameters ) . '</div></div>'; 00327 00328 // Call parser transform hook 00329 if ( $this->mParser && $img->getHandler() ) { 00330 $img->getHandler()->parserTransformHook( $this->mParser, $img ); 00331 } 00332 } 00333 00334 //TODO 00335 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" ); 00336 // $ul = Linker::link( $linkTarget, $ut ); 00337 00338 if( $this->mShowBytes ) { 00339 if( $img ) { 00340 $fileSize = htmlspecialchars( $wgLang->formatSize( $img->getSize() ) ); 00341 } else { 00342 $fileSize = wfMessage( 'filemissing' )->escaped(); 00343 } 00344 $fileSize = "$fileSize<br />\n"; 00345 } else { 00346 $fileSize = ''; 00347 } 00348 00349 $textlink = $this->mShowFilename ? 00350 Linker::link( 00351 $nt, 00352 htmlspecialchars( $wgLang->truncate( $nt->getText(), $this->mCaptionLength ) ), 00353 array(), 00354 array(), 00355 array( 'known', 'noclasses' ) 00356 ) . "<br />\n" : 00357 '' ; 00358 00359 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which 00360 # in version 4.8.6 generated crackpot html in its absence, see: 00361 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar 00362 00363 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug 00364 # Can be safely removed if FF2 falls completely out of existance 00365 $output .= 00366 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">' 00367 . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">' 00368 . $thumbhtml 00369 . "\n\t\t\t" . '<div class="gallerytext">' . "\n" 00370 . $textlink . $text . $fileSize 00371 . "\n\t\t\t</div>" 00372 . "\n\t\t</div></li>"; 00373 } 00374 $output .= "\n</ul>"; 00375 00376 return $output; 00377 } 00378 00382 public function count() { 00383 return count( $this->mImages ); 00384 } 00385 00391 public function setContextTitle( $title ) { 00392 $this->contextTitle = $title; 00393 } 00394 00400 public function getContextTitle() { 00401 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title 00402 ? $this->contextTitle 00403 : false; 00404 } 00405 00406 } //class