MediaWiki master
ImageGalleryBase.php
Go to the documentation of this file.
1<?php
33
41abstract class ImageGalleryBase extends ContextSource {
42 public const LOADING_DEFAULT = 1;
43 public const LOADING_LAZY = 2;
44
49 protected $mImages;
50
54 protected $mShowBytes;
55
60
64 protected $mShowFilename;
65
69 protected $mMode;
70
74 protected $mCaption = false;
75
83 protected $mCaptionLength = true;
84
88 protected $mHideBadImages;
89
93 public $mParser;
94
99 protected $contextTitle = null;
100
102 protected $mAttribs = [];
103
105 protected $mPerRow;
106
108 protected $mWidths;
109
111 protected $mHeights;
112
114 private static $modeMapping;
115
125 public static function factory( $mode = false, ?IContextSource $context = null ) {
126 self::loadModes();
127 if ( !$context ) {
128 $context = RequestContext::getMainAndWarn( __METHOD__ );
129 }
130 if ( !$mode ) {
131 $galleryOptions = $context->getConfig()->get( MainConfigNames::GalleryOptions );
132 $mode = $galleryOptions['mode'];
133 }
134
135 $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
136
137 if ( isset( self::$modeMapping[$mode] ) ) {
138 $class = self::$modeMapping[$mode];
139 return new $class( $mode, $context );
140 } else {
141 throw new ImageGalleryClassNotFoundException( "No gallery class registered for mode $mode" );
142 }
143 }
144
145 private static function loadModes() {
146 if ( self::$modeMapping === null ) {
147 self::$modeMapping = [
148 'traditional' => TraditionalImageGallery::class,
149 'nolines' => NolinesImageGallery::class,
150 'packed' => PackedImageGallery::class,
151 'packed-hover' => PackedHoverImageGallery::class,
152 'packed-overlay' => PackedOverlayImageGallery::class,
153 'slideshow' => SlideshowImageGallery::class,
154 ];
155 // Allow extensions to make a new gallery format.
156 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
157 ->onGalleryGetModes( self::$modeMapping );
158 }
159 }
160
174 public function __construct( $mode = 'traditional', ?IContextSource $context = null ) {
175 if ( $context ) {
176 $this->setContext( $context );
177 }
178
179 $galleryOptions = $this->getConfig()->get( MainConfigNames::GalleryOptions );
180 $this->mImages = [];
181 $this->mShowBytes = $galleryOptions['showBytes'];
182 $this->mShowDimensions = $galleryOptions['showDimensions'];
183 $this->mShowFilename = true;
184 $this->mParser = false;
185 $this->mHideBadImages = false;
186 $this->mPerRow = $galleryOptions['imagesPerRow'];
187 $this->mWidths = $galleryOptions['imageWidth'];
188 $this->mHeights = $galleryOptions['imageHeight'];
189 $this->mCaptionLength = $galleryOptions['captionLength'];
190 $this->mMode = $mode;
191 }
192
203 public function setParser( $parser ) {
204 $this->mParser = $parser;
205 }
206
210 public function setHideBadImages( $flag = true ) {
211 $this->mHideBadImages = $flag;
212 }
213
219 public function setCaption( $caption ) {
220 $this->mCaption = htmlspecialchars( $caption );
221 }
222
228 public function setCaptionHtml( $caption ) {
229 $this->mCaption = $caption;
230 }
231
238 public function setPerRow( $num ) {
239 if ( $num >= 0 ) {
240 $this->mPerRow = (int)$num;
241 }
242 }
243
250 public function setWidths( $num ) {
251 $parser = $this->mParser;
252 if ( !$parser ) {
253 wfDeprecated( __METHOD__ . ' without parser', '1.43' );
254 $parser = MediaWikiServices::getInstance()->getParser();
255 }
256 $parsed = $parser->parseWidthParam( $num, false );
257 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
258 $this->mWidths = $parsed['width'];
259 }
260 }
261
268 public function setHeights( $num ) {
269 $parser = $this->mParser;
270 if ( !$parser ) {
271 wfDeprecated( __METHOD__ . ' without parser', '1.43' );
272 $parser = MediaWikiServices::getInstance()->getParser();
273 }
274 $parsed = $parser->parseWidthParam( $num, false );
275 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
276 $this->mHeights = $parsed['width'];
277 }
278 }
279
289 public function setAdditionalOptions( $options ) {
290 }
291
304 public function add(
305 $title,
306 $html = '',
307 $alt = '',
308 $link = '',
309 $handlerOpts = [],
310 $loading = self::LOADING_DEFAULT,
311 ?array $imageOptions = null
312 ) {
313 if ( $title instanceof File ) {
314 // Old calling convention
315 $title = $title->getTitle();
316 }
317 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ];
318 wfDebug( 'ImageGallery::add ' . $title->getText() );
319 }
320
333 public function insert(
334 $title,
335 $html = '',
336 $alt = '',
337 $link = '',
338 $handlerOpts = [],
339 $loading = self::LOADING_DEFAULT,
340 ?array $imageOptions = null
341 ) {
342 if ( $title instanceof File ) {
343 // Old calling convention
344 $title = $title->getTitle();
345 }
346 array_unshift( $this->mImages, [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ] );
347 }
348
354 public function getImages() {
355 return $this->mImages;
356 }
357
362 public function isEmpty() {
363 return $this->mImages === [];
364 }
365
372 public function setShowDimensions( $f ) {
373 $this->mShowDimensions = (bool)$f;
374 }
375
382 public function setShowBytes( $f ) {
383 $this->mShowBytes = (bool)$f;
384 }
385
392 public function setShowFilename( $f ) {
393 $this->mShowFilename = (bool)$f;
394 }
395
405 public function setAttributes( $attribs ) {
406 $this->mAttribs = $attribs;
407 }
408
414 abstract public function toHTML();
415
419 public function count() {
420 return count( $this->mImages );
421 }
422
428 public function setContextTitle( $title ) {
429 $this->contextTitle = $title;
430 }
431
437 public function getContextTitle() {
438 return $this->contextTitle;
439 }
440
445 protected function getRenderLang() {
446 return $this->mParser
447 ? $this->mParser->getTargetLanguage()
448 : $this->getLanguage();
449 }
450}
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.
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.
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)
__construct( $mode='traditional', ?IContextSource $context=null)
Create a new image gallery object.
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.
static factory( $mode=false, ?IContextSource $context=null)
Get a new image gallery.
setContextTitle( $title)
Set the contextual title.
bool $mShowFilename
Whether to show the filename.
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.
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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Base class for language-specific code.
Definition Language.php:81
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:147
Represents a title within MediaWiki.
Definition Title.php:78
Interface for objects which can provide a MediaWiki context on request.