MediaWiki master
ImageGalleryBase.php
Go to the documentation of this file.
1<?php
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
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 public static function factory( $mode = false, ?IContextSource $context = null ) {
113 self::loadModes();
114 if ( !$context ) {
115 $context = RequestContext::getMainAndWarn( __METHOD__ );
116 }
117 if ( !$mode ) {
118 $galleryOptions = $context->getConfig()->get( MainConfigNames::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 ImageGalleryClassNotFoundException( "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 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
144 ->onGalleryGetModes( self::$modeMapping );
145 }
146 }
147
161 public function __construct( $mode = 'traditional', ?IContextSource $context = null ) {
162 if ( $context ) {
163 $this->setContext( $context );
164 }
165
166 $galleryOptions = $this->getConfig()->get( MainConfigNames::GalleryOptions );
167 $this->mImages = [];
168 $this->mShowBytes = $galleryOptions['showBytes'];
169 $this->mShowDimensions = $galleryOptions['showDimensions'];
170 $this->mShowFilename = true;
171 $this->mParser = false;
172 $this->mHideBadImages = false;
173 $this->mPerRow = $galleryOptions['imagesPerRow'];
174 $this->mWidths = $galleryOptions['imageWidth'];
175 $this->mHeights = $galleryOptions['imageHeight'];
176 $this->mCaptionLength = $galleryOptions['captionLength'];
177 $this->mMode = $mode;
178 }
179
190 public function setParser( $parser ) {
191 $this->mParser = $parser;
192 }
193
197 public function setHideBadImages( $flag = true ) {
198 $this->mHideBadImages = $flag;
199 }
200
206 public function setCaption( $caption ) {
207 $this->mCaption = htmlspecialchars( $caption );
208 }
209
215 public function setCaptionHtml( $caption ) {
216 $this->mCaption = $caption;
217 }
218
225 public function setPerRow( $num ) {
226 if ( $num >= 0 ) {
227 $this->mPerRow = (int)$num;
228 }
229 }
230
237 public function setWidths( $num ) {
238 $parser = $this->mParser;
239 if ( !$parser ) {
240 wfDeprecated( __METHOD__ . ' without parser', '1.43' );
241 $parser = MediaWikiServices::getInstance()->getParser();
242 }
243 $parsed = $parser->parseWidthParam( $num, false );
244 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
245 $this->mWidths = $parsed['width'];
246 }
247 }
248
255 public function setHeights( $num ) {
256 $parser = $this->mParser;
257 if ( !$parser ) {
258 wfDeprecated( __METHOD__ . ' without parser', '1.43' );
259 $parser = MediaWikiServices::getInstance()->getParser();
260 }
261 $parsed = $parser->parseWidthParam( $num, false );
262 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
263 $this->mHeights = $parsed['width'];
264 }
265 }
266
276 public function setAdditionalOptions( $options ) {
277 }
278
291 public function add(
292 $title,
293 $html = '',
294 $alt = '',
295 $link = '',
296 $handlerOpts = [],
297 $loading = self::LOADING_DEFAULT,
298 ?array $imageOptions = null
299 ) {
300 if ( $title instanceof File ) {
301 // Old calling convention
302 $title = $title->getTitle();
303 }
304 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ];
305 wfDebug( 'ImageGallery::add ' . $title->getText() );
306 }
307
320 public function insert(
321 $title,
322 $html = '',
323 $alt = '',
324 $link = '',
325 $handlerOpts = [],
326 $loading = self::LOADING_DEFAULT,
327 ?array $imageOptions = null
328 ) {
329 if ( $title instanceof File ) {
330 // Old calling convention
331 $title = $title->getTitle();
332 }
333 array_unshift( $this->mImages, [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ] );
334 }
335
341 public function getImages() {
342 return $this->mImages;
343 }
344
349 public function isEmpty() {
350 return $this->mImages === [];
351 }
352
359 public function setShowDimensions( $f ) {
360 $this->mShowDimensions = (bool)$f;
361 }
362
369 public function setShowBytes( $f ) {
370 $this->mShowBytes = (bool)$f;
371 }
372
379 public function setShowFilename( $f ) {
380 $this->mShowFilename = (bool)$f;
381 }
382
392 public function setAttributes( $attribs ) {
393 $this->mAttribs = $attribs;
394 }
395
401 abstract public function toHTML();
402
406 public function count() {
407 return count( $this->mImages );
408 }
409
415 public function setContextTitle( $title ) {
416 $this->contextTitle = $title;
417 }
418
424 public function getContextTitle() {
425 return $this->contextTitle;
426 }
427
432 protected function getRenderLang() {
433 return $this->mParser
434 ? $this->mParser->getTargetLanguage()
435 : $this->getLanguage();
436 }
437}
438
440class_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.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
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:69
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:135
Represents a title within MediaWiki.
Definition Title.php:69
Interface for objects which can provide a MediaWiki context on request.