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 Registered parser object for output callbacks
83 * @var Title Contextual title, used when images are being screened against
86 protected $contextTitle = false;
89 protected $mAttribs = [];
92 private static $modeMapping = false;
95 * Get a new image gallery. This is the method other callers
96 * should use to get a gallery.
98 * @param string|bool $mode Mode to use. False to use the default
99 * @param IContextSource|null $context
100 * @return ImageGalleryBase
101 * @throws MWException
103 static function factory( $mode = false, IContextSource
$context = null ) {
106 $context = RequestContext
::getMainAndWarn( __METHOD__
);
109 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
110 $mode = $galleryOptions['mode'];
113 $mode = MediaWikiServices
::getInstance()->getContentLanguage()->lc( $mode );
115 if ( isset( self
::$modeMapping[$mode] ) ) {
116 $class = self
::$modeMapping[$mode];
117 return new $class( $mode, $context );
119 throw new MWException( "No gallery class registered for mode $mode" );
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,
133 // Allow extensions to make a new gallery format.
134 Hooks
::run( 'GalleryGetModes', [ &self
::$modeMapping ] );
139 * Create a new image gallery object.
141 * You should not call this directly, but instead use
142 * ImageGalleryBase::factory().
143 * @param string $mode
144 * @param IContextSource|null $context
146 function __construct( $mode = 'traditional', IContextSource
$context = null ) {
148 $this->setContext( $context );
151 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
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;
166 * Register a parser object. If you do not set this
167 * and the output of this gallery ends up in parser
168 * cache, the javascript will break!
170 * @note This also triggers using the page's target
171 * language instead of the user language.
173 * @param Parser $parser
175 function setParser( $parser ) {
176 $this->mParser
= $parser;
183 function setHideBadImages( $flag = true ) {
184 $this->mHideBadImages
= $flag;
188 * Set the caption (as plain text)
190 * @param string $caption
192 function setCaption( $caption ) {
193 $this->mCaption
= htmlspecialchars( $caption );
197 * Set the caption (as HTML)
199 * @param string $caption
201 public function setCaptionHtml( $caption ) {
202 $this->mCaption
= $caption;
206 * Set how many images will be displayed per row.
208 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
209 * to screensize invalid numbers will be rejected
211 public function setPerRow( $num ) {
213 $this->mPerRow
= (int)$num;
218 * Set how wide each image will be, in pixels.
220 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
221 * and those below 0 are ignored.
223 public function setWidths( $num ) {
224 $parsed = Parser
::parseWidthParam( $num, false );
225 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
226 $this->mWidths
= $parsed['width'];
231 * Set how high each image will be, in pixels.
233 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
234 * and those below 0 are ignored.
236 public function setHeights( $num ) {
237 $parsed = Parser
::parseWidthParam( $num, false );
238 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
239 $this->mHeights
= $parsed['width'];
244 * Allow setting additional options. This is meant
245 * to allow extensions to add additional parameters to
246 * <gallery> parser tag.
248 * @param array $options Attributes of gallery tag
250 public function setAdditionalOptions( $options ) {
254 * Add an image to the gallery.
256 * @param Title $title Title object of the image that is added to the gallery
257 * @param string $html Additional HTML text to be shown. The name and size
258 * of the image are always shown.
259 * @param string $alt Alt text for the image
260 * @param string $link Override image link (optional)
261 * @param array $handlerOpts Array of options for image handler (aka page number)
263 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
264 if ( $title instanceof File
) {
265 // Old calling convention
266 $title = $title->getTitle();
268 $this->mImages
[] = [ $title, $html, $alt, $link, $handlerOpts ];
269 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
273 * Add an image at the beginning of the gallery.
275 * @param Title $title Title object of the image that is added to the gallery
276 * @param string $html Additional HTML text to be shown. The name and size
277 * of the image are always shown.
278 * @param string $alt Alt text for the image
279 * @param string $link Override image link (optional)
280 * @param array $handlerOpts Array of options for image handler (aka page number)
282 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
283 if ( $title instanceof File
) {
284 // Old calling convention
285 $title = $title->getTitle();
287 array_unshift( $this->mImages
, [ &$title, $html, $alt, $link, $handlerOpts ] );
291 * Returns the list of images this gallery contains
294 public function getImages() {
295 return $this->mImages
;
299 * isEmpty() returns true if the gallery contains no images
303 return empty( $this->mImages
);
307 * Enable/Disable showing of the dimensions of an image in the gallery.
308 * Enabled by default.
310 * @param bool $f Set to false to disable
312 function setShowDimensions( $f ) {
313 $this->mShowDimensions
= (bool)$f;
317 * Enable/Disable showing of the file size of an image in the gallery.
318 * Enabled by default.
320 * @param bool $f Set to false to disable
322 function setShowBytes( $f ) {
323 $this->mShowBytes
= (bool)$f;
327 * Enable/Disable showing of the filename of an image in the gallery.
328 * Enabled by default.
330 * @param bool $f Set to false to disable
332 function setShowFilename( $f ) {
333 $this->mShowFilename
= (bool)$f;
337 * Set arbitrary attributes to go on the HTML gallery output element.
338 * Should be suitable for a <ul> element.
340 * Note -- if taking from user input, you should probably run through
341 * Sanitizer::validateAttributes() first.
343 * @param array $attribs Array of HTML attribute pairs
345 function setAttributes( $attribs ) {
346 $this->mAttribs
= $attribs;
350 * Display an html representation of the gallery
352 * @return string The html
354 abstract public function toHTML();
357 * @return int Number of images in the gallery
359 public function count() {
360 return count( $this->mImages
);
364 * Set the contextual title
366 * @param Title $title Contextual title
368 public function setContextTitle( $title ) {
369 $this->contextTitle
= $title;
373 * Get the contextual title, if applicable
375 * @return Title|bool Title or false
377 public function getContextTitle() {
378 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
379 ?
$this->contextTitle
384 * Determines the correct language to be used for this image gallery
387 protected function getRenderLang() {
388 return $this->mParser
389 ?
$this->mParser
->getTargetLanguage()
390 : $this->getLanguage();