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
26 * Add images to the gallery using add(), then render that list to HTML using toHTML().
30 abstract class ImageGalleryBase
extends ContextSource
{
32 * @var array Gallery images
37 * @var bool Whether to show the filesize in bytes in categories
39 protected $mShowBytes;
42 * @var bool Whether to show the dimensions in categories
44 protected $mShowDimensions;
47 * @var bool Whether to show the filename. Default: true
49 protected $mShowFilename;
52 * @var string Gallery mode. Default: traditional
57 * @var bool|string Gallery caption. Default: false
59 protected $mCaption = false;
62 * Length to truncate filename to in caption when using "showfilename".
63 * A value of 'true' will truncate the filename to one line using CSS
64 * and will be the behaviour after deprecation.
68 protected $mCaptionLength = true;
71 * @var bool Hide blacklisted images?
73 protected $mHideBadImages;
76 * @var Parser Registered parser object for output callbacks
81 * @var Title Contextual title, used when images are being screened against
84 protected $contextTitle = false;
87 protected $mAttribs = [];
90 static private $modeMapping = false;
93 * Get a new image gallery. This is the method other callers
94 * should use to get a gallery.
96 * @param string|bool $mode Mode to use. False to use the default
97 * @param IContextSource|null $context
98 * @return ImageGalleryBase
101 static function factory( $mode = false, IContextSource
$context = null ) {
105 $context = RequestContext
::getMainAndWarn( __METHOD__
);
108 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
109 $mode = $galleryOptions['mode'];
112 $mode = $wgContLang->lc( $mode );
114 if ( isset( self
::$modeMapping[$mode] ) ) {
115 $class = self
::$modeMapping[$mode];
116 return new $class( $mode, $context );
118 throw new MWException( "No gallery class registered for mode $mode" );
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,
132 // Allow extensions to make a new gallery format.
133 Hooks
::run( 'GalleryGetModes', [ &self
::$modeMapping ] );
138 * Create a new image gallery object.
140 * You should not call this directly, but instead use
141 * ImageGalleryBase::factory().
142 * @param string $mode
143 * @param IContextSource|null $context
145 function __construct( $mode = 'traditional', IContextSource
$context = null ) {
147 $this->setContext( $context );
150 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
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;
165 * Register a parser object. If you do not set this
166 * and the output of this gallery ends up in parser
167 * cache, the javascript will break!
169 * @note This also triggers using the page's target
170 * language instead of the user language.
172 * @param Parser $parser
174 function setParser( $parser ) {
175 $this->mParser
= $parser;
182 function setHideBadImages( $flag = true ) {
183 $this->mHideBadImages
= $flag;
187 * Set the caption (as plain text)
189 * @param string $caption
191 function setCaption( $caption ) {
192 $this->mCaption
= htmlspecialchars( $caption );
196 * Set the caption (as HTML)
198 * @param string $caption
200 public function setCaptionHtml( $caption ) {
201 $this->mCaption
= $caption;
205 * Set how many images will be displayed per row.
207 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
208 * to screensize invalid numbers will be rejected
210 public function setPerRow( $num ) {
212 $this->mPerRow
= (int)$num;
217 * Set how wide each image will be, in pixels.
219 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
220 * and those below 0 are ignored.
222 public function setWidths( $num ) {
223 $parsed = Parser
::parseWidthParam( $num, false );
224 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
225 $this->mWidths
= $parsed['width'];
230 * Set how high each image will be, in pixels.
232 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
233 * and those below 0 are ignored.
235 public function setHeights( $num ) {
236 $parsed = Parser
::parseWidthParam( $num, false );
237 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
238 $this->mHeights
= $parsed['width'];
243 * Allow setting additional options. This is meant
244 * to allow extensions to add additional parameters to
245 * <gallery> parser tag.
247 * @param array $options Attributes of gallery tag
249 public function setAdditionalOptions( $options ) {
253 * Add an image to the gallery.
255 * @param Title $title Title object of the image that is added to the gallery
256 * @param string $html Additional HTML text to be shown. The name and size
257 * of the image are always shown.
258 * @param string $alt Alt text for the image
259 * @param string $link Override image link (optional)
260 * @param array $handlerOpts Array of options for image handler (aka page number)
262 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
263 if ( $title instanceof File
) {
264 // Old calling convention
265 $title = $title->getTitle();
267 $this->mImages
[] = [ $title, $html, $alt, $link, $handlerOpts ];
268 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
272 * Add an image at the beginning of the gallery.
274 * @param Title $title Title object of the image that is added to the gallery
275 * @param string $html Additional HTML text to be shown. The name and size
276 * of the image are always shown.
277 * @param string $alt Alt text for the image
278 * @param string $link Override image link (optional)
279 * @param array $handlerOpts Array of options for image handler (aka page number)
281 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
282 if ( $title instanceof File
) {
283 // Old calling convention
284 $title = $title->getTitle();
286 array_unshift( $this->mImages
, [ &$title, $html, $alt, $link, $handlerOpts ] );
290 * Returns the list of images this gallery contains
293 public function getImages() {
294 return $this->mImages
;
298 * isEmpty() returns true if the gallery contains no images
302 return empty( $this->mImages
);
306 * Enable/Disable showing of the dimensions of an image in the gallery.
307 * Enabled by default.
309 * @param bool $f Set to false to disable
311 function setShowDimensions( $f ) {
312 $this->mShowDimensions
= (bool)$f;
316 * Enable/Disable showing of the file size of an image in the gallery.
317 * Enabled by default.
319 * @param bool $f Set to false to disable
321 function setShowBytes( $f ) {
322 $this->mShowBytes
= (bool)$f;
326 * Enable/Disable showing of the filename of an image in the gallery.
327 * Enabled by default.
329 * @param bool $f Set to false to disable
331 function setShowFilename( $f ) {
332 $this->mShowFilename
= (bool)$f;
336 * Set arbitrary attributes to go on the HTML gallery output element.
337 * Should be suitable for a <ul> element.
339 * Note -- if taking from user input, you should probably run through
340 * Sanitizer::validateAttributes() first.
342 * @param array $attribs Array of HTML attribute pairs
344 function setAttributes( $attribs ) {
345 $this->mAttribs
= $attribs;
349 * Display an html representation of the gallery
351 * @return string The html
353 abstract public function toHTML();
356 * @return int Number of images in the gallery
358 public function count() {
359 return count( $this->mImages
);
363 * Set the contextual title
365 * @param Title $title Contextual title
367 public function setContextTitle( $title ) {
368 $this->contextTitle
= $title;
372 * Get the contextual title, if applicable
374 * @return Title|bool Title or false
376 public function getContextTitle() {
377 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
378 ?
$this->contextTitle
383 * Determines the correct language to be used for this image gallery
386 protected function getRenderLang() {
387 return $this->mParser
388 ?
$this->mParser
->getTargetLanguage()
389 : $this->getLanguage();