MediaWiki REL1_39
ImageGalleryBase.php
Go to the documentation of this file.
1<?php
25
33abstract class ImageGalleryBase extends ContextSource {
34 public const LOADING_DEFAULT = 1;
35 public const LOADING_LAZY = 2;
36
41 protected $mImages;
42
46 protected $mShowBytes;
47
52
56 protected $mShowFilename;
57
61 protected $mMode;
62
66 protected $mCaption = false;
67
75 protected $mCaptionLength = true;
76
80 protected $mHideBadImages;
81
85 public $mParser;
86
91 protected $contextTitle = null;
92
94 protected $mAttribs = [];
95
97 protected $mPerRow;
98
100 protected $mWidths;
101
103 protected $mHeights;
104
106 private static $modeMapping;
107
117 public static function factory( $mode = false, IContextSource $context = null ) {
118 self::loadModes();
119 if ( !$context ) {
120 $context = RequestContext::getMainAndWarn( __METHOD__ );
121 }
122 if ( !$mode ) {
123 $galleryOptions = $context->getConfig()->get( MainConfigNames::GalleryOptions );
124 $mode = $galleryOptions['mode'];
125 }
126
127 $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
128
129 if ( isset( self::$modeMapping[$mode] ) ) {
130 $class = self::$modeMapping[$mode];
131 return new $class( $mode, $context );
132 } else {
133 throw new ImageGalleryClassNotFoundException( "No gallery class registered for mode $mode" );
134 }
135 }
136
137 private static function loadModes() {
138 if ( self::$modeMapping === null ) {
139 self::$modeMapping = [
140 'traditional' => TraditionalImageGallery::class,
141 'nolines' => NolinesImageGallery::class,
142 'packed' => PackedImageGallery::class,
143 'packed-hover' => PackedHoverImageGallery::class,
144 'packed-overlay' => PackedOverlayImageGallery::class,
145 'slideshow' => SlideshowImageGallery::class,
146 ];
147 // Allow extensions to make a new gallery format.
148 Hooks::runner()->onGalleryGetModes( self::$modeMapping );
149 }
150 }
151
165 public function __construct( $mode = 'traditional', IContextSource $context = null ) {
166 if ( $context ) {
167 $this->setContext( $context );
168 }
169
170 $galleryOptions = $this->getConfig()->get( MainConfigNames::GalleryOptions );
171 $this->mImages = [];
172 $this->mShowBytes = $galleryOptions['showBytes'];
173 $this->mShowDimensions = $galleryOptions['showDimensions'];
174 $this->mShowFilename = true;
175 $this->mParser = false;
176 $this->mHideBadImages = false;
177 $this->mPerRow = $galleryOptions['imagesPerRow'];
178 $this->mWidths = $galleryOptions['imageWidth'];
179 $this->mHeights = $galleryOptions['imageHeight'];
180 $this->mCaptionLength = $galleryOptions['captionLength'];
181 $this->mMode = $mode;
182 }
183
194 public function setParser( $parser ) {
195 $this->mParser = $parser;
196 }
197
201 public function setHideBadImages( $flag = true ) {
202 $this->mHideBadImages = $flag;
203 }
204
210 public function setCaption( $caption ) {
211 $this->mCaption = htmlspecialchars( $caption );
212 }
213
219 public function setCaptionHtml( $caption ) {
220 $this->mCaption = $caption;
221 }
222
229 public function setPerRow( $num ) {
230 if ( $num >= 0 ) {
231 $this->mPerRow = (int)$num;
232 }
233 }
234
241 public function setWidths( $num ) {
242 $parsed = Parser::parseWidthParam( $num, false );
243 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
244 $this->mWidths = $parsed['width'];
245 }
246 }
247
254 public function setHeights( $num ) {
255 $parsed = Parser::parseWidthParam( $num, false );
256 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
257 $this->mHeights = $parsed['width'];
258 }
259 }
260
270 public function setAdditionalOptions( $options ) {
271 }
272
285 public function add(
286 $title,
287 $html = '',
288 $alt = '',
289 $link = '',
290 $handlerOpts = [],
291 $loading = self::LOADING_DEFAULT,
292 ?array $imageOptions = null
293 ) {
294 if ( $title instanceof File ) {
295 // Old calling convention
296 $title = $title->getTitle();
297 }
298 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ];
299 wfDebug( 'ImageGallery::add ' . $title->getText() );
300 }
301
314 public function insert(
315 $title,
316 $html = '',
317 $alt = '',
318 $link = '',
319 $handlerOpts = [],
320 $loading = self::LOADING_DEFAULT,
321 ?array $imageOptions = null
322 ) {
323 if ( $title instanceof File ) {
324 // Old calling convention
325 $title = $title->getTitle();
326 }
327 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ] );
328 }
329
335 public function getImages() {
336 return $this->mImages;
337 }
338
343 public function isEmpty() {
344 return empty( $this->mImages );
345 }
346
353 public function setShowDimensions( $f ) {
354 $this->mShowDimensions = (bool)$f;
355 }
356
363 public function setShowBytes( $f ) {
364 $this->mShowBytes = (bool)$f;
365 }
366
373 public function setShowFilename( $f ) {
374 $this->mShowFilename = (bool)$f;
375 }
376
386 public function setAttributes( $attribs ) {
387 $this->mAttribs = $attribs;
388 }
389
395 abstract public function toHTML();
396
400 public function count() {
401 return count( $this->mImages );
402 }
403
409 public function setContextTitle( $title ) {
410 $this->contextTitle = $title;
411 }
412
418 public function getContextTitle() {
419 return $this->contextTitle;
420 }
421
426 protected function getRenderLang() {
427 return $this->mParser
428 ? $this->mParser->getTargetLanguage()
429 : $this->getLanguage();
430 }
431}
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 ...
setContext(IContextSource $context)
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:67
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().
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:96
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition Parser.php:6431
Represents a title within MediaWiki.
Definition Title.php:49
Interface for objects which can provide a MediaWiki context on request.