MediaWiki  1.34.4
ImageGalleryBase.php
Go to the documentation of this file.
1 <?php
24 
32 abstract class ImageGalleryBase extends ContextSource {
36  protected $mImages;
37 
41  protected $mShowBytes;
42 
46  protected $mShowDimensions;
47 
51  protected $mShowFilename;
52 
56  protected $mMode;
57 
61  protected $mCaption = false;
62 
70  protected $mCaptionLength = true;
71 
75  protected $mHideBadImages;
76 
80  public $mParser;
81 
86  protected $contextTitle = null;
87 
89  protected $mAttribs = [];
90 
92  protected $mPerRow;
93 
95  protected $mWidths;
96 
98  protected $mHeights;
99 
101  private static $modeMapping;
102 
112  static function factory( $mode = false, IContextSource $context = null ) {
113  self::loadModes();
114  if ( !$context ) {
116  }
117  if ( !$mode ) {
118  $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
119  $mode = $galleryOptions['mode'];
120  }
121 
122  $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
123 
124  if ( isset( self::$modeMapping[$mode] ) ) {
125  $class = self::$modeMapping[$mode];
126  return new $class( $mode, $context );
127  } else {
128  throw new MWException( "No gallery class registered for mode $mode" );
129  }
130  }
131 
132  private static function loadModes() {
133  if ( self::$modeMapping === null ) {
134  self::$modeMapping = [
135  'traditional' => TraditionalImageGallery::class,
136  'nolines' => NolinesImageGallery::class,
137  'packed' => PackedImageGallery::class,
138  'packed-hover' => PackedHoverImageGallery::class,
139  'packed-overlay' => PackedOverlayImageGallery::class,
140  'slideshow' => SlideshowImageGallery::class,
141  ];
142  // Allow extensions to make a new gallery format.
143  Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
144  }
145  }
146 
155  function __construct( $mode = 'traditional', IContextSource $context = null ) {
156  if ( $context ) {
157  $this->setContext( $context );
158  }
159 
160  $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
161  $this->mImages = [];
162  $this->mShowBytes = $galleryOptions['showBytes'];
163  $this->mShowDimensions = $galleryOptions['showDimensions'];
164  $this->mShowFilename = true;
165  $this->mParser = false;
166  $this->mHideBadImages = false;
167  $this->mPerRow = $galleryOptions['imagesPerRow'];
168  $this->mWidths = $galleryOptions['imageWidth'];
169  $this->mHeights = $galleryOptions['imageHeight'];
170  $this->mCaptionLength = $galleryOptions['captionLength'];
171  $this->mMode = $mode;
172  }
173 
184  function setParser( $parser ) {
185  $this->mParser = $parser;
186  }
187 
192  function setHideBadImages( $flag = true ) {
193  $this->mHideBadImages = $flag;
194  }
195 
201  function setCaption( $caption ) {
202  $this->mCaption = htmlspecialchars( $caption );
203  }
204 
210  public function setCaptionHtml( $caption ) {
211  $this->mCaption = $caption;
212  }
213 
220  public function setPerRow( $num ) {
221  if ( $num >= 0 ) {
222  $this->mPerRow = (int)$num;
223  }
224  }
225 
232  public function setWidths( $num ) {
233  $parsed = Parser::parseWidthParam( $num, false );
234  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
235  $this->mWidths = $parsed['width'];
236  }
237  }
238 
245  public function setHeights( $num ) {
246  $parsed = Parser::parseWidthParam( $num, false );
247  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
248  $this->mHeights = $parsed['width'];
249  }
250  }
251 
259  public function setAdditionalOptions( $options ) {
260  }
261 
272  function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
273  if ( $title instanceof File ) {
274  // Old calling convention
275  $title = $title->getTitle();
276  }
277  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
278  wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
279  }
280 
291  function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
292  if ( $title instanceof File ) {
293  // Old calling convention
294  $title = $title->getTitle();
295  }
296  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
297  }
298 
303  public function getImages() {
304  return $this->mImages;
305  }
306 
311  function isEmpty() {
312  return empty( $this->mImages );
313  }
314 
321  function setShowDimensions( $f ) {
322  $this->mShowDimensions = (bool)$f;
323  }
324 
331  function setShowBytes( $f ) {
332  $this->mShowBytes = (bool)$f;
333  }
334 
341  function setShowFilename( $f ) {
342  $this->mShowFilename = (bool)$f;
343  }
344 
354  function setAttributes( $attribs ) {
355  $this->mAttribs = $attribs;
356  }
357 
363  abstract public function toHTML();
364 
368  public function count() {
369  return count( $this->mImages );
370  }
371 
377  public function setContextTitle( $title ) {
378  $this->contextTitle = $title;
379  }
380 
386  public function getContextTitle() {
387  return $this->contextTitle;
388  }
389 
394  protected function getRenderLang() {
395  return $this->mParser
396  ? $this->mParser->getTargetLanguage()
397  : $this->getLanguage();
398  }
399 }
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:331
ImageGalleryBase\setParser
setParser( $parser)
Register a parser object.
Definition: ImageGalleryBase.php:184
ImageGalleryBase\$mShowDimensions
bool $mShowDimensions
Whether to show the dimensions in categories.
Definition: ImageGalleryBase.php:46
ImageGalleryBase\setHeights
setHeights( $num)
Set how high each image will be, in pixels.
Definition: ImageGalleryBase.php:245
ImageGalleryBase\$modeMapping
static array $modeMapping
Definition: ImageGalleryBase.php:101
ImageGalleryBase\setContextTitle
setContextTitle( $title)
Set the contextual title.
Definition: ImageGalleryBase.php:377
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
ImageGalleryBase\$mMode
string $mMode
Gallery mode.
Definition: ImageGalleryBase.php:56
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:32
ImageGalleryBase\setWidths
setWidths( $num)
Set how wide each image will be, in pixels.
Definition: ImageGalleryBase.php:232
ImageGalleryBase\setCaption
setCaption( $caption)
Set the caption (as plain text)
Definition: ImageGalleryBase.php:201
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:386
ImageGalleryBase\setShowFilename
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
Definition: ImageGalleryBase.php:341
ImageGalleryBase\insert
insert( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image at the beginning of the gallery.
Definition: ImageGalleryBase.php:291
ImageGalleryBase\$mCaption
bool string $mCaption
Gallery caption.
Definition: ImageGalleryBase.php:61
ImageGalleryBase\$mHeights
int $mHeights
Definition: ImageGalleryBase.php:98
ImageGalleryBase\setAdditionalOptions
setAdditionalOptions( $options)
Allow setting additional options.
Definition: ImageGalleryBase.php:259
ImageGalleryBase\$mCaptionLength
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
Definition: ImageGalleryBase.php:70
ImageGalleryBase\add
add( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image to the gallery.
Definition: ImageGalleryBase.php:272
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
ImageGalleryBase\$mPerRow
int $mPerRow
Definition: ImageGalleryBase.php:92
ImageGalleryBase\setAttributes
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
Definition: ImageGalleryBase.php:354
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:192
ImageGalleryBase\setPerRow
setPerRow( $num)
Set how many images will be displayed per row.
Definition: ImageGalleryBase.php:220
MWException
MediaWiki exception.
Definition: MWException.php:26
ImageGalleryBase\$mShowFilename
bool $mShowFilename
Whether to show the filename.
Definition: ImageGalleryBase.php:51
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:34
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:447
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:303
ImageGalleryBase\__construct
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
Definition: ImageGalleryBase.php:155
ImageGalleryBase\$mImages
array $mImages
Gallery images.
Definition: ImageGalleryBase.php:36
ImageGalleryBase\$mParser
Parser false $mParser
Registered parser object for output callbacks.
Definition: ImageGalleryBase.php:80
ImageGalleryBase\loadModes
static loadModes()
Definition: ImageGalleryBase.php:132
ImageGalleryBase\$mShowBytes
bool $mShowBytes
Whether to show the filesize in bytes in categories.
Definition: ImageGalleryBase.php:41
Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:74
ImageGalleryBase\setCaptionHtml
setCaptionHtml( $caption)
Set the caption (as HTML)
Definition: ImageGalleryBase.php:210
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
ImageGalleryBase\getRenderLang
getRenderLang()
Determines the correct language to be used for this image gallery.
Definition: ImageGalleryBase.php:394
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:311
ImageGalleryBase\$mWidths
int $mWidths
Definition: ImageGalleryBase.php:95
ImageGalleryBase\setShowDimensions
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
Definition: ImageGalleryBase.php:321
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:86
ImageGalleryBase\count
count()
Definition: ImageGalleryBase.php:368
ImageGalleryBase\factory
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
Definition: ImageGalleryBase.php:112
Parser\parseWidthParam
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition: Parser.php:6740
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:75
ImageGalleryBase\$mAttribs
array $mAttribs
Definition: ImageGalleryBase.php:89