MediaWiki
REL1_21
|
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 if ( $this->mPerRow > 0 ) { 00242 $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS ); 00243 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : ''; 00244 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead 00245 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle; 00246 } 00247 00248 $attribs = Sanitizer::mergeAttributes( 00249 array( 'class' => 'gallery' ), $this->mAttribs ); 00250 00251 $output = Xml::openElement( 'ul', $attribs ); 00252 if ( $this->mCaption ) { 00253 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>"; 00254 } 00255 00256 $lang = $this->getLang(); 00257 $params = array( 00258 'width' => $this->mWidths, 00259 'height' => $this->mHeights 00260 ); 00261 # Output each image... 00262 foreach ( $this->mImages as $pair ) { 00263 $nt = $pair[0]; 00264 $text = $pair[1]; # "text" means "caption" here 00265 $alt = $pair[2]; 00266 $link = $pair[3]; 00267 00268 $descQuery = false; 00269 if ( $nt->getNamespace() == NS_FILE ) { 00270 # Get the file... 00271 if ( $this->mParser instanceof Parser ) { 00272 # Give extensions a chance to select the file revision for us 00273 $options = array(); 00274 wfRunHooks( 'BeforeParserFetchFileAndTitle', 00275 array( $this->mParser, $nt, &$options, &$descQuery ) ); 00276 # Fetch and register the file (file title may be different via hooks) 00277 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options ); 00278 } else { 00279 $img = wfFindFile( $nt ); 00280 } 00281 } else { 00282 $img = false; 00283 } 00284 00285 if( !$img ) { 00286 # We're dealing with a non-image, spit out the name and be done with it. 00287 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' 00288 . htmlspecialchars( $nt->getText() ) . '</div>'; 00289 } elseif( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) { 00290 # The image is blacklisted, just show it as a text link. 00291 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' . 00292 Linker::link( 00293 $nt, 00294 htmlspecialchars( $nt->getText() ), 00295 array(), 00296 array(), 00297 array( 'known', 'noclasses' ) 00298 ) . 00299 '</div>'; 00300 } elseif( !( $thumb = $img->transform( $params ) ) ) { 00301 # Error generating thumbnail. 00302 $thumbhtml = "\n\t\t\t" . '<div style="height: ' . ( self::THUMB_PADDING + $this->mHeights ) . 'px;">' 00303 . htmlspecialchars( $img->getLastError() ) . '</div>'; 00304 } else { 00305 $vpad = ( self::THUMB_PADDING + $this->mHeights - $thumb->height ) /2; 00306 00307 $imageParameters = array( 00308 'desc-link' => true, 00309 'desc-query' => $descQuery, 00310 'alt' => $alt, 00311 'custom-url-link' => $link 00312 ); 00313 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text 00314 if ( $alt == '' && $text == '' ) { 00315 $imageParameters['alt'] = $nt->getText(); 00316 } 00317 00318 # Set both fixed width and min-height. 00319 $thumbhtml = "\n\t\t\t" . 00320 '<div class="thumb" style="width: ' . ( $this->mWidths + self::THUMB_PADDING ) . 'px;">' 00321 # Auto-margin centering for block-level elements. Needed now that we have video 00322 # handlers since they may emit block-level elements as opposed to simple <img> tags. 00323 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement 00324 . '<div style="margin:' . $vpad . 'px auto;">' 00325 . $thumb->toHtml( $imageParameters ) . '</div></div>'; 00326 00327 // Call parser transform hook 00328 if ( $this->mParser && $img->getHandler() ) { 00329 $img->getHandler()->parserTransformHook( $this->mParser, $img ); 00330 } 00331 } 00332 00333 //TODO 00334 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" ); 00335 // $ul = Linker::link( $linkTarget, $ut ); 00336 00337 if( $this->mShowBytes ) { 00338 if( $img ) { 00339 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) ); 00340 } else { 00341 $fileSize = wfMessage( 'filemissing' )->escaped(); 00342 } 00343 $fileSize = "$fileSize<br />\n"; 00344 } else { 00345 $fileSize = ''; 00346 } 00347 00348 $textlink = $this->mShowFilename ? 00349 Linker::link( 00350 $nt, 00351 htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) ), 00352 array(), 00353 array(), 00354 array( 'known', 'noclasses' ) 00355 ) . "<br />\n" : 00356 ''; 00357 00358 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which 00359 # in version 4.8.6 generated crackpot html in its absence, see: 00360 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar 00361 00362 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug 00363 # Can be safely removed if FF2 falls completely out of existence 00364 $output .= 00365 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">' 00366 . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">' 00367 . $thumbhtml 00368 . "\n\t\t\t" . '<div class="gallerytext">' . "\n" 00369 . $textlink . $text . $fileSize 00370 . "\n\t\t\t</div>" 00371 . "\n\t\t</div></li>"; 00372 } 00373 $output .= "\n</ul>"; 00374 00375 return $output; 00376 } 00377 00381 public function count() { 00382 return count( $this->mImages ); 00383 } 00384 00390 public function setContextTitle( $title ) { 00391 $this->contextTitle = $title; 00392 } 00393 00399 public function getContextTitle() { 00400 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title 00401 ? $this->contextTitle 00402 : false; 00403 } 00404 00409 private function getLang() { 00410 global $wgLang; 00411 return $this->mParser 00412 ? $this->mParser->getTargetLanguage() 00413 : $wgLang; 00414 } 00415 00416 } //class