MediaWiki REL1_34
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 = null;
87
89 protected $mAttribs = [];
90
92 protected $mPerRow;
93
95 protected $mWidths;
96
98 protected $mHeights;
99
101 private static $modeMapping;
102
112 static function factory( $mode = false, IContextSource $context = null ) {
114 if ( !$context ) {
115 $context = RequestContext::getMainAndWarn( __METHOD__ );
116 }
117 if ( !$mode ) {
118 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
119 $mode = $galleryOptions['mode'];
120 }
121
122 $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
123
124 if ( isset( self::$modeMapping[$mode] ) ) {
125 $class = self::$modeMapping[$mode];
126 return new $class( $mode, $context );
127 } else {
128 throw new MWException( "No gallery class registered for mode $mode" );
129 }
130 }
131
132 private static function loadModes() {
133 if ( self::$modeMapping === null ) {
134 self::$modeMapping = [
135 'traditional' => TraditionalImageGallery::class,
136 'nolines' => NolinesImageGallery::class,
137 'packed' => PackedImageGallery::class,
138 'packed-hover' => PackedHoverImageGallery::class,
139 'packed-overlay' => PackedOverlayImageGallery::class,
140 'slideshow' => SlideshowImageGallery::class,
141 ];
142 // Allow extensions to make a new gallery format.
143 Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
144 }
145 }
146
155 function __construct( $mode = 'traditional', IContextSource $context = null ) {
156 if ( $context ) {
157 $this->setContext( $context );
158 }
159
160 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
161 $this->mImages = [];
162 $this->mShowBytes = $galleryOptions['showBytes'];
163 $this->mShowDimensions = $galleryOptions['showDimensions'];
164 $this->mShowFilename = true;
165 $this->mParser = false;
166 $this->mHideBadImages = false;
167 $this->mPerRow = $galleryOptions['imagesPerRow'];
168 $this->mWidths = $galleryOptions['imageWidth'];
169 $this->mHeights = $galleryOptions['imageHeight'];
170 $this->mCaptionLength = $galleryOptions['captionLength'];
171 $this->mMode = $mode;
172 }
173
184 function setParser( $parser ) {
185 $this->mParser = $parser;
186 }
187
192 function setHideBadImages( $flag = true ) {
193 $this->mHideBadImages = $flag;
194 }
195
201 function setCaption( $caption ) {
202 $this->mCaption = htmlspecialchars( $caption );
203 }
204
210 public function setCaptionHtml( $caption ) {
211 $this->mCaption = $caption;
212 }
213
220 public function setPerRow( $num ) {
221 if ( $num >= 0 ) {
222 $this->mPerRow = (int)$num;
223 }
224 }
225
232 public function setWidths( $num ) {
233 $parsed = Parser::parseWidthParam( $num, false );
234 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
235 $this->mWidths = $parsed['width'];
236 }
237 }
238
245 public function setHeights( $num ) {
246 $parsed = Parser::parseWidthParam( $num, false );
247 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
248 $this->mHeights = $parsed['width'];
249 }
250 }
251
259 public function setAdditionalOptions( $options ) {
260 }
261
272 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
273 if ( $title instanceof File ) {
274 // Old calling convention
275 $title = $title->getTitle();
276 }
277 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
278 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
279 }
280
291 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
292 if ( $title instanceof File ) {
293 // Old calling convention
294 $title = $title->getTitle();
295 }
296 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
297 }
298
303 public function getImages() {
304 return $this->mImages;
305 }
306
311 function isEmpty() {
312 return empty( $this->mImages );
313 }
314
321 function setShowDimensions( $f ) {
322 $this->mShowDimensions = (bool)$f;
323 }
324
331 function setShowBytes( $f ) {
332 $this->mShowBytes = (bool)$f;
333 }
334
341 function setShowFilename( $f ) {
342 $this->mShowFilename = (bool)$f;
343 }
344
354 function setAttributes( $attribs ) {
355 $this->mAttribs = $attribs;
356 }
357
363 abstract public function toHTML();
364
368 public function count() {
369 return count( $this->mImages );
370 }
371
377 public function setContextTitle( $title ) {
378 $this->contextTitle = $title;
379 }
380
386 public function getContextTitle() {
387 return $this->contextTitle;
388 }
389
394 protected function getRenderLang() {
395 return $this->mParser
396 ? $this->mParser->getTargetLanguage()
397 : $this->getLanguage();
398 }
399}
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:61
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
bool string $mCaption
Gallery caption.
Parser false $mParser
Registered parser object for output callbacks.
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.
static array $modeMapping
Title null $contextTitle
Contextual title, used when images are being screened against the bad image list.
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.
bool $mShowFilename
Whether to show the filename.
setParser( $parser)
Register a parser object.
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:74
Represents a title within MediaWiki.
Definition Title.php:42
Interface for objects which can provide a MediaWiki context on request.
getConfig()
Get the site configuration.