MediaWiki  master
ImageGalleryBase.php
Go to the documentation of this file.
1 <?php
24 
32 abstract class ImageGalleryBase extends ContextSource {
33  public const LOADING_DEFAULT = 1;
34  public const LOADING_LAZY = 2;
35 
40  protected $mImages;
41 
45  protected $mShowBytes;
46 
50  protected $mShowDimensions;
51 
55  protected $mShowFilename;
56 
60  protected $mMode;
61 
65  protected $mCaption = false;
66 
74  protected $mCaptionLength = true;
75 
79  protected $mHideBadImages;
80 
84  public $mParser;
85 
90  protected $contextTitle = null;
91 
93  protected $mAttribs = [];
94 
96  protected $mPerRow;
97 
99  protected $mWidths;
100 
102  protected $mHeights;
103 
105  private static $modeMapping;
106 
116  public static function factory( $mode = false, IContextSource $context = null ) {
117  self::loadModes();
118  if ( !$context ) {
120  }
121  if ( !$mode ) {
122  $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
123  $mode = $galleryOptions['mode'];
124  }
125 
126  $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
127 
128  if ( isset( self::$modeMapping[$mode] ) ) {
129  $class = self::$modeMapping[$mode];
130  return new $class( $mode, $context );
131  } else {
132  throw new MWException( "No gallery class registered for mode $mode" );
133  }
134  }
135 
136  private static function loadModes() {
137  if ( self::$modeMapping === null ) {
138  self::$modeMapping = [
139  'traditional' => TraditionalImageGallery::class,
140  'nolines' => NolinesImageGallery::class,
141  'packed' => PackedImageGallery::class,
142  'packed-hover' => PackedHoverImageGallery::class,
143  'packed-overlay' => PackedOverlayImageGallery::class,
144  'slideshow' => SlideshowImageGallery::class,
145  ];
146  // Allow extensions to make a new gallery format.
147  Hooks::runner()->onGalleryGetModes( self::$modeMapping );
148  }
149  }
150 
164  public function __construct( $mode = 'traditional', IContextSource $context = null ) {
165  if ( $context ) {
166  $this->setContext( $context );
167  }
168 
169  $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
170  $this->mImages = [];
171  $this->mShowBytes = $galleryOptions['showBytes'];
172  $this->mShowDimensions = $galleryOptions['showDimensions'];
173  $this->mShowFilename = true;
174  $this->mParser = false;
175  $this->mHideBadImages = false;
176  $this->mPerRow = $galleryOptions['imagesPerRow'];
177  $this->mWidths = $galleryOptions['imageWidth'];
178  $this->mHeights = $galleryOptions['imageHeight'];
179  $this->mCaptionLength = $galleryOptions['captionLength'];
180  $this->mMode = $mode;
181  }
182 
193  public function setParser( $parser ) {
194  $this->mParser = $parser;
195  }
196 
200  public function setHideBadImages( $flag = true ) {
201  $this->mHideBadImages = $flag;
202  }
203 
209  public function setCaption( $caption ) {
210  $this->mCaption = htmlspecialchars( $caption );
211  }
212 
218  public function setCaptionHtml( $caption ) {
219  $this->mCaption = $caption;
220  }
221 
228  public function setPerRow( $num ) {
229  if ( $num >= 0 ) {
230  $this->mPerRow = (int)$num;
231  }
232  }
233 
240  public function setWidths( $num ) {
241  $parsed = Parser::parseWidthParam( $num, false );
242  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
243  $this->mWidths = $parsed['width'];
244  }
245  }
246 
253  public function setHeights( $num ) {
254  $parsed = Parser::parseWidthParam( $num, false );
255  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
256  $this->mHeights = $parsed['width'];
257  }
258  }
259 
269  public function setAdditionalOptions( $options ) {
270  }
271 
283  public function add(
284  $title,
285  $html = '',
286  $alt = '',
287  $link = '',
288  $handlerOpts = [],
289  $loading = self::LOADING_DEFAULT
290  ) {
291  if ( $title instanceof File ) {
292  // Old calling convention
293  $title = $title->getTitle();
294  }
295  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading ];
296  wfDebug( 'ImageGallery::add ' . $title->getText() );
297  }
298 
310  public function insert(
311  $title,
312  $html = '',
313  $alt = '',
314  $link = '',
315  $handlerOpts = [],
316  $loading = self::LOADING_DEFAULT
317  ) {
318  if ( $title instanceof File ) {
319  // Old calling convention
320  $title = $title->getTitle();
321  }
322  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading ] );
323  }
324 
330  public function getImages() {
331  return $this->mImages;
332  }
333 
338  public function isEmpty() {
339  return empty( $this->mImages );
340  }
341 
348  public function setShowDimensions( $f ) {
349  $this->mShowDimensions = (bool)$f;
350  }
351 
358  public function setShowBytes( $f ) {
359  $this->mShowBytes = (bool)$f;
360  }
361 
368  public function setShowFilename( $f ) {
369  $this->mShowFilename = (bool)$f;
370  }
371 
381  public function setAttributes( $attribs ) {
382  $this->mAttribs = $attribs;
383  }
384 
390  abstract public function toHTML();
391 
395  public function count() {
396  return count( $this->mImages );
397  }
398 
404  public function setContextTitle( $title ) {
405  $this->contextTitle = $title;
406  }
407 
413  public function getContextTitle() {
414  return $this->contextTitle;
415  }
416 
421  protected function getRenderLang() {
422  return $this->mParser
423  ? $this->mParser->getTargetLanguage()
424  : $this->getLanguage();
425  }
426 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:37
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:70
ImageGalleryBase\setShowBytes
setShowBytes( $f)
Enable/Disable showing of the file size of an image in the gallery.
Definition: ImageGalleryBase.php:358
ImageGalleryBase\setParser
setParser( $parser)
Register a parser object.
Definition: ImageGalleryBase.php:193
ImageGalleryBase\$mShowDimensions
bool $mShowDimensions
Whether to show the dimensions in categories.
Definition: ImageGalleryBase.php:50
ImageGalleryBase\setHeights
setHeights( $num)
Set how high each image will be, in pixels.
Definition: ImageGalleryBase.php:253
ImageGalleryBase\$modeMapping
static array $modeMapping
Definition: ImageGalleryBase.php:105
ImageGalleryBase\setContextTitle
setContextTitle( $title)
Set the contextual title.
Definition: ImageGalleryBase.php:404
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:165
ImageGalleryBase\$mMode
string $mMode
Gallery mode.
Definition: ImageGalleryBase.php:60
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:32
ImageGalleryBase\setWidths
setWidths( $num)
Set how wide each image will be, in pixels.
Definition: ImageGalleryBase.php:240
ImageGalleryBase\setCaption
setCaption( $caption)
Set the caption (as plain text)
Definition: ImageGalleryBase.php:209
ImageGalleryBase\LOADING_LAZY
const LOADING_LAZY
Definition: ImageGalleryBase.php:34
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:413
ImageGalleryBase\setShowFilename
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
Definition: ImageGalleryBase.php:368
ImageGalleryBase\$mCaption
bool string $mCaption
Gallery caption.
Definition: ImageGalleryBase.php:65
ImageGalleryBase\$mHeights
int $mHeights
Definition: ImageGalleryBase.php:102
ImageGalleryBase\setAdditionalOptions
setAdditionalOptions( $options)
Allow setting additional options.
Definition: ImageGalleryBase.php:269
ImageGalleryBase\$mCaptionLength
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
Definition: ImageGalleryBase.php:74
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:143
ImageGalleryBase\$mPerRow
int $mPerRow
Definition: ImageGalleryBase.php:96
ImageGalleryBase\setAttributes
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
Definition: ImageGalleryBase.php:381
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:63
ImageGalleryBase\setHideBadImages
setHideBadImages( $flag=true)
Definition: ImageGalleryBase.php:200
ImageGalleryBase\setPerRow
setPerRow( $num)
Set how many images will be displayed per row.
Definition: ImageGalleryBase.php:228
MWException
MediaWiki exception.
Definition: MWException.php:29
ImageGalleryBase\$mShowFilename
bool $mShowFilename
Whether to show the filename.
Definition: ImageGalleryBase.php:55
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:31
$title
$title
Definition: testCompression.php:38
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:470
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:910
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:61
ImageGalleryBase\getImages
getImages()
Returns the list of images this gallery contains.
Definition: ImageGalleryBase.php:330
ImageGalleryBase\__construct
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
Definition: ImageGalleryBase.php:164
ImageGalleryBase\$mParser
Parser false $mParser
Registered parser object for output callbacks.
Definition: ImageGalleryBase.php:84
ImageGalleryBase\insert
insert( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT)
Add an image at the beginning of the gallery.
Definition: ImageGalleryBase.php:310
ImageGalleryBase\loadModes
static loadModes()
Definition: ImageGalleryBase.php:136
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:172
ImageGalleryBase\$mShowBytes
bool $mShowBytes
Whether to show the filesize in bytes in categories.
Definition: ImageGalleryBase.php:45
Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:84
ImageGalleryBase\setCaptionHtml
setCaptionHtml( $caption)
Set the caption (as HTML)
Definition: ImageGalleryBase.php:218
ImageGalleryBase\add
add( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT)
Add an image to the gallery.
Definition: ImageGalleryBase.php:283
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
ImageGalleryBase\$mImages
array[] $mImages
Gallery images -var array<int,array{0:Title,1:string,2:string,3:string,4:array,5:int}>
Definition: ImageGalleryBase.php:40
ImageGalleryBase\getRenderLang
getRenderLang()
Determines the correct language to be used for this image gallery.
Definition: ImageGalleryBase.php:421
Title
Represents a title within MediaWiki.
Definition: Title.php:41
ImageGalleryBase\isEmpty
isEmpty()
isEmpty() returns true if the gallery contains no images
Definition: ImageGalleryBase.php:338
ImageGalleryBase\$mWidths
int $mWidths
Definition: ImageGalleryBase.php:99
ImageGalleryBase\setShowDimensions
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
Definition: ImageGalleryBase.php:348
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:90
ImageGalleryBase\count
count()
Definition: ImageGalleryBase.php:395
ImageGalleryBase\factory
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
Definition: ImageGalleryBase.php:116
Parser\parseWidthParam
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition: Parser.php:6170
ImageGalleryBase\LOADING_DEFAULT
const LOADING_DEFAULT
Definition: ImageGalleryBase.php:33
ImageGalleryBase\toHTML
toHTML()
Display an html representation of the gallery.
ImageGalleryBase\$mHideBadImages
bool $mHideBadImages
Hide blacklisted images?
Definition: ImageGalleryBase.php:79
ImageGalleryBase\$mAttribs
array $mAttribs
Definition: ImageGalleryBase.php:93