MediaWiki REL1_37
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
200 public function setHideBadImages( $flag = true ) {
201 $this->mHideBadImages = $flag;
202 }
203
209 public function setCaption( $caption ) {
210 $this->mCaption = htmlspecialchars( $caption );
211 }
212
218 public function setCaptionHtml( $caption ) {
219 $this->mCaption = $caption;
220 }
221
228 public function setPerRow( $num ) {
229 if ( $num >= 0 ) {
230 $this->mPerRow = (int)$num;
231 }
232 }
233
240 public function setWidths( $num ) {
241 $parsed = Parser::parseWidthParam( $num, false );
242 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
243 $this->mWidths = $parsed['width'];
244 }
245 }
246
253 public function setHeights( $num ) {
254 $parsed = Parser::parseWidthParam( $num, false );
255 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
256 $this->mHeights = $parsed['width'];
257 }
258 }
259
269 public function setAdditionalOptions( $options ) {
270 }
271
283 public function add(
284 $title,
285 $html = '',
286 $alt = '',
287 $link = '',
288 $handlerOpts = [],
289 $loading = self::LOADING_DEFAULT
290 ) {
291 if ( $title instanceof File ) {
292 // Old calling convention
293 $title = $title->getTitle();
294 }
295 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading ];
296 wfDebug( 'ImageGallery::add ' . $title->getText() );
297 }
298
310 public function insert(
311 $title,
312 $html = '',
313 $alt = '',
314 $link = '',
315 $handlerOpts = [],
316 $loading = self::LOADING_DEFAULT
317 ) {
318 if ( $title instanceof File ) {
319 // Old calling convention
320 $title = $title->getTitle();
321 }
322 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading ] );
323 }
324
330 public function getImages() {
331 return $this->mImages;
332 }
333
338 public function isEmpty() {
339 return empty( $this->mImages );
340 }
341
348 public function setShowDimensions( $f ) {
349 $this->mShowDimensions = (bool)$f;
350 }
351
358 public function setShowBytes( $f ) {
359 $this->mShowBytes = (bool)$f;
360 }
361
368 public function setShowFilename( $f ) {
369 $this->mShowFilename = (bool)$f;
370 }
371
381 public function setAttributes( $attribs ) {
382 $this->mAttribs = $attribs;
383 }
384
390 abstract public function toHTML();
391
395 public function count() {
396 return count( $this->mImages );
397 }
398
404 public function setContextTitle( $title ) {
405 $this->contextTitle = $title;
406 }
407
413 public function getContextTitle() {
414 return $this->contextTitle;
415 }
416
421 protected function getRenderLang() {
422 return $this->mParser
423 ? $this->mParser->getTargetLanguage()
424 : $this->getLanguage();
425 }
426}
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:66
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 bad images?
setCaption( $caption)
Set the caption (as plain text)
setHideBadImages( $flag=true)
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.
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:91
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition Parser.php:6279
Represents a title within MediaWiki.
Definition Title.php:48
Interface for objects which can provide a MediaWiki context on request.
getConfig()
Get the site configuration.