MediaWiki  master
ImageGalleryBase.php
Go to the documentation of this file.
1 <?php
28 
36 abstract class ImageGalleryBase extends ContextSource {
37  public const LOADING_DEFAULT = 1;
38  public const LOADING_LAZY = 2;
39 
44  protected $mImages;
45 
49  protected $mShowBytes;
50 
54  protected $mShowDimensions;
55 
59  protected $mShowFilename;
60 
64  protected $mMode;
65 
69  protected $mCaption = false;
70 
78  protected $mCaptionLength = true;
79 
83  protected $mHideBadImages;
84 
88  public $mParser;
89 
94  protected $contextTitle = null;
95 
97  protected $mAttribs = [];
98 
100  protected $mPerRow;
101 
103  protected $mWidths;
104 
106  protected $mHeights;
107 
109  private static $modeMapping;
110 
120  public static function factory( $mode = false, IContextSource $context = null ) {
121  self::loadModes();
122  if ( !$context ) {
123  $context = RequestContext::getMainAndWarn( __METHOD__ );
124  }
125  if ( !$mode ) {
126  $galleryOptions = $context->getConfig()->get( MainConfigNames::GalleryOptions );
127  $mode = $galleryOptions['mode'];
128  }
129 
130  $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
131 
132  if ( isset( self::$modeMapping[$mode] ) ) {
133  $class = self::$modeMapping[$mode];
134  return new $class( $mode, $context );
135  } else {
136  throw new ImageGalleryClassNotFoundException( "No gallery class registered for mode $mode" );
137  }
138  }
139 
140  private static function loadModes() {
141  if ( self::$modeMapping === null ) {
142  self::$modeMapping = [
143  'traditional' => TraditionalImageGallery::class,
144  'nolines' => NolinesImageGallery::class,
145  'packed' => PackedImageGallery::class,
146  'packed-hover' => PackedHoverImageGallery::class,
147  'packed-overlay' => PackedOverlayImageGallery::class,
148  'slideshow' => SlideshowImageGallery::class,
149  ];
150  // Allow extensions to make a new gallery format.
151  ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
152  ->onGalleryGetModes( self::$modeMapping );
153  }
154  }
155 
169  public function __construct( $mode = 'traditional', IContextSource $context = null ) {
170  if ( $context ) {
171  $this->setContext( $context );
172  }
173 
174  $galleryOptions = $this->getConfig()->get( MainConfigNames::GalleryOptions );
175  $this->mImages = [];
176  $this->mShowBytes = $galleryOptions['showBytes'];
177  $this->mShowDimensions = $galleryOptions['showDimensions'];
178  $this->mShowFilename = true;
179  $this->mParser = false;
180  $this->mHideBadImages = false;
181  $this->mPerRow = $galleryOptions['imagesPerRow'];
182  $this->mWidths = $galleryOptions['imageWidth'];
183  $this->mHeights = $galleryOptions['imageHeight'];
184  $this->mCaptionLength = $galleryOptions['captionLength'];
185  $this->mMode = $mode;
186  }
187 
198  public function setParser( $parser ) {
199  $this->mParser = $parser;
200  }
201 
205  public function setHideBadImages( $flag = true ) {
206  $this->mHideBadImages = $flag;
207  }
208 
214  public function setCaption( $caption ) {
215  $this->mCaption = htmlspecialchars( $caption );
216  }
217 
223  public function setCaptionHtml( $caption ) {
224  $this->mCaption = $caption;
225  }
226 
233  public function setPerRow( $num ) {
234  if ( $num >= 0 ) {
235  $this->mPerRow = (int)$num;
236  }
237  }
238 
245  public function setWidths( $num ) {
246  $parsed = Parser::parseWidthParam( $num, false );
247  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
248  $this->mWidths = $parsed['width'];
249  }
250  }
251 
258  public function setHeights( $num ) {
259  $parsed = Parser::parseWidthParam( $num, false );
260  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
261  $this->mHeights = $parsed['width'];
262  }
263  }
264 
274  public function setAdditionalOptions( $options ) {
275  }
276 
289  public function add(
290  $title,
291  $html = '',
292  $alt = '',
293  $link = '',
294  $handlerOpts = [],
295  $loading = self::LOADING_DEFAULT,
296  ?array $imageOptions = null
297  ) {
298  if ( $title instanceof File ) {
299  // Old calling convention
300  $title = $title->getTitle();
301  }
302  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ];
303  wfDebug( 'ImageGallery::add ' . $title->getText() );
304  }
305 
318  public function insert(
319  $title,
320  $html = '',
321  $alt = '',
322  $link = '',
323  $handlerOpts = [],
324  $loading = self::LOADING_DEFAULT,
325  ?array $imageOptions = null
326  ) {
327  if ( $title instanceof File ) {
328  // Old calling convention
329  $title = $title->getTitle();
330  }
331  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ] );
332  }
333 
339  public function getImages() {
340  return $this->mImages;
341  }
342 
347  public function isEmpty() {
348  return empty( $this->mImages );
349  }
350 
357  public function setShowDimensions( $f ) {
358  $this->mShowDimensions = (bool)$f;
359  }
360 
367  public function setShowBytes( $f ) {
368  $this->mShowBytes = (bool)$f;
369  }
370 
377  public function setShowFilename( $f ) {
378  $this->mShowFilename = (bool)$f;
379  }
380 
390  public function setAttributes( $attribs ) {
391  $this->mAttribs = $attribs;
392  }
393 
399  abstract public function toHTML();
400 
404  public function count() {
405  return count( $this->mImages );
406  }
407 
413  public function setContextTitle( $title ) {
414  $this->contextTitle = $title;
415  }
416 
422  public function getContextTitle() {
423  return $this->contextTitle;
424  }
425 
430  protected function getRenderLang() {
431  return $this->mParser
432  ? $this->mParser->getTargetLanguage()
433  : $this->getLanguage();
434  }
435 }
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:68
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:566
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Stub object for the user language.
Represents a title within MediaWiki.
Definition: Title.php:82
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition: Parser.php:6397
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.