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  public 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 
305  public function getImages() {
306  return $this->mImages;
307  }
308 
313  function isEmpty() {
314  return empty( $this->mImages );
315  }
316 
323  function setShowDimensions( $f ) {
324  $this->mShowDimensions = (bool)$f;
325  }
326 
333  function setShowBytes( $f ) {
334  $this->mShowBytes = (bool)$f;
335  }
336 
343  function setShowFilename( $f ) {
344  $this->mShowFilename = (bool)$f;
345  }
346 
356  function setAttributes( $attribs ) {
357  $this->mAttribs = $attribs;
358  }
359 
365  abstract public function toHTML();
366 
370  public function count() {
371  return count( $this->mImages );
372  }
373 
379  public function setContextTitle( $title ) {
380  $this->contextTitle = $title;
381  }
382 
388  public function getContextTitle() {
389  return $this->contextTitle;
390  }
391 
396  protected function getRenderLang() {
397  return $this->mParser
398  ? $this->mParser->getTargetLanguage()
399  : $this->getLanguage();
400  }
401 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:33
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
ImageGalleryBase\setShowBytes
setShowBytes( $f)
Enable/Disable showing of the file size of an image in the gallery.
Definition: ImageGalleryBase.php:333
ImageGalleryBase\setParser
setParser( $parser)
Register a parser object.
Definition: ImageGalleryBase.php:185
ImageGalleryBase\$mShowDimensions
bool $mShowDimensions
Whether to show the dimensions in categories.
Definition: ImageGalleryBase.php:47
ImageGalleryBase\setHeights
setHeights( $num)
Set how high each image will be, in pixels.
Definition: ImageGalleryBase.php:246
ImageGalleryBase\$modeMapping
static array $modeMapping
Definition: ImageGalleryBase.php:102
ImageGalleryBase\setContextTitle
setContextTitle( $title)
Set the contextual title.
Definition: ImageGalleryBase.php:379
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:130
ImageGalleryBase\$mMode
string $mMode
Gallery mode.
Definition: ImageGalleryBase.php:57
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:32
ImageGalleryBase\setWidths
setWidths( $num)
Set how wide each image will be, in pixels.
Definition: ImageGalleryBase.php:233
ImageGalleryBase\setCaption
setCaption( $caption)
Set the caption (as plain text)
Definition: ImageGalleryBase.php:202
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:388
ImageGalleryBase\setShowFilename
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
Definition: ImageGalleryBase.php:343
ImageGalleryBase\insert
insert( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image at the beginning of the gallery.
Definition: ImageGalleryBase.php:292
ImageGalleryBase\$mCaption
bool string $mCaption
Gallery caption.
Definition: ImageGalleryBase.php:62
ImageGalleryBase\$mHeights
int $mHeights
Definition: ImageGalleryBase.php:99
ImageGalleryBase\setAdditionalOptions
setAdditionalOptions( $options)
Allow setting additional options.
Definition: ImageGalleryBase.php:260
ImageGalleryBase\$mCaptionLength
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
Definition: ImageGalleryBase.php:71
ImageGalleryBase\add
add( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image to the gallery.
Definition: ImageGalleryBase.php:273
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
ImageGalleryBase\$mPerRow
int $mPerRow
Definition: ImageGalleryBase.php:93
ImageGalleryBase\setAttributes
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
Definition: ImageGalleryBase.php:356
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
ImageGalleryBase\setHideBadImages
setHideBadImages( $flag=true)
Set bad image flag.
Definition: ImageGalleryBase.php:193
ImageGalleryBase\setPerRow
setPerRow( $num)
Set how many images will be displayed per row.
Definition: ImageGalleryBase.php:221
MWException
MediaWiki exception.
Definition: MWException.php:26
ImageGalleryBase\$mShowFilename
bool $mShowFilename
Whether to show the filename.
Definition: ImageGalleryBase.php:52
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:29
$title
$title
Definition: testCompression.php:36
RequestContext\getMainAndWarn
static getMainAndWarn( $func=__METHOD__)
Get the RequestContext object associated with the main request and gives a warning to the log,...
Definition: RequestContext.php:463
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:55
ImageGalleryBase\getImages
getImages()
Returns the list of images this gallery contains.
Definition: ImageGalleryBase.php:305
ImageGalleryBase\__construct
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
Definition: ImageGalleryBase.php:156
ImageGalleryBase\$mParser
Parser false $mParser
Registered parser object for output callbacks.
Definition: ImageGalleryBase.php:81
ImageGalleryBase\loadModes
static loadModes()
Definition: ImageGalleryBase.php:133
ImageGalleryBase\$mShowBytes
bool $mShowBytes
Whether to show the filesize in bytes in categories.
Definition: ImageGalleryBase.php:42
ImageGalleryBase\setCaptionHtml
setCaptionHtml( $caption)
Set the caption (as HTML)
Definition: ImageGalleryBase.php:211
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
ImageGalleryBase\$mImages
array[] $mImages
Gallery images @phan-var array<int,array{0:Title,1:string,2:string,3:string,4:array}>
Definition: ImageGalleryBase.php:37
ImageGalleryBase\getRenderLang
getRenderLang()
Determines the correct language to be used for this image gallery.
Definition: ImageGalleryBase.php:396
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ImageGalleryBase\isEmpty
isEmpty()
isEmpty() returns true if the gallery contains no images
Definition: ImageGalleryBase.php:313
ImageGalleryBase\$mWidths
int $mWidths
Definition: ImageGalleryBase.php:96
ImageGalleryBase\setShowDimensions
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
Definition: ImageGalleryBase.php:323
IContextSource\getConfig
getConfig()
Get the site configuration.
ImageGalleryBase\$contextTitle
Title null $contextTitle
Contextual title, used when images are being screened against the bad image list.
Definition: ImageGalleryBase.php:87
ImageGalleryBase\count
count()
Definition: ImageGalleryBase.php:370
ImageGalleryBase\factory
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
Definition: ImageGalleryBase.php:113
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
ImageGalleryBase\toHTML
toHTML()
Display an html representation of the gallery.
ImageGalleryBase\$mHideBadImages
bool $mHideBadImages
Hide blacklisted images?
Definition: ImageGalleryBase.php:76
ImageGalleryBase\$mAttribs
array $mAttribs
Definition: ImageGalleryBase.php:90