MediaWiki  master
ImageGalleryBase.php
Go to the documentation of this file.
1 <?php
26 
34 abstract class ImageGalleryBase extends ContextSource {
35  public const LOADING_DEFAULT = 1;
36  public const LOADING_LAZY = 2;
37 
42  protected $mImages;
43 
47  protected $mShowBytes;
48 
52  protected $mShowDimensions;
53 
57  protected $mShowFilename;
58 
62  protected $mMode;
63 
67  protected $mCaption = false;
68 
76  protected $mCaptionLength = true;
77 
81  protected $mHideBadImages;
82 
86  public $mParser;
87 
92  protected $contextTitle = null;
93 
95  protected $mAttribs = [];
96 
98  protected $mPerRow;
99 
101  protected $mWidths;
102 
104  protected $mHeights;
105 
107  private static $modeMapping;
108 
118  public static function factory( $mode = false, IContextSource $context = null ) {
119  self::loadModes();
120  if ( !$context ) {
121  $context = RequestContext::getMainAndWarn( __METHOD__ );
122  }
123  if ( !$mode ) {
124  $galleryOptions = $context->getConfig()->get( MainConfigNames::GalleryOptions );
125  $mode = $galleryOptions['mode'];
126  }
127 
128  $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
129 
130  if ( isset( self::$modeMapping[$mode] ) ) {
131  $class = self::$modeMapping[$mode];
132  return new $class( $mode, $context );
133  } else {
134  throw new ImageGalleryClassNotFoundException( "No gallery class registered for mode $mode" );
135  }
136  }
137 
138  private static function loadModes() {
139  if ( self::$modeMapping === null ) {
140  self::$modeMapping = [
141  'traditional' => TraditionalImageGallery::class,
142  'nolines' => NolinesImageGallery::class,
143  'packed' => PackedImageGallery::class,
144  'packed-hover' => PackedHoverImageGallery::class,
145  'packed-overlay' => PackedOverlayImageGallery::class,
146  'slideshow' => SlideshowImageGallery::class,
147  ];
148  // Allow extensions to make a new gallery format.
149  Hooks::runner()->onGalleryGetModes( self::$modeMapping );
150  }
151  }
152 
166  public function __construct( $mode = 'traditional', IContextSource $context = null ) {
167  if ( $context ) {
168  $this->setContext( $context );
169  }
170 
171  $galleryOptions = $this->getConfig()->get( MainConfigNames::GalleryOptions );
172  $this->mImages = [];
173  $this->mShowBytes = $galleryOptions['showBytes'];
174  $this->mShowDimensions = $galleryOptions['showDimensions'];
175  $this->mShowFilename = true;
176  $this->mParser = false;
177  $this->mHideBadImages = false;
178  $this->mPerRow = $galleryOptions['imagesPerRow'];
179  $this->mWidths = $galleryOptions['imageWidth'];
180  $this->mHeights = $galleryOptions['imageHeight'];
181  $this->mCaptionLength = $galleryOptions['captionLength'];
182  $this->mMode = $mode;
183  }
184 
195  public function setParser( $parser ) {
196  $this->mParser = $parser;
197  }
198 
202  public function setHideBadImages( $flag = true ) {
203  $this->mHideBadImages = $flag;
204  }
205 
211  public function setCaption( $caption ) {
212  $this->mCaption = htmlspecialchars( $caption );
213  }
214 
220  public function setCaptionHtml( $caption ) {
221  $this->mCaption = $caption;
222  }
223 
230  public function setPerRow( $num ) {
231  if ( $num >= 0 ) {
232  $this->mPerRow = (int)$num;
233  }
234  }
235 
242  public function setWidths( $num ) {
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  $parsed = Parser::parseWidthParam( $num, false );
257  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
258  $this->mHeights = $parsed['width'];
259  }
260  }
261 
271  public function setAdditionalOptions( $options ) {
272  }
273 
286  public function add(
287  $title,
288  $html = '',
289  $alt = '',
290  $link = '',
291  $handlerOpts = [],
292  $loading = self::LOADING_DEFAULT,
293  ?array $imageOptions = null
294  ) {
295  if ( $title instanceof File ) {
296  // Old calling convention
297  $title = $title->getTitle();
298  }
299  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ];
300  wfDebug( 'ImageGallery::add ' . $title->getText() );
301  }
302 
315  public function insert(
316  $title,
317  $html = '',
318  $alt = '',
319  $link = '',
320  $handlerOpts = [],
321  $loading = self::LOADING_DEFAULT,
322  ?array $imageOptions = null
323  ) {
324  if ( $title instanceof File ) {
325  // Old calling convention
326  $title = $title->getTitle();
327  }
328  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ] );
329  }
330 
336  public function getImages() {
337  return $this->mImages;
338  }
339 
344  public function isEmpty() {
345  return empty( $this->mImages );
346  }
347 
354  public function setShowDimensions( $f ) {
355  $this->mShowDimensions = (bool)$f;
356  }
357 
364  public function setShowBytes( $f ) {
365  $this->mShowBytes = (bool)$f;
366  }
367 
374  public function setShowFilename( $f ) {
375  $this->mShowFilename = (bool)$f;
376  }
377 
387  public function setAttributes( $attribs ) {
388  $this->mAttribs = $attribs;
389  }
390 
396  abstract public function toHTML();
397 
401  public function count() {
402  return count( $this->mImages );
403  }
404 
410  public function setContextTitle( $title ) {
411  $this->contextTitle = $title;
412  }
413 
419  public function getContextTitle() {
420  return $this->contextTitle;
421  }
422 
427  protected function getRenderLang() {
428  return $this->mParser
429  ? $this->mParser->getTargetLanguage()
430  : $this->getLanguage();
431  }
432 }
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
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
Image gallery.
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.
Stub object for the user language.
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition: Parser.php:6363
static getMainAndWarn( $func=__METHOD__)
Get the RequestContext object associated with the main request and gives a warning to the log,...
Interface for objects which can provide a MediaWiki context on request.