MediaWiki  master
ImageGalleryBase.php
Go to the documentation of this file.
1 <?php
27 
35 abstract class ImageGalleryBase extends ContextSource {
36  public const LOADING_DEFAULT = 1;
37  public const LOADING_LAZY = 2;
38 
43  protected $mImages;
44 
48  protected $mShowBytes;
49 
53  protected $mShowDimensions;
54 
58  protected $mShowFilename;
59 
63  protected $mMode;
64 
68  protected $mCaption = false;
69 
77  protected $mCaptionLength = true;
78 
82  protected $mHideBadImages;
83 
87  public $mParser;
88 
93  protected $contextTitle = null;
94 
96  protected $mAttribs = [];
97 
99  protected $mPerRow;
100 
102  protected $mWidths;
103 
105  protected $mHeights;
106 
108  private static $modeMapping;
109 
119  public static function factory( $mode = false, IContextSource $context = null ) {
120  self::loadModes();
121  if ( !$context ) {
122  $context = RequestContext::getMainAndWarn( __METHOD__ );
123  }
124  if ( !$mode ) {
125  $galleryOptions = $context->getConfig()->get( MainConfigNames::GalleryOptions );
126  $mode = $galleryOptions['mode'];
127  }
128 
129  $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
130 
131  if ( isset( self::$modeMapping[$mode] ) ) {
132  $class = self::$modeMapping[$mode];
133  return new $class( $mode, $context );
134  } else {
135  throw new ImageGalleryClassNotFoundException( "No gallery class registered for mode $mode" );
136  }
137  }
138 
139  private static function loadModes() {
140  if ( self::$modeMapping === null ) {
141  self::$modeMapping = [
142  'traditional' => TraditionalImageGallery::class,
143  'nolines' => NolinesImageGallery::class,
144  'packed' => PackedImageGallery::class,
145  'packed-hover' => PackedHoverImageGallery::class,
146  'packed-overlay' => PackedOverlayImageGallery::class,
147  'slideshow' => SlideshowImageGallery::class,
148  ];
149  // Allow extensions to make a new gallery format.
150  ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
151  ->onGalleryGetModes( self::$modeMapping );
152  }
153  }
154 
168  public function __construct( $mode = 'traditional', IContextSource $context = null ) {
169  if ( $context ) {
170  $this->setContext( $context );
171  }
172 
173  $galleryOptions = $this->getConfig()->get( MainConfigNames::GalleryOptions );
174  $this->mImages = [];
175  $this->mShowBytes = $galleryOptions['showBytes'];
176  $this->mShowDimensions = $galleryOptions['showDimensions'];
177  $this->mShowFilename = true;
178  $this->mParser = false;
179  $this->mHideBadImages = false;
180  $this->mPerRow = $galleryOptions['imagesPerRow'];
181  $this->mWidths = $galleryOptions['imageWidth'];
182  $this->mHeights = $galleryOptions['imageHeight'];
183  $this->mCaptionLength = $galleryOptions['captionLength'];
184  $this->mMode = $mode;
185  }
186 
197  public function setParser( $parser ) {
198  $this->mParser = $parser;
199  }
200 
204  public function setHideBadImages( $flag = true ) {
205  $this->mHideBadImages = $flag;
206  }
207 
213  public function setCaption( $caption ) {
214  $this->mCaption = htmlspecialchars( $caption );
215  }
216 
222  public function setCaptionHtml( $caption ) {
223  $this->mCaption = $caption;
224  }
225 
232  public function setPerRow( $num ) {
233  if ( $num >= 0 ) {
234  $this->mPerRow = (int)$num;
235  }
236  }
237 
244  public function setWidths( $num ) {
245  $parsed = Parser::parseWidthParam( $num, false );
246  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
247  $this->mWidths = $parsed['width'];
248  }
249  }
250 
257  public function setHeights( $num ) {
258  $parsed = Parser::parseWidthParam( $num, false );
259  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
260  $this->mHeights = $parsed['width'];
261  }
262  }
263 
273  public function setAdditionalOptions( $options ) {
274  }
275 
288  public function add(
289  $title,
290  $html = '',
291  $alt = '',
292  $link = '',
293  $handlerOpts = [],
294  $loading = self::LOADING_DEFAULT,
295  ?array $imageOptions = null
296  ) {
297  if ( $title instanceof File ) {
298  // Old calling convention
299  $title = $title->getTitle();
300  }
301  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ];
302  wfDebug( 'ImageGallery::add ' . $title->getText() );
303  }
304 
317  public function insert(
318  $title,
319  $html = '',
320  $alt = '',
321  $link = '',
322  $handlerOpts = [],
323  $loading = self::LOADING_DEFAULT,
324  ?array $imageOptions = null
325  ) {
326  if ( $title instanceof File ) {
327  // Old calling convention
328  $title = $title->getTitle();
329  }
330  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ] );
331  }
332 
338  public function getImages() {
339  return $this->mImages;
340  }
341 
346  public function isEmpty() {
347  return $this->mImages === [];
348  }
349 
356  public function setShowDimensions( $f ) {
357  $this->mShowDimensions = (bool)$f;
358  }
359 
366  public function setShowBytes( $f ) {
367  $this->mShowBytes = (bool)$f;
368  }
369 
376  public function setShowFilename( $f ) {
377  $this->mShowFilename = (bool)$f;
378  }
379 
389  public function setAttributes( $attribs ) {
390  $this->mAttribs = $attribs;
391  }
392 
398  abstract public function toHTML();
399 
403  public function count() {
404  return count( $this->mImages );
405  }
406 
412  public function setContextTitle( $title ) {
413  $this->contextTitle = $title;
414  }
415 
421  public function getContextTitle() {
422  return $this->contextTitle;
423  }
424 
429  protected function getRenderLang() {
430  return $this->mParser
431  ? $this->mParser->getTargetLanguage()
432  : $this->getLanguage();
433  }
434 }
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:70
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().
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:568
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition: Title.php:76
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition: Parser.php:6284
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.