MediaWiki  master
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 = false;
87 
89  protected $mAttribs = [];
90 
92  private static $modeMapping = false;
93 
103  static function factory( $mode = false, IContextSource $context = null ) {
104  self::loadModes();
105  if ( !$context ) {
107  }
108  if ( !$mode ) {
109  $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
110  $mode = $galleryOptions['mode'];
111  }
112 
113  $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
114 
115  if ( isset( self::$modeMapping[$mode] ) ) {
116  $class = self::$modeMapping[$mode];
117  return new $class( $mode, $context );
118  } else {
119  throw new MWException( "No gallery class registered for mode $mode" );
120  }
121  }
122 
123  private static function loadModes() {
124  if ( self::$modeMapping === false ) {
125  self::$modeMapping = [
126  'traditional' => TraditionalImageGallery::class,
127  'nolines' => NolinesImageGallery::class,
128  'packed' => PackedImageGallery::class,
129  'packed-hover' => PackedHoverImageGallery::class,
130  'packed-overlay' => PackedOverlayImageGallery::class,
131  'slideshow' => SlideshowImageGallery::class,
132  ];
133  // Allow extensions to make a new gallery format.
134  Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
135  }
136  }
137 
146  function __construct( $mode = 'traditional', IContextSource $context = null ) {
147  if ( $context ) {
148  $this->setContext( $context );
149  }
150 
151  $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
152  $this->mImages = [];
153  $this->mShowBytes = $galleryOptions['showBytes'];
154  $this->mShowDimensions = $galleryOptions['showDimensions'];
155  $this->mShowFilename = true;
156  $this->mParser = false;
157  $this->mHideBadImages = false;
158  $this->mPerRow = $galleryOptions['imagesPerRow'];
159  $this->mWidths = $galleryOptions['imageWidth'];
160  $this->mHeights = $galleryOptions['imageHeight'];
161  $this->mCaptionLength = $galleryOptions['captionLength'];
162  $this->mMode = $mode;
163  }
164 
175  function setParser( $parser ) {
176  $this->mParser = $parser;
177  }
178 
183  function setHideBadImages( $flag = true ) {
184  $this->mHideBadImages = $flag;
185  }
186 
192  function setCaption( $caption ) {
193  $this->mCaption = htmlspecialchars( $caption );
194  }
195 
201  public function setCaptionHtml( $caption ) {
202  $this->mCaption = $caption;
203  }
204 
211  public function setPerRow( $num ) {
212  if ( $num >= 0 ) {
213  $this->mPerRow = (int)$num;
214  }
215  }
216 
223  public function setWidths( $num ) {
224  $parsed = Parser::parseWidthParam( $num, false );
225  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
226  $this->mWidths = $parsed['width'];
227  }
228  }
229 
236  public function setHeights( $num ) {
237  $parsed = Parser::parseWidthParam( $num, false );
238  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
239  $this->mHeights = $parsed['width'];
240  }
241  }
242 
250  public function setAdditionalOptions( $options ) {
251  }
252 
263  function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
264  if ( $title instanceof File ) {
265  // Old calling convention
266  $title = $title->getTitle();
267  }
268  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
269  wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
270  }
271 
282  function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
283  if ( $title instanceof File ) {
284  // Old calling convention
285  $title = $title->getTitle();
286  }
287  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
288  }
289 
294  public function getImages() {
295  return $this->mImages;
296  }
297 
302  function isEmpty() {
303  return empty( $this->mImages );
304  }
305 
312  function setShowDimensions( $f ) {
313  $this->mShowDimensions = (bool)$f;
314  }
315 
322  function setShowBytes( $f ) {
323  $this->mShowBytes = (bool)$f;
324  }
325 
332  function setShowFilename( $f ) {
333  $this->mShowFilename = (bool)$f;
334  }
335 
345  function setAttributes( $attribs ) {
346  $this->mAttribs = $attribs;
347  }
348 
354  abstract public function toHTML();
355 
359  public function count() {
360  return count( $this->mImages );
361  }
362 
368  public function setContextTitle( $title ) {
369  $this->contextTitle = $title;
370  }
371 
377  public function getContextTitle() {
378  return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
379  ? $this->contextTitle
380  : false;
381  }
382 
387  protected function getRenderLang() {
388  return $this->mParser
389  ? $this->mParser->getTargetLanguage()
390  : $this->getLanguage();
391  }
392 }
setContext(IContextSource $context)
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1982
array $mImages
Gallery images.
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setCaptionHtml( $caption)
Set the caption (as HTML)
bool $mShowBytes
Whether to show the filesize in bytes in categories.
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
IContextSource $context
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition: hooks.txt:1799
static bool $modeMapping
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3051
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition: Parser.php:6351
getRenderLang()
Determines the correct language to be used for this image gallery.
getConfig()
Get the site configuration.
Parser $mParser
Registered parser object for output callbacks.
isEmpty()
isEmpty() returns true if the gallery contains no images
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
setParser( $parser)
Register a parser object.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1982
setCaption( $caption)
Set the caption (as plain text)
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition: hooks.txt:1982
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
toHTML()
Display an html representation of the gallery.
setAdditionalOptions( $options)
Allow setting additional options.
setShowBytes( $f)
Enable/Disable showing of the file size of an image in the gallery.
bool $mHideBadImages
Hide blacklisted images?
setWidths( $num)
Set how wide each image will be, in pixels.
getContextTitle()
Get the contextual title, if applicable.
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
bool $mShowDimensions
Whether to show the dimensions in categories.
insert( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image at the beginning of the gallery.
Image gallery.
setContextTitle( $title)
Set the contextual title.
string $mMode
Gallery mode.
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
setHideBadImages( $flag=true)
Set bad image flag.
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
setHeights( $num)
Set how high each image will be, in pixels.
bool string $mCaption
Gallery caption.
bool $mShowFilename
Whether to show the filename.
$f
Definition: router.php:79
static getMainAndWarn( $func=__METHOD__)
Get the RequestContext object associated with the main request and gives a warning to the log...
add( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image to the gallery.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
getImages()
Returns the list of images this gallery contains.
Title $contextTitle
Contextual title, used when images are being screened against the bad image list. ...
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
setPerRow( $num)
Set how many images will be displayed per row.
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200