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 * @var bool Hide blacklisted images?
64 protected $mHideBadImages;
67 * @var Parser Registered parser object for output callbacks
72 * @var Title Contextual title, used when images are being screened against
75 protected $contextTitle = false;
78 protected $mAttribs = [];
81 static private $modeMapping = false;
84 * Get a new image gallery. This is the method other callers
85 * should use to get a gallery.
87 * @param string|bool $mode Mode to use. False to use the default
88 * @param IContextSource|null $context
89 * @return ImageGalleryBase
92 static function factory( $mode = false, IContextSource
$context = null ) {
96 $context = RequestContext
::getMainAndWarn( __METHOD__
);
99 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
100 $mode = $galleryOptions['mode'];
103 $mode = $wgContLang->lc( $mode );
105 if ( isset( self
::$modeMapping[$mode] ) ) {
106 $class = self
::$modeMapping[$mode];
107 return new $class( $mode, $context );
109 throw new MWException( "No gallery class registered for mode $mode" );
113 private static function loadModes() {
114 if ( self
::$modeMapping === false ) {
115 self
::$modeMapping = [
116 'traditional' => TraditionalImageGallery
::class,
117 'nolines' => NolinesImageGallery
::class,
118 'packed' => PackedImageGallery
::class,
119 'packed-hover' => PackedHoverImageGallery
::class,
120 'packed-overlay' => PackedOverlayImageGallery
::class,
121 'slideshow' => SlideshowImageGallery
::class,
123 // Allow extensions to make a new gallery format.
124 Hooks
::run( 'GalleryGetModes', [ &self
::$modeMapping ] );
129 * Create a new image gallery object.
131 * You should not call this directly, but instead use
132 * ImageGalleryBase::factory().
133 * @param string $mode
134 * @param IContextSource|null $context
136 function __construct( $mode = 'traditional', IContextSource
$context = null ) {
138 $this->setContext( $context );
141 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
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;
156 * Register a parser object. If you do not set this
157 * and the output of this gallery ends up in parser
158 * cache, the javascript will break!
160 * @note This also triggers using the page's target
161 * language instead of the user language.
163 * @param Parser $parser
165 function setParser( $parser ) {
166 $this->mParser
= $parser;
173 function setHideBadImages( $flag = true ) {
174 $this->mHideBadImages
= $flag;
178 * Set the caption (as plain text)
180 * @param string $caption
182 function setCaption( $caption ) {
183 $this->mCaption
= htmlspecialchars( $caption );
187 * Set the caption (as HTML)
189 * @param string $caption
191 public function setCaptionHtml( $caption ) {
192 $this->mCaption
= $caption;
196 * Set how many images will be displayed per row.
198 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
199 * to screensize invalid numbers will be rejected
201 public function setPerRow( $num ) {
203 $this->mPerRow
= (int)$num;
208 * Set how wide each image will be, in pixels.
210 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
211 * and those below 0 are ignored.
213 public function setWidths( $num ) {
214 $parsed = Parser
::parseWidthParam( $num, false );
215 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
216 $this->mWidths
= $parsed['width'];
221 * Set how high each image will be, in pixels.
223 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
224 * and those below 0 are ignored.
226 public function setHeights( $num ) {
227 $parsed = Parser
::parseWidthParam( $num, false );
228 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
229 $this->mHeights
= $parsed['width'];
234 * Allow setting additional options. This is meant
235 * to allow extensions to add additional parameters to
236 * <gallery> parser tag.
238 * @param array $options Attributes of gallery tag
240 public function setAdditionalOptions( $options ) {
244 * Add an image to the gallery.
246 * @param Title $title Title object of the image that is added to the gallery
247 * @param string $html Additional HTML text to be shown. The name and size
248 * of the image are always shown.
249 * @param string $alt Alt text for the image
250 * @param string $link Override image link (optional)
251 * @param array $handlerOpts Array of options for image handler (aka page number)
253 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
254 if ( $title instanceof File
) {
255 // Old calling convention
256 $title = $title->getTitle();
258 $this->mImages
[] = [ $title, $html, $alt, $link, $handlerOpts ];
259 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
263 * Add an image at the beginning of 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 insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
273 if ( $title instanceof File
) {
274 // Old calling convention
275 $title = $title->getTitle();
277 array_unshift( $this->mImages
, [ &$title, $html, $alt, $link, $handlerOpts ] );
281 * Returns the list of images this gallery contains
284 public function getImages() {
285 return $this->mImages
;
289 * isEmpty() returns true if the gallery contains no images
293 return empty( $this->mImages
);
297 * Enable/Disable showing of the dimensions of an image in the gallery.
298 * Enabled by default.
300 * @param bool $f Set to false to disable
302 function setShowDimensions( $f ) {
303 $this->mShowDimensions
= (bool)$f;
307 * Enable/Disable showing of the file size of an image in the gallery.
308 * Enabled by default.
310 * @param bool $f Set to false to disable
312 function setShowBytes( $f ) {
313 $this->mShowBytes
= (bool)$f;
317 * Enable/Disable showing of the filename of an image in the gallery.
318 * Enabled by default.
320 * @param bool $f Set to false to disable
322 function setShowFilename( $f ) {
323 $this->mShowFilename
= (bool)$f;
327 * Set arbitrary attributes to go on the HTML gallery output element.
328 * Should be suitable for a <ul> element.
330 * Note -- if taking from user input, you should probably run through
331 * Sanitizer::validateAttributes() first.
333 * @param array $attribs Array of HTML attribute pairs
335 function setAttributes( $attribs ) {
336 $this->mAttribs
= $attribs;
340 * Display an html representation of the gallery
342 * @return string The html
344 abstract public function toHTML();
347 * @return int Number of images in the gallery
349 public function count() {
350 return count( $this->mImages
);
354 * Set the contextual title
356 * @param Title $title Contextual title
358 public function setContextTitle( $title ) {
359 $this->contextTitle
= $title;
363 * Get the contextual title, if applicable
365 * @return Title|bool Title or false
367 public function getContextTitle() {
368 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
369 ?
$this->contextTitle
374 * Determines the correct language to be used for this image gallery
377 protected function getRenderLang() {
378 return $this->mParser
379 ?
$this->mParser
->getTargetLanguage()
380 : $this->getLanguage();