Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / includes / gallery / ImageGalleryBase.php
blobeeb8a8ff849e7675f5d28ca326e3310827a9b527
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 dimensions in categories
44 protected $mShowDimensions;
46 /**
47 * @var bool Whether to show the filename. Default: true
49 protected $mShowFilename;
51 /**
52 * @var string Gallery mode. Default: traditional
54 protected $mMode;
56 /**
57 * @var bool|string Gallery caption. Default: false
59 protected $mCaption = false;
61 /**
62 * @var bool Hide blacklisted images?
64 protected $mHideBadImages;
66 /**
67 * @var Parser Registered parser object for output callbacks
69 public $mParser;
71 /**
72 * @var Title Contextual title, used when images are being screened against
73 * the bad image list
75 protected $contextTitle = false;
77 /** @var array */
78 protected $mAttribs = [];
80 /** @var bool */
81 static private $modeMapping = false;
83 /**
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
90 * @throws MWException
92 static function factory( $mode = false, IContextSource $context = null ) {
93 global $wgContLang;
94 self::loadModes();
95 if ( !$context ) {
96 $context = RequestContext::getMainAndWarn( __METHOD__ );
98 if ( !$mode ) {
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 );
108 } else {
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',
117 'nolines' => 'NolinesImageGallery',
118 'packed' => 'PackedImageGallery',
119 'packed-hover' => 'PackedHoverImageGallery',
120 'packed-overlay' => 'PackedOverlayImageGallery',
121 'slideshow' => 'SlideshowImageGallery',
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 ) {
137 if ( $context ) {
138 $this->setContext( $context );
141 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
142 $this->mImages = [];
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;
170 * Set bad image flag
171 * @param bool $flag
173 function setHideBadImages( $flag = true ) {
174 $this->mHideBadImages = $flag;
178 * Set the caption (as plain text)
180 * @param string $caption Caption
182 function setCaption( $caption ) {
183 $this->mCaption = htmlspecialchars( $caption );
187 * Set the caption (as HTML)
189 * @param string $caption 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 ) {
202 if ( $num >= 0 ) {
203 $this->mPerRow = (int)$num;
208 * Set how wide each image will be, in pixels.
210 * @param int $num Integer > 0; invalid numbers will be ignored
212 public function setWidths( $num ) {
213 if ( $num > 0 ) {
214 $this->mWidths = (int)$num;
219 * Set how high each image will be, in pixels.
221 * @param int $num Integer > 0; invalid numbers will be ignored
223 public function setHeights( $num ) {
224 if ( $num > 0 ) {
225 $this->mHeights = (int)$num;
230 * Allow setting additional options. This is meant
231 * to allow extensions to add additional parameters to
232 * <gallery> parser tag.
234 * @param array $options Attributes of gallery tag
236 public function setAdditionalOptions( $options ) {
240 * Add an image to the gallery.
242 * @param Title $title Title object of the image that is added to the gallery
243 * @param string $html Additional HTML text to be shown. The name and size
244 * of the image are always shown.
245 * @param string $alt Alt text for the image
246 * @param string $link Override image link (optional)
247 * @param array $handlerOpts Array of options for image handler (aka page number)
249 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
250 if ( $title instanceof File ) {
251 // Old calling convention
252 $title = $title->getTitle();
254 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
255 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
259 * Add an image at the beginning of the gallery.
261 * @param Title $title Title object of the image that is added to the gallery
262 * @param string $html Additional HTML text to be shown. The name and size
263 * of the image are always shown.
264 * @param string $alt Alt text for the image
265 * @param string $link Override image link (optional)
266 * @param array $handlerOpts Array of options for image handler (aka page number)
268 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
269 if ( $title instanceof File ) {
270 // Old calling convention
271 $title = $title->getTitle();
273 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
277 * Returns the list of images this gallery contains
278 * @return array
280 public function getImages() {
281 return $this->mImages;
285 * isEmpty() returns true if the gallery contains no images
286 * @return bool
288 function isEmpty() {
289 return empty( $this->mImages );
293 * Enable/Disable showing of the dimensions of an image in the gallery.
294 * Enabled by default.
296 * @param bool $f Set to false to disable
298 function setShowDimensions( $f ) {
299 $this->mShowDimensions = (bool)$f;
303 * Enable/Disable showing of the file size of an image in the gallery.
304 * Enabled by default.
306 * @param bool $f Set to false to disable
308 function setShowBytes( $f ) {
309 $this->mShowBytes = (bool)$f;
313 * Enable/Disable showing of the filename of an image in the gallery.
314 * Enabled by default.
316 * @param bool $f Set to false to disable
318 function setShowFilename( $f ) {
319 $this->mShowFilename = (bool)$f;
323 * Set arbitrary attributes to go on the HTML gallery output element.
324 * Should be suitable for a <ul> element.
326 * Note -- if taking from user input, you should probably run through
327 * Sanitizer::validateAttributes() first.
329 * @param array $attribs Array of HTML attribute pairs
331 function setAttributes( $attribs ) {
332 $this->mAttribs = $attribs;
336 * Display an html representation of the gallery
338 * @return string The html
340 abstract public function toHTML();
343 * @return int Number of images in the gallery
345 public function count() {
346 return count( $this->mImages );
350 * Set the contextual title
352 * @param Title $title Contextual title
354 public function setContextTitle( $title ) {
355 $this->contextTitle = $title;
359 * Get the contextual title, if applicable
361 * @return Title|bool Title or false
363 public function getContextTitle() {
364 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
365 ? $this->contextTitle
366 : false;
370 * Determines the correct language to be used for this image gallery
371 * @return Language
373 protected function getRenderLang() {
374 return $this->mParser
375 ? $this->mParser->getTargetLanguage()
376 : $this->getLanguage();