MediaWiki  REL1_31
ImageGalleryBase.php
Go to the documentation of this file.
1 <?php
30 abstract class ImageGalleryBase extends ContextSource {
34  protected $mImages;
35 
39  protected $mShowBytes;
40 
44  protected $mShowDimensions;
45 
49  protected $mShowFilename;
50 
54  protected $mMode;
55 
59  protected $mCaption = false;
60 
68  protected $mCaptionLength = true;
69 
73  protected $mHideBadImages;
74 
78  public $mParser;
79 
84  protected $contextTitle = false;
85 
87  protected $mAttribs = [];
88 
90  static private $modeMapping = false;
91 
101  static function factory( $mode = false, IContextSource $context = null ) {
103  self::loadModes();
104  if ( !$context ) {
106  }
107  if ( !$mode ) {
108  $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
109  $mode = $galleryOptions['mode'];
110  }
111 
112  $mode = $wgContLang->lc( $mode );
113 
114  if ( isset( self::$modeMapping[$mode] ) ) {
115  $class = self::$modeMapping[$mode];
116  return new $class( $mode, $context );
117  } else {
118  throw new MWException( "No gallery class registered for mode $mode" );
119  }
120  }
121 
122  private static function loadModes() {
123  if ( self::$modeMapping === false ) {
124  self::$modeMapping = [
125  'traditional' => TraditionalImageGallery::class,
126  'nolines' => NolinesImageGallery::class,
127  'packed' => PackedImageGallery::class,
128  'packed-hover' => PackedHoverImageGallery::class,
129  'packed-overlay' => PackedOverlayImageGallery::class,
130  'slideshow' => SlideshowImageGallery::class,
131  ];
132  // Allow extensions to make a new gallery format.
133  Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
134  }
135  }
136 
145  function __construct( $mode = 'traditional', IContextSource $context = null ) {
146  if ( $context ) {
147  $this->setContext( $context );
148  }
149 
150  $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
151  $this->mImages = [];
152  $this->mShowBytes = $galleryOptions['showBytes'];
153  $this->mShowDimensions = $galleryOptions['showDimensions'];
154  $this->mShowFilename = true;
155  $this->mParser = false;
156  $this->mHideBadImages = false;
157  $this->mPerRow = $galleryOptions['imagesPerRow'];
158  $this->mWidths = $galleryOptions['imageWidth'];
159  $this->mHeights = $galleryOptions['imageHeight'];
160  $this->mCaptionLength = $galleryOptions['captionLength'];
161  $this->mMode = $mode;
162  }
163 
174  function setParser( $parser ) {
175  $this->mParser = $parser;
176  }
177 
182  function setHideBadImages( $flag = true ) {
183  $this->mHideBadImages = $flag;
184  }
185 
191  function setCaption( $caption ) {
192  $this->mCaption = htmlspecialchars( $caption );
193  }
194 
200  public function setCaptionHtml( $caption ) {
201  $this->mCaption = $caption;
202  }
203 
210  public function setPerRow( $num ) {
211  if ( $num >= 0 ) {
212  $this->mPerRow = (int)$num;
213  }
214  }
215 
222  public function setWidths( $num ) {
223  $parsed = Parser::parseWidthParam( $num, false );
224  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
225  $this->mWidths = $parsed['width'];
226  }
227  }
228 
235  public function setHeights( $num ) {
236  $parsed = Parser::parseWidthParam( $num, false );
237  if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
238  $this->mHeights = $parsed['width'];
239  }
240  }
241 
249  public function setAdditionalOptions( $options ) {
250  }
251 
262  function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
263  if ( $title instanceof File ) {
264  // Old calling convention
265  $title = $title->getTitle();
266  }
267  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
268  wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
269  }
270 
281  function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
282  if ( $title instanceof File ) {
283  // Old calling convention
284  $title = $title->getTitle();
285  }
286  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
287  }
288 
293  public function getImages() {
294  return $this->mImages;
295  }
296 
301  function isEmpty() {
302  return empty( $this->mImages );
303  }
304 
311  function setShowDimensions( $f ) {
312  $this->mShowDimensions = (bool)$f;
313  }
314 
321  function setShowBytes( $f ) {
322  $this->mShowBytes = (bool)$f;
323  }
324 
331  function setShowFilename( $f ) {
332  $this->mShowFilename = (bool)$f;
333  }
334 
344  function setAttributes( $attribs ) {
345  $this->mAttribs = $attribs;
346  }
347 
353  abstract public function toHTML();
354 
358  public function count() {
359  return count( $this->mImages );
360  }
361 
367  public function setContextTitle( $title ) {
368  $this->contextTitle = $title;
369  }
370 
376  public function getContextTitle() {
377  return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
378  ? $this->contextTitle
379  : false;
380  }
381 
386  protected function getRenderLang() {
387  return $this->mParser
388  ? $this->mParser->getTargetLanguage()
389  : $this->getLanguage();
390  }
391 }
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:321
ImageGalleryBase\setParser
setParser( $parser)
Register a parser object.
Definition: ImageGalleryBase.php:174
$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:2013
ImageGalleryBase\$mShowDimensions
bool $mShowDimensions
Whether to show the dimensions in categories.
Definition: ImageGalleryBase.php:44
ImageGalleryBase\setHeights
setHeights( $num)
Set how high each image will be, in pixels.
Definition: ImageGalleryBase.php:235
array
the array() calling protocol came about after MediaWiki 1.4rc1.
ImageGalleryBase\setContextTitle
setContextTitle( $title)
Set the contextual title.
Definition: ImageGalleryBase.php:367
ImageGalleryBase\$mMode
string $mMode
Gallery mode.
Definition: ImageGalleryBase.php:54
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:30
ImageGalleryBase\setWidths
setWidths( $num)
Set how wide each image will be, in pixels.
Definition: ImageGalleryBase.php:222
ImageGalleryBase\setCaption
setCaption( $caption)
Set the caption (as plain text)
Definition: ImageGalleryBase.php:191
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:376
ImageGalleryBase\setShowFilename
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
Definition: ImageGalleryBase.php:331
ImageGalleryBase\insert
insert( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image at the beginning of the gallery.
Definition: ImageGalleryBase.php:281
ImageGalleryBase\$mCaption
bool string $mCaption
Gallery caption.
Definition: ImageGalleryBase.php:59
ImageGalleryBase\setAdditionalOptions
setAdditionalOptions( $options)
Allow setting additional options.
Definition: ImageGalleryBase.php:249
ImageGalleryBase\$mCaptionLength
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
Definition: ImageGalleryBase.php:68
ImageGalleryBase\add
add( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image to the gallery.
Definition: ImageGalleryBase.php:262
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:37
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:344
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:182
ImageGalleryBase\setPerRow
setPerRow( $num)
Set how many images will be displayed per row.
Definition: ImageGalleryBase.php:210
MWException
MediaWiki exception.
Definition: MWException.php:26
ImageGalleryBase\$mShowFilename
bool $mShowFilename
Whether to show the filename.
Definition: ImageGalleryBase.php:49
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:2603
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:29
$link
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3021
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
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
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:994
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:84
ImageGalleryBase\getImages
getImages()
Returns the list of images this gallery contains.
Definition: ImageGalleryBase.php:293
ImageGalleryBase\__construct
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
Definition: ImageGalleryBase.php:145
$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:2001
$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:2014
ImageGalleryBase\$mImages
array $mImages
Gallery images.
Definition: ImageGalleryBase.php:34
ImageGalleryBase\loadModes
static loadModes()
Definition: ImageGalleryBase.php:122
ImageGalleryBase\$modeMapping
static bool $modeMapping
Definition: ImageGalleryBase.php:90
ImageGalleryBase\$mShowBytes
bool $mShowBytes
Whether to show the filesize in bytes in categories.
Definition: ImageGalleryBase.php:39
Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:70
ImageGalleryBase\setCaptionHtml
setCaptionHtml( $caption)
Set the caption (as HTML)
Definition: ImageGalleryBase.php:200
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:386
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:301
ImageGalleryBase\setShowDimensions
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
Definition: ImageGalleryBase.php:311
IContextSource\getConfig
getConfig()
Get the site configuration.
ImageGalleryBase\count
count()
Definition: ImageGalleryBase.php:358
ImageGalleryBase\factory
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
Definition: ImageGalleryBase.php:101
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:56
Parser\parseWidthParam
static parseWidthParam( $value, $parseHeight=true)
Parsed a width param of imagelink like 300px or 200x300px.
Definition: Parser.php:6084
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
ImageGalleryBase\toHTML
toHTML()
Display an html representation of the gallery.
ImageGalleryBase\$mHideBadImages
bool $mHideBadImages
Hide blacklisted images?
Definition: ImageGalleryBase.php:73
ImageGalleryBase\$mParser
Parser $mParser
Registered parser object for output callbacks.
Definition: ImageGalleryBase.php:78
ImageGalleryBase\$mAttribs
array $mAttribs
Definition: ImageGalleryBase.php:87
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:57