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 
159  public function __construct( $mode = 'traditional', IContextSource $context = null ) {
160  if ( $context ) {
161  $this->setContext( $context );
162  }
163 
164  $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
165  $this->mImages = [];
166  $this->mShowBytes = $galleryOptions['showBytes'];
167  $this->mShowDimensions = $galleryOptions['showDimensions'];
168  $this->mShowFilename = true;
169  $this->mParser = false;
170  $this->mHideBadImages = false;
171  $this->mPerRow = $galleryOptions['imagesPerRow'];
172  $this->mWidths = $galleryOptions['imageWidth'];
173  $this->mHeights = $galleryOptions['imageHeight'];
174  $this->mCaptionLength = $galleryOptions['captionLength'];
175  $this->mMode = $mode;
176  }
177 
188  public function setParser( $parser ) {
189  $this->mParser = $parser;
190  }
191 
196  public function setHideBadImages( $flag = true ) {
197  $this->mHideBadImages = $flag;
198  }
199 
205  public function setCaption( $caption ) {
206  $this->mCaption = htmlspecialchars( $caption );
207  }
208 
214  public function setCaptionHtml( $caption ) {
215  $this->mCaption = $caption;
216  }
217 
224  public function setPerRow( $num ) {
225  if ( $num >= 0 ) {
226  $this->mPerRow = (int)$num;
227  }
228  }
229 
236  public function setWidths( $num ) {
237  $parsed = Parser::parseWidthParam( $num, false );
238  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
239  $this->mWidths = $parsed['width'];
240  }
241  }
242 
249  public function setHeights( $num ) {
250  $parsed = Parser::parseWidthParam( $num, false );
251  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
252  $this->mHeights = $parsed['width'];
253  }
254  }
255 
263  public function setAdditionalOptions( $options ) {
264  }
265 
277  public function add(
278  $title,
279  $html = '',
280  $alt = '',
281  $link = '',
282  $handlerOpts = [],
283  $loading = self::LOADING_DEFAULT
284  ) {
285  if ( $title instanceof File ) {
286  // Old calling convention
287  $title = $title->getTitle();
288  }
289  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading ];
290  wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
291  }
292 
304  public function insert(
305  $title,
306  $html = '',
307  $alt = '',
308  $link = '',
309  $handlerOpts = [],
310  $loading = self::LOADING_DEFAULT
311  ) {
312  if ( $title instanceof File ) {
313  // Old calling convention
314  $title = $title->getTitle();
315  }
316  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading ] );
317  }
318 
324  public function getImages() {
325  return $this->mImages;
326  }
327 
332  public function isEmpty() {
333  return empty( $this->mImages );
334  }
335 
342  public function setShowDimensions( $f ) {
343  $this->mShowDimensions = (bool)$f;
344  }
345 
352  public function setShowBytes( $f ) {
353  $this->mShowBytes = (bool)$f;
354  }
355 
362  public function setShowFilename( $f ) {
363  $this->mShowFilename = (bool)$f;
364  }
365 
375  public function setAttributes( $attribs ) {
376  $this->mAttribs = $attribs;
377  }
378 
384  abstract public function toHTML();
385 
389  public function count() {
390  return count( $this->mImages );
391  }
392 
398  public function setContextTitle( $title ) {
399  $this->contextTitle = $title;
400  }
401 
407  public function getContextTitle() {
408  return $this->contextTitle;
409  }
410 
415  protected function getRenderLang() {
416  return $this->mParser
417  ? $this->mParser->getTargetLanguage()
418  : $this->getLanguage();
419  }
420 }
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:352
ImageGalleryBase\setParser
setParser( $parser)
Register a parser object.
Definition: ImageGalleryBase.php:188
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:249
ImageGalleryBase\$modeMapping
static array $modeMapping
Definition: ImageGalleryBase.php:105
ImageGalleryBase\setContextTitle
setContextTitle( $title)
Set the contextual title.
Definition: ImageGalleryBase.php:398
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
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:236
ImageGalleryBase\setCaption
setCaption( $caption)
Set the caption (as plain text)
Definition: ImageGalleryBase.php:205
ImageGalleryBase\LOADING_LAZY
const LOADING_LAZY
Definition: ImageGalleryBase.php:34
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:407
ImageGalleryBase\setShowFilename
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
Definition: ImageGalleryBase.php:362
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:263
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:128
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:375
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:62
ImageGalleryBase\setHideBadImages
setHideBadImages( $flag=true)
Set bad image flag.
Definition: ImageGalleryBase.php:196
ImageGalleryBase\setPerRow
setPerRow( $num)
Set how many images will be displayed per row.
Definition: ImageGalleryBase.php:224
MWException
MediaWiki exception.
Definition: MWException.php:26
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:29
$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:467
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:324
ImageGalleryBase\__construct
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
Definition: ImageGalleryBase.php:159
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:304
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:171
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:81
ImageGalleryBase\setCaptionHtml
setCaptionHtml( $caption)
Set the caption (as HTML)
Definition: ImageGalleryBase.php:214
ImageGalleryBase\add
add( $title, $html='', $alt='', $link='', $handlerOpts=[], $loading=self::LOADING_DEFAULT)
Add an image to the gallery.
Definition: ImageGalleryBase.php:277
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
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:415
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:332
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:342
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:389
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:6144
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