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 
201  public function setHideBadImages( $flag = true ) {
202  $this->mHideBadImages = $flag;
203  }
204 
210  public function setCaption( $caption ) {
211  $this->mCaption = htmlspecialchars( $caption );
212  }
213 
219  public function setCaptionHtml( $caption ) {
220  $this->mCaption = $caption;
221  }
222 
229  public function setPerRow( $num ) {
230  if ( $num >= 0 ) {
231  $this->mPerRow = (int)$num;
232  }
233  }
234 
241  public function setWidths( $num ) {
242  $parsed = Parser::parseWidthParam( $num, false );
243  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
244  $this->mWidths = $parsed['width'];
245  }
246  }
247 
254  public function setHeights( $num ) {
255  $parsed = Parser::parseWidthParam( $num, false );
256  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
257  $this->mHeights = $parsed['width'];
258  }
259  }
260 
270  public function setAdditionalOptions( $options ) {
271  }
272 
284  public function add(
285  $title,
286  $html = '',
287  $alt = '',
288  $link = '',
289  $handlerOpts = [],
290  $loading = self::LOADING_DEFAULT
291  ) {
292  if ( $title instanceof File ) {
293  // Old calling convention
294  $title = $title->getTitle();
295  }
296  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading ];
297  wfDebug( 'ImageGallery::add ' . $title->getText() );
298  }
299 
311  public function insert(
312  $title,
313  $html = '',
314  $alt = '',
315  $link = '',
316  $handlerOpts = [],
317  $loading = self::LOADING_DEFAULT
318  ) {
319  if ( $title instanceof File ) {
320  // Old calling convention
321  $title = $title->getTitle();
322  }
323  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading ] );
324  }
325 
331  public function getImages() {
332  return $this->mImages;
333  }
334 
339  public function isEmpty() {
340  return empty( $this->mImages );
341  }
342 
349  public function setShowDimensions( $f ) {
350  $this->mShowDimensions = (bool)$f;
351  }
352 
359  public function setShowBytes( $f ) {
360  $this->mShowBytes = (bool)$f;
361  }
362 
369  public function setShowFilename( $f ) {
370  $this->mShowFilename = (bool)$f;
371  }
372 
382  public function setAttributes( $attribs ) {
383  $this->mAttribs = $attribs;
384  }
385 
391  abstract public function toHTML();
392 
396  public function count() {
397  return count( $this->mImages );
398  }
399 
405  public function setContextTitle( $title ) {
406  $this->contextTitle = $title;
407  }
408 
414  public function getContextTitle() {
415  return $this->contextTitle;
416  }
417 
422  protected function getRenderLang() {
423  return $this->mParser
424  ? $this->mParser->getTargetLanguage()
425  : $this->getLanguage();
426  }
427 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:34
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:67
ImageGalleryBase\setShowBytes
setShowBytes( $f)
Enable/Disable showing of the file size of an image in the gallery.
Definition: ImageGalleryBase.php:359
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:254
ImageGalleryBase\$modeMapping
static array $modeMapping
Definition: ImageGalleryBase.php:105
ImageGalleryBase\setContextTitle
setContextTitle( $title)
Set the contextual title.
Definition: ImageGalleryBase.php:405
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
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:241
ImageGalleryBase\setCaption
setCaption( $caption)
Set the caption (as plain text)
Definition: ImageGalleryBase.php:210
ImageGalleryBase\LOADING_LAZY
const LOADING_LAZY
Definition: ImageGalleryBase.php:34
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:414
ImageGalleryBase\setShowFilename
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
Definition: ImageGalleryBase.php:369
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:270
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:140
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:382
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)
Set bad image flag.
Definition: ImageGalleryBase.php:201
ImageGalleryBase\setPerRow
setPerRow( $num)
Set how many images will be displayed per row.
Definition: ImageGalleryBase.php:229
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:30
$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:469
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:909
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:58
ImageGalleryBase\getImages
getImages()
Returns the list of images this gallery contains.
Definition: ImageGalleryBase.php:331
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:311
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:219
ImageGalleryBase\add
add( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT)
Add an image to the gallery.
Definition: ImageGalleryBase.php:284
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:422
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:339
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:349
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:396
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:6206
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