MediaWiki  master
ImageGalleryBase.php
Go to the documentation of this file.
1 <?php
24 
32 abstract class ImageGalleryBase extends ContextSource {
37  protected $mImages;
38 
42  protected $mShowBytes;
43 
47  protected $mShowDimensions;
48 
52  protected $mShowFilename;
53 
57  protected $mMode;
58 
62  protected $mCaption = false;
63 
71  protected $mCaptionLength = true;
72 
76  protected $mHideBadImages;
77 
81  public $mParser;
82 
87  protected $contextTitle = null;
88 
90  protected $mAttribs = [];
91 
93  protected $mPerRow;
94 
96  protected $mWidths;
97 
99  protected $mHeights;
100 
102  private static $modeMapping;
103 
113  static function factory( $mode = false, IContextSource $context = null ) {
114  self::loadModes();
115  if ( !$context ) {
117  }
118  if ( !$mode ) {
119  $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
120  $mode = $galleryOptions['mode'];
121  }
122 
123  $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
124 
125  if ( isset( self::$modeMapping[$mode] ) ) {
126  $class = self::$modeMapping[$mode];
127  return new $class( $mode, $context );
128  } else {
129  throw new MWException( "No gallery class registered for mode $mode" );
130  }
131  }
132 
133  private static function loadModes() {
134  if ( self::$modeMapping === null ) {
135  self::$modeMapping = [
136  'traditional' => TraditionalImageGallery::class,
137  'nolines' => NolinesImageGallery::class,
138  'packed' => PackedImageGallery::class,
139  'packed-hover' => PackedHoverImageGallery::class,
140  'packed-overlay' => PackedOverlayImageGallery::class,
141  'slideshow' => SlideshowImageGallery::class,
142  ];
143  // Allow extensions to make a new gallery format.
144  Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
145  }
146  }
147 
156  function __construct( $mode = 'traditional', IContextSource $context = null ) {
157  if ( $context ) {
158  $this->setContext( $context );
159  }
160 
161  $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
162  $this->mImages = [];
163  $this->mShowBytes = $galleryOptions['showBytes'];
164  $this->mShowDimensions = $galleryOptions['showDimensions'];
165  $this->mShowFilename = true;
166  $this->mParser = false;
167  $this->mHideBadImages = false;
168  $this->mPerRow = $galleryOptions['imagesPerRow'];
169  $this->mWidths = $galleryOptions['imageWidth'];
170  $this->mHeights = $galleryOptions['imageHeight'];
171  $this->mCaptionLength = $galleryOptions['captionLength'];
172  $this->mMode = $mode;
173  }
174 
185  function setParser( $parser ) {
186  $this->mParser = $parser;
187  }
188 
193  function setHideBadImages( $flag = true ) {
194  $this->mHideBadImages = $flag;
195  }
196 
202  function setCaption( $caption ) {
203  $this->mCaption = htmlspecialchars( $caption );
204  }
205 
211  public function setCaptionHtml( $caption ) {
212  $this->mCaption = $caption;
213  }
214 
221  public function setPerRow( $num ) {
222  if ( $num >= 0 ) {
223  $this->mPerRow = (int)$num;
224  }
225  }
226 
233  public function setWidths( $num ) {
234  $parsed = Parser::parseWidthParam( $num, false );
235  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
236  $this->mWidths = $parsed['width'];
237  }
238  }
239 
246  public function setHeights( $num ) {
247  $parsed = Parser::parseWidthParam( $num, false );
248  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
249  $this->mHeights = $parsed['width'];
250  }
251  }
252 
260  public function setAdditionalOptions( $options ) {
261  }
262 
273  function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
274  if ( $title instanceof File ) {
275  // Old calling convention
276  $title = $title->getTitle();
277  }
278  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
279  wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
280  }
281 
292  function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
293  if ( $title instanceof File ) {
294  // Old calling convention
295  $title = $title->getTitle();
296  }
297  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
298  }
299 
304  public function getImages() {
305  return $this->mImages;
306  }
307 
312  function isEmpty() {
313  return empty( $this->mImages );
314  }
315 
322  function setShowDimensions( $f ) {
323  $this->mShowDimensions = (bool)$f;
324  }
325 
332  function setShowBytes( $f ) {
333  $this->mShowBytes = (bool)$f;
334  }
335 
342  function setShowFilename( $f ) {
343  $this->mShowFilename = (bool)$f;
344  }
345 
355  function setAttributes( $attribs ) {
356  $this->mAttribs = $attribs;
357  }
358 
364  abstract public function toHTML();
365 
369  public function count() {
370  return count( $this->mImages );
371  }
372 
378  public function setContextTitle( $title ) {
379  $this->contextTitle = $title;
380  }
381 
387  public function getContextTitle() {
388  return $this->contextTitle;
389  }
390 
395  protected function getRenderLang() {
396  return $this->mParser
397  ? $this->mParser->getTargetLanguage()
398  : $this->getLanguage();
399  }
400 }
setContext(IContextSource $context)
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setCaptionHtml( $caption)
Set the caption (as HTML)
bool $mShowBytes
Whether to show the filesize in bytes in categories.
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
array [] $mImages
Gallery images -var array<int,array{0:Title,1:string,2:string,3:string,4:array}>
IContextSource $context
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition: Parser.php:6445
getRenderLang()
Determines the correct language to be used for this image gallery.
getConfig()
Get the site configuration.
isEmpty()
isEmpty() returns true if the gallery contains no images
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
Title null $contextTitle
Contextual title, used when images are being screened against the bad image list. ...
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
setParser( $parser)
Register a parser object.
setCaption( $caption)
Set the caption (as plain text)
toHTML()
Display an html representation of the gallery.
setAdditionalOptions( $options)
Allow setting additional options.
setShowBytes( $f)
Enable/Disable showing of the file size of an image in the gallery.
bool $mHideBadImages
Hide blacklisted images?
setWidths( $num)
Set how wide each image will be, in pixels.
getContextTitle()
Get the contextual title, if applicable.
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
bool $mShowDimensions
Whether to show the dimensions in categories.
insert( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image at the beginning of the gallery.
Image gallery.
setContextTitle( $title)
Set the contextual title.
string $mMode
Gallery mode.
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
setHideBadImages( $flag=true)
Set bad image flag.
setHeights( $num)
Set how high each image will be, in pixels.
static array $modeMapping
bool string $mCaption
Gallery caption.
bool $mShowFilename
Whether to show the filename.
static getMainAndWarn( $func=__METHOD__)
Get the RequestContext object associated with the main request and gives a warning to the log...
add( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image to the gallery.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
getImages()
Returns the list of images this gallery contains.
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
setPerRow( $num)
Set how many images will be displayed per row.
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
Parser false $mParser
Registered parser object for output callbacks.