MediaWiki REL1_35
ImageGalleryBase.php
Go to the documentation of this file.
1<?php
24
32abstract class ImageGalleryBase extends ContextSource {
33 public const LOADING_DEFAULT = 1;
34 public const LOADING_LAZY = 2;
35
40 protected $mImages;
41
45 protected $mShowBytes;
46
51
55 protected $mShowFilename;
56
60 protected $mMode;
61
65 protected $mCaption = false;
66
74 protected $mCaptionLength = true;
75
79 protected $mHideBadImages;
80
84 public $mParser;
85
90 protected $contextTitle = null;
91
93 protected $mAttribs = [];
94
96 protected $mPerRow;
97
99 protected $mWidths;
100
102 protected $mHeights;
103
105 private static $modeMapping;
106
116 public static function factory( $mode = false, IContextSource $context = null ) {
117 self::loadModes();
118 if ( !$context ) {
119 $context = RequestContext::getMainAndWarn( __METHOD__ );
120 }
121 if ( !$mode ) {
122 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
123 $mode = $galleryOptions['mode'];
124 }
125
126 $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
127
128 if ( isset( self::$modeMapping[$mode] ) ) {
129 $class = self::$modeMapping[$mode];
130 return new $class( $mode, $context );
131 } else {
132 throw new MWException( "No gallery class registered for mode $mode" );
133 }
134 }
135
136 private static function loadModes() {
137 if ( self::$modeMapping === null ) {
138 self::$modeMapping = [
139 'traditional' => TraditionalImageGallery::class,
140 'nolines' => NolinesImageGallery::class,
141 'packed' => PackedImageGallery::class,
142 'packed-hover' => PackedHoverImageGallery::class,
143 'packed-overlay' => PackedOverlayImageGallery::class,
144 'slideshow' => SlideshowImageGallery::class,
145 ];
146 // Allow extensions to make a new gallery format.
147 Hooks::runner()->onGalleryGetModes( self::$modeMapping );
148 }
149 }
150
164 public function __construct( $mode = 'traditional', IContextSource $context = null ) {
165 if ( $context ) {
166 $this->setContext( $context );
167 }
168
169 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
170 $this->mImages = [];
171 $this->mShowBytes = $galleryOptions['showBytes'];
172 $this->mShowDimensions = $galleryOptions['showDimensions'];
173 $this->mShowFilename = true;
174 $this->mParser = false;
175 $this->mHideBadImages = false;
176 $this->mPerRow = $galleryOptions['imagesPerRow'];
177 $this->mWidths = $galleryOptions['imageWidth'];
178 $this->mHeights = $galleryOptions['imageHeight'];
179 $this->mCaptionLength = $galleryOptions['captionLength'];
180 $this->mMode = $mode;
181 }
182
193 public function setParser( $parser ) {
194 $this->mParser = $parser;
195 }
196
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
284 public function add(
285 $title,
286 $html = '',
287 $alt = '',
288 $link = '',
289 $handlerOpts = [],
290 $loading = self::LOADING_DEFAULT
291 ) {
292 if ( $title instanceof File ) {
293 // Old calling convention
294 $title = $title->getTitle();
295 }
296 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading ];
297 wfDebug( 'ImageGallery::add ' . $title->getText() );
298 }
299
311 public function insert(
312 $title,
313 $html = '',
314 $alt = '',
315 $link = '',
316 $handlerOpts = [],
317 $loading = self::LOADING_DEFAULT
318 ) {
319 if ( $title instanceof File ) {
320 // Old calling convention
321 $title = $title->getTitle();
322 }
323 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading ] );
324 }
325
331 public function getImages() {
332 return $this->mImages;
333 }
334
339 public function isEmpty() {
340 return empty( $this->mImages );
341 }
342
349 public function setShowDimensions( $f ) {
350 $this->mShowDimensions = (bool)$f;
351 }
352
359 public function setShowBytes( $f ) {
360 $this->mShowBytes = (bool)$f;
361 }
362
369 public function setShowFilename( $f ) {
370 $this->mShowFilename = (bool)$f;
371 }
372
382 public function setAttributes( $attribs ) {
383 $this->mAttribs = $attribs;
384 }
385
391 abstract public function toHTML();
392
396 public function count() {
397 return count( $this->mImages );
398 }
399
405 public function setContextTitle( $title ) {
406 $this->contextTitle = $title;
407 }
408
414 public function getContextTitle() {
415 return $this->contextTitle;
416 }
417
422 protected function getRenderLang() {
423 return $this->mParser
424 ? $this->mParser->getTargetLanguage()
425 : $this->getLanguage();
426 }
427}
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 ...
IContextSource $context
setContext(IContextSource $context)
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:63
insert( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT)
Add an image at the beginning of the gallery.
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
bool string $mCaption
Gallery caption.
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
add( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT)
Add an image to the gallery.
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
bool $mHideBadImages
Hide blacklisted images?
setCaption( $caption)
Set the caption (as plain text)
setHideBadImages( $flag=true)
Set bad image flag.
static array $modeMapping
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.
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.
array[] $mImages
Gallery images -var array<int,array{0:Title,1:string,2:string,3:string,4:array,5:int}>
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:85
Represents a title within MediaWiki.
Definition Title.php:42
Interface for objects which can provide a MediaWiki context on request.
getConfig()
Get the site configuration.