5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
28 * Add images to the gallery using add(), then render that list to HTML using toHTML().
32 abstract class ImageGalleryBase
extends ContextSource
{
34 * @var array Gallery images
39 * @var bool Whether to show the filesize in bytes in categories
41 protected $mShowBytes;
44 * @var bool Whether to show the dimensions in categories
46 protected $mShowDimensions;
49 * @var bool Whether to show the filename. Default: true
51 protected $mShowFilename;
54 * @var string Gallery mode. Default: traditional
59 * @var bool|string Gallery caption. Default: false
61 protected $mCaption = false;
64 * Length to truncate filename to in caption when using "showfilename".
65 * A value of 'true' will truncate the filename to one line using CSS
66 * and will be the behaviour after deprecation.
70 protected $mCaptionLength = true;
73 * @var bool Hide blacklisted images?
75 protected $mHideBadImages;
78 * @var Parser|false Registered parser object for output callbacks
83 * @var Title|null Contextual title, used when images are being screened against
86 protected $contextTitle = null;
89 protected $mAttribs = [];
101 private static $modeMapping;
104 * Get a new image gallery. This is the method other callers
105 * should use to get a gallery.
107 * @param string|bool $mode Mode to use. False to use the default
108 * @param IContextSource|null $context
109 * @return ImageGalleryBase
110 * @throws MWException
112 static function factory( $mode = false, IContextSource
$context = null ) {
115 $context = RequestContext
::getMainAndWarn( __METHOD__
);
118 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
119 $mode = $galleryOptions['mode'];
122 $mode = MediaWikiServices
::getInstance()->getContentLanguage()->lc( $mode );
124 if ( isset( self
::$modeMapping[$mode] ) ) {
125 $class = self
::$modeMapping[$mode];
126 return new $class( $mode, $context );
128 throw new MWException( "No gallery class registered for mode $mode" );
132 private static function loadModes() {
133 if ( self
::$modeMapping === null ) {
134 self
::$modeMapping = [
135 'traditional' => TraditionalImageGallery
::class,
136 'nolines' => NolinesImageGallery
::class,
137 'packed' => PackedImageGallery
::class,
138 'packed-hover' => PackedHoverImageGallery
::class,
139 'packed-overlay' => PackedOverlayImageGallery
::class,
140 'slideshow' => SlideshowImageGallery
::class,
142 // Allow extensions to make a new gallery format.
143 Hooks
::run( 'GalleryGetModes', [ &self
::$modeMapping ] );
148 * Create a new image gallery object.
150 * You should not call this directly, but instead use
151 * ImageGalleryBase::factory().
152 * @param string $mode
153 * @param IContextSource|null $context
155 function __construct( $mode = 'traditional', IContextSource
$context = null ) {
157 $this->setContext( $context );
160 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
162 $this->mShowBytes
= $galleryOptions['showBytes'];
163 $this->mShowDimensions
= $galleryOptions['showDimensions'];
164 $this->mShowFilename
= true;
165 $this->mParser
= false;
166 $this->mHideBadImages
= false;
167 $this->mPerRow
= $galleryOptions['imagesPerRow'];
168 $this->mWidths
= $galleryOptions['imageWidth'];
169 $this->mHeights
= $galleryOptions['imageHeight'];
170 $this->mCaptionLength
= $galleryOptions['captionLength'];
171 $this->mMode
= $mode;
175 * Register a parser object. If you do not set this
176 * and the output of this gallery ends up in parser
177 * cache, the javascript will break!
179 * @note This also triggers using the page's target
180 * language instead of the user language.
182 * @param Parser $parser
184 function setParser( $parser ) {
185 $this->mParser
= $parser;
192 function setHideBadImages( $flag = true ) {
193 $this->mHideBadImages
= $flag;
197 * Set the caption (as plain text)
199 * @param string $caption
201 function setCaption( $caption ) {
202 $this->mCaption
= htmlspecialchars( $caption );
206 * Set the caption (as HTML)
208 * @param string $caption
210 public function setCaptionHtml( $caption ) {
211 $this->mCaption
= $caption;
215 * Set how many images will be displayed per row.
217 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
218 * to screensize invalid numbers will be rejected
220 public function setPerRow( $num ) {
222 $this->mPerRow
= (int)$num;
227 * Set how wide each image will be, in pixels.
229 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
230 * and those below 0 are ignored.
232 public function setWidths( $num ) {
233 $parsed = Parser
::parseWidthParam( $num, false );
234 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
235 $this->mWidths
= $parsed['width'];
240 * Set how high each image will be, in pixels.
242 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
243 * and those below 0 are ignored.
245 public function setHeights( $num ) {
246 $parsed = Parser
::parseWidthParam( $num, false );
247 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
248 $this->mHeights
= $parsed['width'];
253 * Allow setting additional options. This is meant
254 * to allow extensions to add additional parameters to
255 * <gallery> parser tag.
257 * @param array $options Attributes of gallery tag
259 public function setAdditionalOptions( $options ) {
263 * Add an image to the gallery.
265 * @param Title $title Title object of the image that is added to the gallery
266 * @param string $html Additional HTML text to be shown. The name and size
267 * of the image are always shown.
268 * @param string $alt Alt text for the image
269 * @param string $link Override image link (optional)
270 * @param array $handlerOpts Array of options for image handler (aka page number)
272 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
273 if ( $title instanceof File
) {
274 // Old calling convention
275 $title = $title->getTitle();
277 $this->mImages
[] = [ $title, $html, $alt, $link, $handlerOpts ];
278 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
282 * Add an image at the beginning of the gallery.
284 * @param Title $title Title object of the image that is added to the gallery
285 * @param string $html Additional HTML text to be shown. The name and size
286 * of the image are always shown.
287 * @param string $alt Alt text for the image
288 * @param string $link Override image link (optional)
289 * @param array $handlerOpts Array of options for image handler (aka page number)
291 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
292 if ( $title instanceof File
) {
293 // Old calling convention
294 $title = $title->getTitle();
296 array_unshift( $this->mImages
, [ &$title, $html, $alt, $link, $handlerOpts ] );
300 * Returns the list of images this gallery contains
303 public function getImages() {
304 return $this->mImages
;
308 * isEmpty() returns true if the gallery contains no images
312 return empty( $this->mImages
);
316 * Enable/Disable showing of the dimensions of an image in the gallery.
317 * Enabled by default.
319 * @param bool $f Set to false to disable
321 function setShowDimensions( $f ) {
322 $this->mShowDimensions
= (bool)$f;
326 * Enable/Disable showing of the file size of an image in the gallery.
327 * Enabled by default.
329 * @param bool $f Set to false to disable
331 function setShowBytes( $f ) {
332 $this->mShowBytes
= (bool)$f;
336 * Enable/Disable showing of the filename of an image in the gallery.
337 * Enabled by default.
339 * @param bool $f Set to false to disable
341 function setShowFilename( $f ) {
342 $this->mShowFilename
= (bool)$f;
346 * Set arbitrary attributes to go on the HTML gallery output element.
347 * Should be suitable for a <ul> element.
349 * Note -- if taking from user input, you should probably run through
350 * Sanitizer::validateAttributes() first.
352 * @param array $attribs Array of HTML attribute pairs
354 function setAttributes( $attribs ) {
355 $this->mAttribs
= $attribs;
359 * Display an html representation of the gallery
361 * @return string The html
363 abstract public function toHTML();
366 * @return int Number of images in the gallery
368 public function count() {
369 return count( $this->mImages
);
373 * Set the contextual title
375 * @param Title|null $title Contextual title
377 public function setContextTitle( $title ) {
378 $this->contextTitle
= $title;
382 * Get the contextual title, if applicable
386 public function getContextTitle() {
387 return $this->contextTitle
;
391 * Determines the correct language to be used for this image gallery
394 protected function getRenderLang() {
395 return $this->mParser
396 ?
$this->mParser
->getTargetLanguage()
397 : $this->getLanguage();