Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / gallery / ImageGalleryBase.php
blob6884f65626dd66f8485885feb2de0e9ad376b145
1 <?php
2 /**
3 * Image gallery.
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
20 * @file
23 /**
24 * Image gallery
26 * Add images to the gallery using add(), then render that list to HTML using toHTML().
28 * @ingroup Media
30 abstract class ImageGalleryBase extends ContextSource {
31 /**
32 * @var array Gallery images
34 protected $mImages;
36 /**
37 * @var bool Whether to show the filesize in bytes in categories
39 protected $mShowBytes;
41 /**
42 * @var bool Whether to show the filename. Default: true
44 protected $mShowFilename;
46 /**
47 * @var string Gallery mode. Default: traditional
49 protected $mMode;
51 /**
52 * @var bool|string Gallery caption. Default: false
54 protected $mCaption = false;
56 /**
57 * @var bool Hide blacklisted images?
59 protected $mHideBadImages;
61 /**
62 * @var Parser Registered parser object for output callbacks
64 public $mParser;
66 /**
67 * @var Title Contextual title, used when images are being screened against
68 * the bad image list
70 protected $contextTitle = false;
72 /** @var array */
73 protected $mAttribs = [];
75 /** @var bool */
76 static private $modeMapping = false;
78 /**
79 * Get a new image gallery. This is the method other callers
80 * should use to get a gallery.
82 * @param string|bool $mode Mode to use. False to use the default
83 * @param IContextSource|null $context
84 * @return ImageGalleryBase
85 * @throws MWException
87 static function factory( $mode = false, IContextSource $context = null ) {
88 global $wgContLang;
89 self::loadModes();
90 if ( !$context ) {
91 $context = RequestContext::getMainAndWarn( __METHOD__ );
93 if ( !$mode ) {
94 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
95 $mode = $galleryOptions['mode'];
98 $mode = $wgContLang->lc( $mode );
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" );
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 'slideshow' => 'SlideshowImageGallery',
118 // Allow extensions to make a new gallery format.
119 Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
124 * Create a new image gallery object.
126 * You should not call this directly, but instead use
127 * ImageGalleryBase::factory().
128 * @param string $mode
129 * @param IContextSource|null $context
131 function __construct( $mode = 'traditional', IContextSource $context = null ) {
132 if ( $context ) {
133 $this->setContext( $context );
136 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
137 $this->mImages = [];
138 $this->mShowBytes = $galleryOptions['showBytes'];
139 $this->mShowFilename = true;
140 $this->mParser = false;
141 $this->mHideBadImages = false;
142 $this->mPerRow = $galleryOptions['imagesPerRow'];
143 $this->mWidths = $galleryOptions['imageWidth'];
144 $this->mHeights = $galleryOptions['imageHeight'];
145 $this->mCaptionLength = $galleryOptions['captionLength'];
146 $this->mMode = $mode;
150 * Register a parser object. If you do not set this
151 * and the output of this gallery ends up in parser
152 * cache, the javascript will break!
154 * @note This also triggers using the page's target
155 * language instead of the user language.
157 * @param Parser $parser
159 function setParser( $parser ) {
160 $this->mParser = $parser;
164 * Set bad image flag
165 * @param bool $flag
167 function setHideBadImages( $flag = true ) {
168 $this->mHideBadImages = $flag;
172 * Set the caption (as plain text)
174 * @param string $caption Caption
176 function setCaption( $caption ) {
177 $this->mCaption = htmlspecialchars( $caption );
181 * Set the caption (as HTML)
183 * @param string $caption Caption
185 public function setCaptionHtml( $caption ) {
186 $this->mCaption = $caption;
190 * Set how many images will be displayed per row.
192 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
193 * to screensize invalid numbers will be rejected
195 public function setPerRow( $num ) {
196 if ( $num >= 0 ) {
197 $this->mPerRow = (int)$num;
202 * Set how wide each image will be, in pixels.
204 * @param int $num Integer > 0; invalid numbers will be ignored
206 public function setWidths( $num ) {
207 if ( $num > 0 ) {
208 $this->mWidths = (int)$num;
213 * Set how high each image will be, in pixels.
215 * @param int $num Integer > 0; invalid numbers will be ignored
217 public function setHeights( $num ) {
218 if ( $num > 0 ) {
219 $this->mHeights = (int)$num;
224 * Allow setting additional options. This is meant
225 * to allow extensions to add additional parameters to
226 * <gallery> parser tag.
228 * @param array $options Attributes of gallery tag
230 public function setAdditionalOptions( $options ) {
234 * Add an image to the gallery.
236 * @param Title $title Title object of the image that is added to the gallery
237 * @param string $html Additional HTML text to be shown. The name and size
238 * of the image are always shown.
239 * @param string $alt Alt text for the image
240 * @param string $link Override image link (optional)
241 * @param array $handlerOpts Array of options for image handler (aka page number)
243 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
244 if ( $title instanceof File ) {
245 // Old calling convention
246 $title = $title->getTitle();
248 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
249 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
253 * Add an image at the beginning of 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 insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
263 if ( $title instanceof File ) {
264 // Old calling convention
265 $title = $title->getTitle();
267 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
271 * Returns the list of images this gallery contains
272 * @return array
274 public function getImages() {
275 return $this->mImages;
279 * isEmpty() returns true if the gallery contains no images
280 * @return bool
282 function isEmpty() {
283 return empty( $this->mImages );
287 * Enable/Disable showing of the file size of an image in the gallery.
288 * Enabled by default.
290 * @param bool $f Set to false to disable
292 function setShowBytes( $f ) {
293 $this->mShowBytes = (bool)$f;
297 * Enable/Disable showing of the filename of an image in the gallery.
298 * Enabled by default.
300 * @param bool $f Set to false to disable
302 function setShowFilename( $f ) {
303 $this->mShowFilename = (bool)$f;
307 * Set arbitrary attributes to go on the HTML gallery output element.
308 * Should be suitable for a <ul> element.
310 * Note -- if taking from user input, you should probably run through
311 * Sanitizer::validateAttributes() first.
313 * @param array $attribs Array of HTML attribute pairs
315 function setAttributes( $attribs ) {
316 $this->mAttribs = $attribs;
320 * Display an html representation of the gallery
322 * @return string The html
324 abstract public function toHTML();
327 * @return int Number of images in the gallery
329 public function count() {
330 return count( $this->mImages );
334 * Set the contextual title
336 * @param Title $title Contextual title
338 public function setContextTitle( $title ) {
339 $this->contextTitle = $title;
343 * Get the contextual title, if applicable
345 * @return Title|bool Title or false
347 public function getContextTitle() {
348 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
349 ? $this->contextTitle
350 : false;
354 * Determines the correct language to be used for this image gallery
355 * @return Language
357 protected function getRenderLang() {
358 return $this->mParser
359 ? $this->mParser->getTargetLanguage()
360 : $this->getLanguage();