MediaWiki  1.27.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 $mShowFilename;
45 
49  protected $mMode;
50 
54  protected $mCaption = false;
55 
59  protected $mHideBadImages;
60 
64  public $mParser;
65 
70  protected $contextTitle = false;
71 
73  protected $mAttribs = [];
74 
76  static private $modeMapping = false;
77 
87  static function factory( $mode = false, IContextSource $context = null ) {
89  self::loadModes();
90  if ( !$context ) {
92  }
93  if ( !$mode ) {
94  $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
95  $mode = $galleryOptions['mode'];
96  }
97 
98  $mode = $wgContLang->lc( $mode );
99 
100  if ( isset( self::$modeMapping[$mode] ) ) {
101  $class = self::$modeMapping[$mode];
102  return new $class( $mode, $context );
103  } else {
104  throw new MWException( "No gallery class registered for mode $mode" );
105  }
106  }
107 
108  private static function loadModes() {
109  if ( self::$modeMapping === false ) {
110  self::$modeMapping = [
111  'traditional' => 'TraditionalImageGallery',
112  'nolines' => 'NolinesImageGallery',
113  'packed' => 'PackedImageGallery',
114  'packed-hover' => 'PackedHoverImageGallery',
115  'packed-overlay' => 'PackedOverlayImageGallery',
116  ];
117  // Allow extensions to make a new gallery format.
118  Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
119  }
120  }
121 
130  function __construct( $mode = 'traditional', IContextSource $context = null ) {
131  if ( $context ) {
132  $this->setContext( $context );
133  }
134 
135  $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
136  $this->mImages = [];
137  $this->mShowBytes = $galleryOptions['showBytes'];
138  $this->mShowFilename = true;
139  $this->mParser = false;
140  $this->mHideBadImages = false;
141  $this->mPerRow = $galleryOptions['imagesPerRow'];
142  $this->mWidths = $galleryOptions['imageWidth'];
143  $this->mHeights = $galleryOptions['imageHeight'];
144  $this->mCaptionLength = $galleryOptions['captionLength'];
145  $this->mMode = $mode;
146  }
147 
158  function setParser( $parser ) {
159  $this->mParser = $parser;
160  }
161 
166  function setHideBadImages( $flag = true ) {
167  $this->mHideBadImages = $flag;
168  }
169 
175  function setCaption( $caption ) {
176  $this->mCaption = htmlspecialchars( $caption );
177  }
178 
184  public function setCaptionHtml( $caption ) {
185  $this->mCaption = $caption;
186  }
187 
194  public function setPerRow( $num ) {
195  if ( $num >= 0 ) {
196  $this->mPerRow = (int)$num;
197  }
198  }
199 
205  public function setWidths( $num ) {
206  if ( $num > 0 ) {
207  $this->mWidths = (int)$num;
208  }
209  }
210 
216  public function setHeights( $num ) {
217  if ( $num > 0 ) {
218  $this->mHeights = (int)$num;
219  }
220  }
221 
229  public function setAdditionalOptions( $options ) {
230  }
231 
242  function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
243  if ( $title instanceof File ) {
244  // Old calling convention
245  $title = $title->getTitle();
246  }
247  $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
248  wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
249  }
250 
261  function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
262  if ( $title instanceof File ) {
263  // Old calling convention
264  $title = $title->getTitle();
265  }
266  array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
267  }
268 
273  public function getImages() {
274  return $this->mImages;
275  }
276 
281  function isEmpty() {
282  return empty( $this->mImages );
283  }
284 
291  function setShowBytes( $f ) {
292  $this->mShowBytes = (bool)$f;
293  }
294 
301  function setShowFilename( $f ) {
302  $this->mShowFilename = (bool)$f;
303  }
304 
314  function setAttributes( $attribs ) {
315  $this->mAttribs = $attribs;
316  }
317 
323  abstract public function toHTML();
324 
328  public function count() {
329  return count( $this->mImages );
330  }
331 
337  public function setContextTitle( $title ) {
338  $this->contextTitle = $title;
339  }
340 
346  public function getContextTitle() {
347  return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
348  ? $this->contextTitle
349  : false;
350  }
351 
356  protected function getRenderLang() {
357  return $this->mParser
358  ? $this->mParser->getTargetLanguage()
359  : $this->getLanguage();
360  }
361 }
setContext(IContextSource $context)
Set the IContextSource object.
__construct($mode= 'traditional', IContextSource $context=null)
Create a new image gallery 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 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:1798
Interface for objects which can provide a MediaWiki context on request.
array $mImages
Gallery images.
getLanguage()
Get the Language object.
setCaption($caption)
Set the caption (as plain text)
setAttributes($attribs)
Set arbitrary attributes to go on the HTML gallery output element.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
bool $mShowBytes
Whether to show the filesize in bytes in categories.
add($title, $html= '', $alt= '', $link= '', $handlerOpts=[])
Add an image to the gallery.
Represents a title within MediaWiki.
Definition: Title.php:34
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
IContextSource $context
magic word & $parser
Definition: hooks.txt:2321
static bool $modeMapping
setShowBytes($f)
Enable/Disable showing of the file size 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.
the value to return A Title object or null for latest to be modified or replaced by the hook handler or if authentication is not possible after cache objects are set for highlighting & $link
Definition: hooks.txt:2581
setCaptionHtml($caption)
Set the caption (as HTML)
static factory($mode=false, IContextSource $context=null)
Get a new image gallery.
setParser($parser)
Register a parser object.
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
setAdditionalOptions($options)
Allow setting additional options.
setPerRow($num)
Set how many images will be displayed per row.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1004
setShowFilename($f)
Enable/Disable showing of the filename of an image in the gallery.
getConfig()
Get the Config object.
setHideBadImages($flag=true)
Set bad image flag.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
toHTML()
Display an html representation of the gallery.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
setContextTitle($title)
Set the contextual title.
bool $mHideBadImages
Hide blacklisted images?
getContextTitle()
Get the contextual title, if applicable.
Image gallery.
string $mMode
Gallery mode.
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
setHeights($num)
Set how high each image will be, in pixels.
bool string $mCaption
Gallery caption.
bool $mShowFilename
Whether to show the filename.
static getMainAndWarn($func=__METHOD__)
Get the RequestContext object associated with the main request and gives a warning to the log...
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 local content language as $wgContLang
Definition: design.txt:56
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:50
getImages()
Returns the list of images this gallery contains.
Title $contextTitle
Contextual title, used when images are being screened against the bad image list. ...
setWidths($num)
Set how wide each image will be, in pixels.
insert($title, $html= '', $alt= '', $link= '', $handlerOpts=[])
Add an image at the beginning of the gallery.
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:1798