MediaWiki  REL1_24
ImageGalleryBase.php
Go to the documentation of this file.
00001 <?php
00030 abstract class ImageGalleryBase extends ContextSource {
00035     public $mImages;
00036 
00041     public $mShowBytes;
00042 
00047     public $mShowFilename;
00048 
00053     public $mMode;
00054 
00059     public $mCaption = false;
00060 
00065     public $mHideBadImages;
00066 
00070     public $mParser;
00071 
00076     protected $contextTitle = false;
00077 
00079     protected $mAttribs = array();
00080 
00082     static private $modeMapping = false;
00083 
00093     static function factory( $mode = false, IContextSource $context = null ) {
00094         global $wgContLang;
00095         self::loadModes();
00096         if ( !$context ) {
00097             $context = RequestContext::getMainAndWarn( __METHOD__ );
00098         }
00099         if ( !$mode ) {
00100             $galleryOpions = $context->getConfig()->get( 'GalleryOptions' );
00101             $mode = $galleryOpions['mode'];
00102         }
00103 
00104         $mode = $wgContLang->lc( $mode );
00105 
00106         if ( isset( self::$modeMapping[$mode] ) ) {
00107             return new self::$modeMapping[$mode]( $mode, $context );
00108         } else {
00109             throw new MWException( "No gallery class registered for mode $mode" );
00110         }
00111     }
00112 
00113     private static function loadModes() {
00114         if ( self::$modeMapping === false ) {
00115             self::$modeMapping = array(
00116                 'traditional' => 'TraditionalImageGallery',
00117                 'nolines' => 'NolinesImageGallery',
00118                 'packed' => 'PackedImageGallery',
00119                 'packed-hover' => 'PackedHoverImageGallery',
00120                 'packed-overlay' => 'PackedOverlayImageGallery',
00121             );
00122             // Allow extensions to make a new gallery format.
00123             wfRunHooks( 'GalleryGetModes', self::$modeMapping );
00124         }
00125     }
00126 
00135     function __construct( $mode = 'traditional', IContextSource $context = null ) {
00136         if ( $context ) {
00137             $this->setContext( $context );
00138         }
00139 
00140         $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
00141         $this->mImages = array();
00142         $this->mShowBytes = $galleryOptions['showBytes'];
00143         $this->mShowFilename = true;
00144         $this->mParser = false;
00145         $this->mHideBadImages = false;
00146         $this->mPerRow = $galleryOptions['imagesPerRow'];
00147         $this->mWidths = $galleryOptions['imageWidth'];
00148         $this->mHeights = $galleryOptions['imageHeight'];
00149         $this->mCaptionLength = $galleryOptions['captionLength'];
00150         $this->mMode = $mode;
00151     }
00152 
00163     function setParser( $parser ) {
00164         $this->mParser = $parser;
00165     }
00166 
00171     function setHideBadImages( $flag = true ) {
00172         $this->mHideBadImages = $flag;
00173     }
00174 
00180     function setCaption( $caption ) {
00181         $this->mCaption = htmlspecialchars( $caption );
00182     }
00183 
00189     public function setCaptionHtml( $caption ) {
00190         $this->mCaption = $caption;
00191     }
00192 
00199     public function setPerRow( $num ) {
00200         if ( $num >= 0 ) {
00201             $this->mPerRow = (int)$num;
00202         }
00203     }
00204 
00210     public function setWidths( $num ) {
00211         if ( $num > 0 ) {
00212             $this->mWidths = (int)$num;
00213         }
00214     }
00215 
00221     public function setHeights( $num ) {
00222         if ( $num > 0 ) {
00223             $this->mHeights = (int)$num;
00224         }
00225     }
00226 
00234     public function setAdditionalOptions( $options ) {
00235     }
00236 
00247     function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
00248         if ( $title instanceof File ) {
00249             // Old calling convention
00250             $title = $title->getTitle();
00251         }
00252         $this->mImages[] = array( $title, $html, $alt, $link, $handlerOpts );
00253         wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
00254     }
00255 
00266     function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
00267         if ( $title instanceof File ) {
00268             // Old calling convention
00269             $title = $title->getTitle();
00270         }
00271         array_unshift( $this->mImages, array( &$title, $html, $alt, $link, $handlerOpts ) );
00272     }
00273 
00278     public function getImages() {
00279         return $this->mImages;
00280     }
00281 
00286     function isEmpty() {
00287         return empty( $this->mImages );
00288     }
00289 
00296     function setShowBytes( $f ) {
00297         $this->mShowBytes = (bool)$f;
00298     }
00299 
00306     function setShowFilename( $f ) {
00307         $this->mShowFilename = (bool)$f;
00308     }
00309 
00319     function setAttributes( $attribs ) {
00320         $this->mAttribs = $attribs;
00321     }
00322 
00328     abstract public function toHTML();
00329 
00333     public function count() {
00334         return count( $this->mImages );
00335     }
00336 
00342     public function setContextTitle( $title ) {
00343         $this->contextTitle = $title;
00344     }
00345 
00351     public function getContextTitle() {
00352         return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
00353             ? $this->contextTitle
00354             : false;
00355     }
00356 
00361     protected function getRenderLang() {
00362         return $this->mParser
00363             ? $this->mParser->getTargetLanguage()
00364             : $this->getLanguage();
00365     }
00366 }