MediaWiki master
ImageGalleryBase.php
Go to the documentation of this file.
1<?php
31
39abstract class ImageGalleryBase extends ContextSource {
40 public const LOADING_DEFAULT = 1;
41 public const LOADING_LAZY = 2;
42
47 protected $mImages;
48
52 protected $mShowBytes;
53
58
62 protected $mShowFilename;
63
67 protected $mMode;
68
72 protected $mCaption = false;
73
81 protected $mCaptionLength = true;
82
86 protected $mHideBadImages;
87
91 public $mParser;
92
97 protected $contextTitle = null;
98
100 protected $mAttribs = [];
101
103 protected $mPerRow;
104
106 protected $mWidths;
107
109 protected $mHeights;
110
112 private static $modeMapping;
113
123 public static function factory( $mode = false, IContextSource $context = null ) {
124 self::loadModes();
125 if ( !$context ) {
126 $context = RequestContext::getMainAndWarn( __METHOD__ );
127 }
128 if ( !$mode ) {
129 $galleryOptions = $context->getConfig()->get( MainConfigNames::GalleryOptions );
130 $mode = $galleryOptions['mode'];
131 }
132
133 $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
134
135 if ( isset( self::$modeMapping[$mode] ) ) {
136 $class = self::$modeMapping[$mode];
137 return new $class( $mode, $context );
138 } else {
139 throw new ImageGalleryClassNotFoundException( "No gallery class registered for mode $mode" );
140 }
141 }
142
143 private static function loadModes() {
144 if ( self::$modeMapping === null ) {
145 self::$modeMapping = [
146 'traditional' => TraditionalImageGallery::class,
147 'nolines' => NolinesImageGallery::class,
148 'packed' => PackedImageGallery::class,
149 'packed-hover' => PackedHoverImageGallery::class,
150 'packed-overlay' => PackedOverlayImageGallery::class,
151 'slideshow' => SlideshowImageGallery::class,
152 ];
153 // Allow extensions to make a new gallery format.
154 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
155 ->onGalleryGetModes( self::$modeMapping );
156 }
157 }
158
172 public function __construct( $mode = 'traditional', IContextSource $context = null ) {
173 if ( $context ) {
174 $this->setContext( $context );
175 }
176
177 $galleryOptions = $this->getConfig()->get( MainConfigNames::GalleryOptions );
178 $this->mImages = [];
179 $this->mShowBytes = $galleryOptions['showBytes'];
180 $this->mShowDimensions = $galleryOptions['showDimensions'];
181 $this->mShowFilename = true;
182 $this->mParser = false;
183 $this->mHideBadImages = false;
184 $this->mPerRow = $galleryOptions['imagesPerRow'];
185 $this->mWidths = $galleryOptions['imageWidth'];
186 $this->mHeights = $galleryOptions['imageHeight'];
187 $this->mCaptionLength = $galleryOptions['captionLength'];
188 $this->mMode = $mode;
189 }
190
201 public function setParser( $parser ) {
202 $this->mParser = $parser;
203 }
204
208 public function setHideBadImages( $flag = true ) {
209 $this->mHideBadImages = $flag;
210 }
211
217 public function setCaption( $caption ) {
218 $this->mCaption = htmlspecialchars( $caption );
219 }
220
226 public function setCaptionHtml( $caption ) {
227 $this->mCaption = $caption;
228 }
229
236 public function setPerRow( $num ) {
237 if ( $num >= 0 ) {
238 $this->mPerRow = (int)$num;
239 }
240 }
241
248 public function setWidths( $num ) {
249 $parsed = Parser::parseWidthParam( $num, false );
250 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
251 $this->mWidths = $parsed['width'];
252 }
253 }
254
261 public function setHeights( $num ) {
262 $parsed = Parser::parseWidthParam( $num, false );
263 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
264 $this->mHeights = $parsed['width'];
265 }
266 }
267
277 public function setAdditionalOptions( $options ) {
278 }
279
292 public function add(
293 $title,
294 $html = '',
295 $alt = '',
296 $link = '',
297 $handlerOpts = [],
298 $loading = self::LOADING_DEFAULT,
299 ?array $imageOptions = null
300 ) {
301 if ( $title instanceof File ) {
302 // Old calling convention
303 $title = $title->getTitle();
304 }
305 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ];
306 wfDebug( 'ImageGallery::add ' . $title->getText() );
307 }
308
321 public function insert(
322 $title,
323 $html = '',
324 $alt = '',
325 $link = '',
326 $handlerOpts = [],
327 $loading = self::LOADING_DEFAULT,
328 ?array $imageOptions = null
329 ) {
330 if ( $title instanceof File ) {
331 // Old calling convention
332 $title = $title->getTitle();
333 }
334 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ] );
335 }
336
342 public function getImages() {
343 return $this->mImages;
344 }
345
350 public function isEmpty() {
351 return $this->mImages === [];
352 }
353
360 public function setShowDimensions( $f ) {
361 $this->mShowDimensions = (bool)$f;
362 }
363
370 public function setShowBytes( $f ) {
371 $this->mShowBytes = (bool)$f;
372 }
373
380 public function setShowFilename( $f ) {
381 $this->mShowFilename = (bool)$f;
382 }
383
393 public function setAttributes( $attribs ) {
394 $this->mAttribs = $attribs;
395 }
396
402 abstract public function toHTML();
403
407 public function count() {
408 return count( $this->mImages );
409 }
410
416 public function setContextTitle( $title ) {
417 $this->contextTitle = $title;
418 }
419
425 public function getContextTitle() {
426 return $this->contextTitle;
427 }
428
433 protected function getRenderLang() {
434 return $this->mParser
435 ? $this->mParser->getTargetLanguage()
436 : $this->getLanguage();
437 }
438}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:73
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
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 bad images?
setCaption( $caption)
Set the caption (as plain text)
setHideBadImages( $flag=true)
string false $mCaption
Gallery caption.
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.
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.
insert( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT, ?array $imageOptions=null)
Add an image at the beginning of the 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.
add( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT, ?array $imageOptions=null)
Add an image to the gallery.
array[] $mImages
Gallery images.
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
Class for exceptions thrown by ImageGalleryBase::factory().
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setContext(IContextSource $context)
Group all the pieces relevant to the context of a request into one instance.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:155
Represents a title within MediaWiki.
Definition Title.php:78
Interface for objects which can provide a MediaWiki context on request.