MediaWiki REL1_32
ImageGalleryBase.php
Go to the documentation of this file.
1<?php
24
32abstract class ImageGalleryBase extends ContextSource {
36 protected $mImages;
37
41 protected $mShowBytes;
42
47
51 protected $mShowFilename;
52
56 protected $mMode;
57
61 protected $mCaption = false;
62
70 protected $mCaptionLength = true;
71
75 protected $mHideBadImages;
76
80 public $mParser;
81
86 protected $contextTitle = false;
87
89 protected $mAttribs = [];
90
92 static private $modeMapping = false;
93
103 static function factory( $mode = false, IContextSource $context = null ) {
105 if ( !$context ) {
106 $context = RequestContext::getMainAndWarn( __METHOD__ );
107 }
108 if ( !$mode ) {
109 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
110 $mode = $galleryOptions['mode'];
111 }
112
113 $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
114
115 if ( isset( self::$modeMapping[$mode] ) ) {
116 $class = self::$modeMapping[$mode];
117 return new $class( $mode, $context );
118 } else {
119 throw new MWException( "No gallery class registered for mode $mode" );
120 }
121 }
122
123 private static function loadModes() {
124 if ( self::$modeMapping === false ) {
125 self::$modeMapping = [
126 'traditional' => TraditionalImageGallery::class,
127 'nolines' => NolinesImageGallery::class,
128 'packed' => PackedImageGallery::class,
129 'packed-hover' => PackedHoverImageGallery::class,
130 'packed-overlay' => PackedOverlayImageGallery::class,
131 'slideshow' => SlideshowImageGallery::class,
132 ];
133 // Allow extensions to make a new gallery format.
134 Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
135 }
136 }
137
146 function __construct( $mode = 'traditional', IContextSource $context = null ) {
147 if ( $context ) {
148 $this->setContext( $context );
149 }
150
151 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
152 $this->mImages = [];
153 $this->mShowBytes = $galleryOptions['showBytes'];
154 $this->mShowDimensions = $galleryOptions['showDimensions'];
155 $this->mShowFilename = true;
156 $this->mParser = false;
157 $this->mHideBadImages = false;
158 $this->mPerRow = $galleryOptions['imagesPerRow'];
159 $this->mWidths = $galleryOptions['imageWidth'];
160 $this->mHeights = $galleryOptions['imageHeight'];
161 $this->mCaptionLength = $galleryOptions['captionLength'];
162 $this->mMode = $mode;
163 }
164
175 function setParser( $parser ) {
176 $this->mParser = $parser;
177 }
178
183 function setHideBadImages( $flag = true ) {
184 $this->mHideBadImages = $flag;
185 }
186
192 function setCaption( $caption ) {
193 $this->mCaption = htmlspecialchars( $caption );
194 }
195
201 public function setCaptionHtml( $caption ) {
202 $this->mCaption = $caption;
203 }
204
211 public function setPerRow( $num ) {
212 if ( $num >= 0 ) {
213 $this->mPerRow = (int)$num;
214 }
215 }
216
223 public function setWidths( $num ) {
224 $parsed = Parser::parseWidthParam( $num, false );
225 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
226 $this->mWidths = $parsed['width'];
227 }
228 }
229
236 public function setHeights( $num ) {
237 $parsed = Parser::parseWidthParam( $num, false );
238 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
239 $this->mHeights = $parsed['width'];
240 }
241 }
242
250 public function setAdditionalOptions( $options ) {
251 }
252
263 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
264 if ( $title instanceof File ) {
265 // Old calling convention
266 $title = $title->getTitle();
267 }
268 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
269 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
270 }
271
282 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
283 if ( $title instanceof File ) {
284 // Old calling convention
285 $title = $title->getTitle();
286 }
287 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
288 }
289
294 public function getImages() {
295 return $this->mImages;
296 }
297
302 function isEmpty() {
303 return empty( $this->mImages );
304 }
305
312 function setShowDimensions( $f ) {
313 $this->mShowDimensions = (bool)$f;
314 }
315
322 function setShowBytes( $f ) {
323 $this->mShowBytes = (bool)$f;
324 }
325
332 function setShowFilename( $f ) {
333 $this->mShowFilename = (bool)$f;
334 }
335
346 $this->mAttribs = $attribs;
347 }
348
354 abstract public function toHTML();
355
359 public function count() {
360 return count( $this->mImages );
361 }
362
368 public function setContextTitle( $title ) {
369 $this->contextTitle = $title;
370 }
371
377 public function getContextTitle() {
378 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
379 ? $this->contextTitle
380 : false;
381 }
382
387 protected function getRenderLang() {
388 return $this->mParser
389 ? $this->mParser->getTargetLanguage()
390 : $this->getLanguage();
391 }
392}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
IContextSource $context
setContext(IContextSource $context)
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:51
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
bool string $mCaption
Gallery caption.
setHeights( $num)
Set how high each image will be, in pixels.
bool $mShowDimensions
Whether to show the dimensions in categories.
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
isEmpty()
isEmpty() returns true if the gallery contains no images
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
bool $mHideBadImages
Hide blacklisted images?
setCaption( $caption)
Set the caption (as plain text)
setHideBadImages( $flag=true)
Set bad image flag.
Parser $mParser
Registered parser object for output callbacks.
setCaptionHtml( $caption)
Set the caption (as HTML)
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
getContextTitle()
Get the contextual title, if applicable.
string $mMode
Gallery mode.
setAdditionalOptions( $options)
Allow setting additional options.
setPerRow( $num)
Set how many images will be displayed per row.
insert( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image at the beginning of the gallery.
setContextTitle( $title)
Set the contextual title.
Title $contextTitle
Contextual title, used when images are being screened against the bad image list.
bool $mShowFilename
Whether to show the filename.
setParser( $parser)
Register a parser object.
static bool $modeMapping
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
bool $mShowBytes
Whether to show the filesize in bytes in categories.
toHTML()
Display an html representation of the gallery.
setShowBytes( $f)
Enable/Disable showing of the file size of an image in the gallery.
getRenderLang()
Determines the correct language to be used for this image gallery.
setWidths( $num)
Set how wide each image will be, in pixels.
getImages()
Returns the list of images this gallery contains.
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
array $mImages
Gallery images.
add( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image to the gallery.
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:68
Represents a title within MediaWiki.
Definition Title.php:39
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition hooks.txt:1873
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2050
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:994
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2062
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:3106
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2063
Interface for objects which can provide a MediaWiki context on request.
getConfig()
Get the site configuration.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))