MediaWiki  1.32.5
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  static private $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 }
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:322
ImageGalleryBase\setParser
setParser( $parser)
Register a parser object.
Definition: ImageGalleryBase.php:175
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:236
ImageGalleryBase\setContextTitle
setContextTitle( $title)
Set the contextual title.
Definition: ImageGalleryBase.php:368
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:223
ImageGalleryBase\setCaption
setCaption( $caption)
Set the caption (as plain text)
Definition: ImageGalleryBase.php:192
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:377
ImageGalleryBase\setShowFilename
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
Definition: ImageGalleryBase.php:332
ImageGalleryBase\insert
insert( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image at the beginning of the gallery.
Definition: ImageGalleryBase.php:282
ImageGalleryBase\$mCaption
bool string $mCaption
Gallery caption.
Definition: ImageGalleryBase.php:61
ImageGalleryBase\setAdditionalOptions
setAdditionalOptions( $options)
Allow setting additional options.
Definition: ImageGalleryBase.php:250
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:263
php
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
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
ImageGalleryBase\setAttributes
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
Definition: ImageGalleryBase.php:345
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:51
ImageGalleryBase\setHideBadImages
setHideBadImages( $flag=true)
Set bad image flag.
Definition: ImageGalleryBase.php:183
ImageGalleryBase\setPerRow
setPerRow( $num)
Set how many images will be displayed per row.
Definition: ImageGalleryBase.php:211
$html
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:2044
MWException
MediaWiki exception.
Definition: MWException.php:26
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
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
$attribs
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:2044
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$parser
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition: hooks.txt:1841
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:450
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
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:988
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:55
ImageGalleryBase\$contextTitle
Title $contextTitle
Contextual title, used when images are being screened against the bad image list.
Definition: ImageGalleryBase.php:86
ImageGalleryBase\getImages
getImages()
Returns the list of images this gallery contains.
Definition: ImageGalleryBase.php:294
ImageGalleryBase\__construct
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
Definition: ImageGalleryBase.php:146
ImageGalleryBase\$mImages
array $mImages
Gallery images.
Definition: ImageGalleryBase.php:36
ImageGalleryBase\loadModes
static loadModes()
Definition: ImageGalleryBase.php:123
ImageGalleryBase\$modeMapping
static bool $modeMapping
Definition: ImageGalleryBase.php:92
ImageGalleryBase\$mShowBytes
bool $mShowBytes
Whether to show the filesize in bytes in categories.
Definition: ImageGalleryBase.php:41
ImageGalleryBase\setCaptionHtml
setCaptionHtml( $caption)
Set the caption (as HTML)
Definition: ImageGalleryBase.php:201
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:387
Title
Represents a title within MediaWiki.
Definition: Title.php:39
ImageGalleryBase\isEmpty
isEmpty()
isEmpty() returns true if the gallery contains no images
Definition: ImageGalleryBase.php:302
$options
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:2044
ImageGalleryBase\setShowDimensions
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
Definition: ImageGalleryBase.php:312
IContextSource\getConfig
getConfig()
Get the site configuration.
$link
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3098
ImageGalleryBase\count
count()
Definition: ImageGalleryBase.php:359
ImageGalleryBase\factory
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
Definition: ImageGalleryBase.php:103
class
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
MediaWikiServices
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
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\$mParser
Parser $mParser
Registered parser object for output callbacks.
Definition: ImageGalleryBase.php:80
ImageGalleryBase\$mAttribs
array $mAttribs
Definition: ImageGalleryBase.php:89