MediaWiki master
ImageGalleryBase.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Gallery;
8
20
28abstract class ImageGalleryBase extends ContextSource {
29 public const LOADING_DEFAULT = 1;
30 public const LOADING_LAZY = 2;
31
36 protected $mImages;
37
41 protected $mShowBytes;
42
47
51 protected $mShowFilename;
52
56 protected $mMode;
57
61 protected $mCaption = false;
62
71 protected $mCaptionLength = true;
72
76 protected $mHideBadImages;
77
81 public $mParser;
82
87 protected $contextTitle = null;
88
90 protected $mAttribs = [];
91
93 protected $mPerRow;
94
96 protected $mWidths;
97
99 protected $mHeights;
100
102 private static $modeMapping;
103
113 public static function factory( $mode = false, ?IContextSource $context = null ) {
114 self::loadModes();
115 if ( !$context ) {
116 $context = RequestContext::getMainAndWarn( __METHOD__ );
117 }
118 if ( !$mode ) {
119 $galleryOptions = $context->getConfig()->get( MainConfigNames::GalleryOptions );
120 $mode = $galleryOptions['mode'];
121 }
122
123 $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
124
125 if ( isset( self::$modeMapping[$mode] ) ) {
126 $class = self::$modeMapping[$mode];
127 return new $class( $mode, $context );
128 } else {
129 throw new ImageGalleryClassNotFoundException( "No gallery class registered for mode $mode" );
130 }
131 }
132
133 private static function loadModes() {
134 if ( self::$modeMapping === null ) {
135 self::$modeMapping = [
136 'traditional' => TraditionalImageGallery::class,
137 'nolines' => NolinesImageGallery::class,
138 'packed' => PackedImageGallery::class,
139 'packed-hover' => PackedHoverImageGallery::class,
140 'packed-overlay' => PackedOverlayImageGallery::class,
141 'slideshow' => SlideshowImageGallery::class,
142 ];
143 // Allow extensions to make a new gallery format.
144 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
145 ->onGalleryGetModes( self::$modeMapping );
146 }
147 }
148
162 public function __construct( $mode = 'traditional', ?IContextSource $context = null ) {
163 if ( $context ) {
164 $this->setContext( $context );
165 }
166
167 $galleryOptions = $this->getConfig()->get( MainConfigNames::GalleryOptions );
168 $this->mImages = [];
169 $this->mShowBytes = $galleryOptions['showBytes'];
170 $this->mShowDimensions = $galleryOptions['showDimensions'];
171 $this->mShowFilename = true;
172 $this->mParser = false;
173 $this->mHideBadImages = false;
174 $this->mPerRow = $galleryOptions['imagesPerRow'];
175 $this->mWidths = $galleryOptions['imageWidth'];
176 $this->mHeights = $galleryOptions['imageHeight'];
177 $this->mCaptionLength = $galleryOptions['captionLength'];
178 $this->mMode = $mode;
179
180 if ( $this->mCaptionLength !== true ) {
182 "\$wgGalleryOptions['captionLength'] is deprecated, use CSS to truncate captions instead",
183 '1.47'
184 );
185 }
186 }
187
198 public function setParser( $parser ) {
199 $this->mParser = $parser;
200 }
201
205 public function setHideBadImages( $flag = true ) {
206 $this->mHideBadImages = $flag;
207 }
208
214 public function setCaption( $caption ) {
215 $this->mCaption = htmlspecialchars( $caption );
216 }
217
223 public function setCaptionHtml( $caption ) {
224 $this->mCaption = $caption;
225 }
226
233 public function setPerRow( $num ) {
234 if ( $num >= 0 ) {
235 $this->mPerRow = (int)$num;
236 }
237 }
238
245 public function setWidths( $num ) {
246 $parser = $this->mParser;
247 if ( !$parser ) {
248 wfDeprecated( __METHOD__ . ' without parser', '1.43' );
249 $parser = MediaWikiServices::getInstance()->getParser();
250 }
251 $parsed = $parser->parseWidthParam( $num, false );
252 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
253 $this->mWidths = $parsed['width'];
254 }
255 }
256
263 public function setHeights( $num ) {
264 $parser = $this->mParser;
265 if ( !$parser ) {
266 wfDeprecated( __METHOD__ . ' without parser', '1.43' );
267 $parser = MediaWikiServices::getInstance()->getParser();
268 }
269 $parsed = $parser->parseWidthParam( $num, false );
270 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
271 $this->mHeights = $parsed['width'];
272 }
273 }
274
284 public function setAdditionalOptions( $options ) {
285 }
286
299 public function add(
300 $title,
301 $html = '',
302 $alt = '',
303 $link = '',
304 $handlerOpts = [],
305 $loading = self::LOADING_DEFAULT,
306 ?array $imageOptions = null
307 ) {
308 if ( $title instanceof File ) {
309 // Old calling convention
310 $title = $title->getTitle();
311 }
312 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ];
313 wfDebug( 'ImageGallery::add ' . $title->getText() );
314 }
315
328 public function insert(
329 $title,
330 $html = '',
331 $alt = '',
332 $link = '',
333 $handlerOpts = [],
334 $loading = self::LOADING_DEFAULT,
335 ?array $imageOptions = null
336 ) {
337 if ( $title instanceof File ) {
338 // Old calling convention
339 $title = $title->getTitle();
340 }
341 array_unshift( $this->mImages, [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ] );
342 }
343
349 public function getImages() {
350 return $this->mImages;
351 }
352
357 public function isEmpty() {
358 return $this->mImages === [];
359 }
360
367 public function setShowDimensions( $f ) {
368 $this->mShowDimensions = (bool)$f;
369 }
370
377 public function setShowBytes( $f ) {
378 $this->mShowBytes = (bool)$f;
379 }
380
387 public function setShowFilename( $f ) {
388 $this->mShowFilename = (bool)$f;
389 }
390
400 public function setAttributes( $attribs ) {
401 $this->mAttribs = $attribs;
402 }
403
409 abstract public function toHTML();
410
414 public function count() {
415 return count( $this->mImages );
416 }
417
423 public function setContextTitle( $title ) {
424 $this->contextTitle = $title;
425 }
426
432 public function getContextTitle() {
433 return $this->contextTitle;
434 }
435
440 protected function getRenderLang() {
441 return $this->mParser
442 ? $this->mParser->getTargetLanguage()
443 : $this->getLanguage();
444 }
445}
446
448class_alias( ImageGalleryBase::class, 'ImageGalleryBase' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:80
Class for exceptions thrown by ImageGalleryBase::factory().
toHTML()
Display an html representation of the gallery.
isEmpty()
isEmpty() returns true if the gallery contains no images
bool $mShowBytes
Whether to show the filesize in bytes in categories.
setPerRow( $num)
Set how many images will be displayed per row.
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
Title null $contextTitle
Contextual title, used when images are being screened against the bad image list.
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
add( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT, ?array $imageOptions=null)
Add an image to the gallery.
setCaptionHtml( $caption)
Set the caption (as HTML)
string false $mCaption
Gallery caption.
setParser( $parser)
Register a parser object.
insert( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT, ?array $imageOptions=null)
Add an image at the beginning of the gallery.
setShowBytes( $f)
Enable/Disable showing of the file size of an image in the gallery.
static factory( $mode=false, ?IContextSource $context=null)
Get a new image gallery.
Parser false $mParser
Registered parser object for output callbacks.
bool $mShowDimensions
Whether to show the dimensions in categories.
setContextTitle( $title)
Set the contextual title.
__construct( $mode='traditional', ?IContextSource $context=null)
Create a new image gallery object.
setWidths( $num)
Set how wide each image will be, in pixels.
setAdditionalOptions( $options)
Allow setting additional options.
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
setCaption( $caption)
Set the caption (as plain text)
getContextTitle()
Get the contextual title, if applicable.
getRenderLang()
Determines the correct language to be used for this image gallery.
bool $mShowFilename
Whether to show the filename.
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
getImages()
Returns the list of images this gallery contains.
setHeights( $num)
Set how high each image will be, in pixels.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Base class for language-specific code.
Definition Language.php:65
A class containing constants representing the names of configuration variables.
const GalleryOptions
Name constant for the GalleryOptions setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:138
Represents a title within MediaWiki.
Definition Title.php:69
Interface for objects which can provide a MediaWiki context on request.