MediaWiki  1.30.2
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 
64  protected $mHideBadImages;
65 
69  public $mParser;
70 
75  protected $contextTitle = false;
76 
78  protected $mAttribs = [];
79 
81  static private $modeMapping = false;
82 
92  static function factory( $mode = false, IContextSource $context = null ) {
95  if ( !$context ) {
97  }
98  if ( !$mode ) {
99  $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
100  $mode = $galleryOptions['mode'];
101  }
102 
103  $mode = $wgContLang->lc( $mode );
104 
105  if ( isset( self::$modeMapping[$mode] ) ) {
106  $class = self::$modeMapping[$mode];
107  return new $class( $mode, $context );
108  } else {
109  throw new MWException( "No gallery class registered for mode $mode" );
110  }
111  }
112 
113  private static function loadModes() {
114  if ( self::$modeMapping === false ) {
115  self::$modeMapping = [
116  'traditional' => 'TraditionalImageGallery',
117  'nolines' => 'NolinesImageGallery',
118  'packed' => 'PackedImageGallery',
119  'packed-hover' => 'PackedHoverImageGallery',
120  'packed-overlay' => 'PackedOverlayImageGallery',
121  'slideshow' => 'SlideshowImageGallery',
122  ];
123  // Allow extensions to make a new gallery format.
124  Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
125  }
126  }
127 
136  function __construct( $mode = 'traditional', IContextSource $context = null ) {
137  if ( $context ) {
138  $this->setContext( $context );
139  }
140 
141  $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
142  $this->mImages = [];
143  $this->mShowBytes = $galleryOptions['showBytes'];
144  $this->mShowDimensions = $galleryOptions['showDimensions'];
145  $this->mShowFilename = true;
146  $this->mParser = false;
147  $this->mHideBadImages = false;
148  $this->mPerRow = $galleryOptions['imagesPerRow'];
149  $this->mWidths = $galleryOptions['imageWidth'];
150  $this->mHeights = $galleryOptions['imageHeight'];
151  $this->mCaptionLength = $galleryOptions['captionLength'];
152  $this->mMode = $mode;
153  }
154 
165  function setParser( $parser ) {
166  $this->mParser = $parser;
167  }
168 
173  function setHideBadImages( $flag = true ) {
174  $this->mHideBadImages = $flag;
175  }
176 
182  function setCaption( $caption ) {
183  $this->mCaption = htmlspecialchars( $caption );
184  }
185 
191  public function setCaptionHtml( $caption ) {
192  $this->mCaption = $caption;
193  }
194 
201  public function setPerRow( $num ) {
202  if ( $num >= 0 ) {
203  $this->mPerRow = (int)$num;
204  }
205  }
206 
212  public function setWidths( $num ) {
213  if ( $num > 0 ) {
214  $this->mWidths = (int)$num;
215  }
216  }
217 
223  public function setHeights( $num ) {
224  if ( $num > 0 ) {
225  $this->mHeights = (int)$num;
226  }
227  }
228 
236  public function setAdditionalOptions( $options ) {
237  }
238 
249  function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
250  if ( $title instanceof File ) {
251  // Old calling convention
252  $title = $title->getTitle();
253  }
254  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
255  wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
256  }
257 
268  function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
269  if ( $title instanceof File ) {
270  // Old calling convention
271  $title = $title->getTitle();
272  }
273  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
274  }
275 
280  public function getImages() {
281  return $this->mImages;
282  }
283 
288  function isEmpty() {
289  return empty( $this->mImages );
290  }
291 
298  function setShowDimensions( $f ) {
299  $this->mShowDimensions = (bool)$f;
300  }
301 
308  function setShowBytes( $f ) {
309  $this->mShowBytes = (bool)$f;
310  }
311 
318  function setShowFilename( $f ) {
319  $this->mShowFilename = (bool)$f;
320  }
321 
331  function setAttributes( $attribs ) {
332  $this->mAttribs = $attribs;
333  }
334 
340  abstract public function toHTML();
341 
345  public function count() {
346  return count( $this->mImages );
347  }
348 
354  public function setContextTitle( $title ) {
355  $this->contextTitle = $title;
356  }
357 
363  public function getContextTitle() {
364  return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
365  ? $this->contextTitle
366  : false;
367  }
368 
373  protected function getRenderLang() {
374  return $this->mParser
375  ? $this->mParser->getTargetLanguage()
376  : $this->getLanguage();
377  }
378 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:34
ContextSource\getConfig
getConfig()
Get the Config object.
Definition: ContextSource.php:68
ImageGalleryBase\setShowBytes
setShowBytes( $f)
Enable/Disable showing of the file size of an image in the gallery.
Definition: ImageGalleryBase.php:308
ImageGalleryBase\setParser
setParser( $parser)
Register a parser object.
Definition: ImageGalleryBase.php:165
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:223
ImageGalleryBase\setContextTitle
setContextTitle( $title)
Set the contextual title.
Definition: ImageGalleryBase.php:354
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:212
ImageGalleryBase\setCaption
setCaption( $caption)
Set the caption (as plain text)
Definition: ImageGalleryBase.php:182
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:363
ImageGalleryBase\setShowFilename
setShowFilename( $f)
Enable/Disable showing of the filename of an image in the gallery.
Definition: ImageGalleryBase.php:318
ImageGalleryBase\insert
insert( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image at the beginning of the gallery.
Definition: ImageGalleryBase.php:268
ImageGalleryBase\$mCaption
bool string $mCaption
Gallery caption.
Definition: ImageGalleryBase.php:59
ImageGalleryBase\setAdditionalOptions
setAdditionalOptions( $options)
Allow setting additional options.
Definition: ImageGalleryBase.php:236
ImageGalleryBase\add
add( $title, $html='', $alt='', $link='', $handlerOpts=[])
Add an image to the gallery.
Definition: ImageGalleryBase.php:249
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()
Get the Language object.
Definition: ContextSource.php:143
ImageGalleryBase\setAttributes
setAttributes( $attribs)
Set arbitrary attributes to go on the HTML gallery output element.
Definition: ImageGalleryBase.php:331
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:173
ImageGalleryBase\setPerRow
setPerRow( $num)
Set how many images will be displayed per row.
Definition: ImageGalleryBase.php:201
$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:1965
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:932
ImageGalleryBase\$mShowFilename
bool $mShowFilename
Whether to show the filename.
Definition: ImageGalleryBase.php:49
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:30
$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:1965
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:2572
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
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:486
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:1047
ContextSource\setContext
setContext(IContextSource $context)
Set the IContextSource object.
Definition: ContextSource.php:58
ImageGalleryBase\$contextTitle
Title $contextTitle
Contextual title, used when images are being screened against the bad image list.
Definition: ImageGalleryBase.php:75
ImageGalleryBase\getImages
getImages()
Returns the list of images this gallery contains.
Definition: ImageGalleryBase.php:280
ImageGalleryBase\__construct
__construct( $mode='traditional', IContextSource $context=null)
Create a new image gallery object.
Definition: ImageGalleryBase.php:136
ImageGalleryBase\$mImages
array $mImages
Gallery images.
Definition: ImageGalleryBase.php:34
ImageGalleryBase\loadModes
static loadModes()
Definition: ImageGalleryBase.php:113
ImageGalleryBase\$modeMapping
static bool $modeMapping
Definition: ImageGalleryBase.php:81
ImageGalleryBase\$mShowBytes
bool $mShowBytes
Whether to show the filesize in bytes in categories.
Definition: ImageGalleryBase.php:39
ImageGalleryBase\setCaptionHtml
setCaptionHtml( $caption)
Set the caption (as HTML)
Definition: ImageGalleryBase.php:191
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
ImageGalleryBase\getRenderLang
getRenderLang()
Determines the correct language to be used for this image gallery.
Definition: ImageGalleryBase.php:373
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:288
$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:1965
ImageGalleryBase\setShowDimensions
setShowDimensions( $f)
Enable/Disable showing of the dimensions of an image in the gallery.
Definition: ImageGalleryBase.php:298
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:2981
ImageGalleryBase\count
count()
Definition: ImageGalleryBase.php:345
ImageGalleryBase\factory
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
Definition: ImageGalleryBase.php:92
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:64
ImageGalleryBase\$mParser
Parser $mParser
Registered parser object for output callbacks.
Definition: ImageGalleryBase.php:69
array
the array() calling protocol came about after MediaWiki 1.4rc1.
ImageGalleryBase\$mAttribs
array $mAttribs
Definition: ImageGalleryBase.php:78
$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:56