Merge "Declare dynamic properties"
[lhc/web/wiklou.git] / includes / gallery / ImageGalleryBase.php
1 <?php
2 /**
3 * Image gallery.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Image gallery
27 *
28 * Add images to the gallery using add(), then render that list to HTML using toHTML().
29 *
30 * @ingroup Media
31 */
32 abstract class ImageGalleryBase extends ContextSource {
33 /**
34 * @var array Gallery images
35 */
36 protected $mImages;
37
38 /**
39 * @var bool Whether to show the filesize in bytes in categories
40 */
41 protected $mShowBytes;
42
43 /**
44 * @var bool Whether to show the dimensions in categories
45 */
46 protected $mShowDimensions;
47
48 /**
49 * @var bool Whether to show the filename. Default: true
50 */
51 protected $mShowFilename;
52
53 /**
54 * @var string Gallery mode. Default: traditional
55 */
56 protected $mMode;
57
58 /**
59 * @var bool|string Gallery caption. Default: false
60 */
61 protected $mCaption = false;
62
63 /**
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.
67 *
68 * @var bool|int
69 */
70 protected $mCaptionLength = true;
71
72 /**
73 * @var bool Hide blacklisted images?
74 */
75 protected $mHideBadImages;
76
77 /**
78 * @var Parser|false Registered parser object for output callbacks
79 */
80 public $mParser;
81
82 /**
83 * @var Title|null Contextual title, used when images are being screened against
84 * the bad image list
85 */
86 protected $contextTitle = null;
87
88 /** @var array */
89 protected $mAttribs = [];
90
91 /** @var int */
92 protected $mPerRow;
93
94 /** @var int */
95 protected $mWidths;
96
97 /** @var int */
98 protected $mHeights;
99
100 /** @var array */
101 private static $modeMapping;
102
103 /**
104 * Get a new image gallery. This is the method other callers
105 * should use to get a gallery.
106 *
107 * @param string|bool $mode Mode to use. False to use the default
108 * @param IContextSource|null $context
109 * @return ImageGalleryBase
110 * @throws MWException
111 */
112 static function factory( $mode = false, IContextSource $context = null ) {
113 self::loadModes();
114 if ( !$context ) {
115 $context = RequestContext::getMainAndWarn( __METHOD__ );
116 }
117 if ( !$mode ) {
118 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
119 $mode = $galleryOptions['mode'];
120 }
121
122 $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
123
124 if ( isset( self::$modeMapping[$mode] ) ) {
125 $class = self::$modeMapping[$mode];
126 return new $class( $mode, $context );
127 } else {
128 throw new MWException( "No gallery class registered for mode $mode" );
129 }
130 }
131
132 private static function loadModes() {
133 if ( self::$modeMapping === null ) {
134 self::$modeMapping = [
135 'traditional' => TraditionalImageGallery::class,
136 'nolines' => NolinesImageGallery::class,
137 'packed' => PackedImageGallery::class,
138 'packed-hover' => PackedHoverImageGallery::class,
139 'packed-overlay' => PackedOverlayImageGallery::class,
140 'slideshow' => SlideshowImageGallery::class,
141 ];
142 // Allow extensions to make a new gallery format.
143 Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
144 }
145 }
146
147 /**
148 * Create a new image gallery object.
149 *
150 * You should not call this directly, but instead use
151 * ImageGalleryBase::factory().
152 * @param string $mode
153 * @param IContextSource|null $context
154 */
155 function __construct( $mode = 'traditional', IContextSource $context = null ) {
156 if ( $context ) {
157 $this->setContext( $context );
158 }
159
160 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
161 $this->mImages = [];
162 $this->mShowBytes = $galleryOptions['showBytes'];
163 $this->mShowDimensions = $galleryOptions['showDimensions'];
164 $this->mShowFilename = true;
165 $this->mParser = false;
166 $this->mHideBadImages = false;
167 $this->mPerRow = $galleryOptions['imagesPerRow'];
168 $this->mWidths = $galleryOptions['imageWidth'];
169 $this->mHeights = $galleryOptions['imageHeight'];
170 $this->mCaptionLength = $galleryOptions['captionLength'];
171 $this->mMode = $mode;
172 }
173
174 /**
175 * Register a parser object. If you do not set this
176 * and the output of this gallery ends up in parser
177 * cache, the javascript will break!
178 *
179 * @note This also triggers using the page's target
180 * language instead of the user language.
181 *
182 * @param Parser $parser
183 */
184 function setParser( $parser ) {
185 $this->mParser = $parser;
186 }
187
188 /**
189 * Set bad image flag
190 * @param bool $flag
191 */
192 function setHideBadImages( $flag = true ) {
193 $this->mHideBadImages = $flag;
194 }
195
196 /**
197 * Set the caption (as plain text)
198 *
199 * @param string $caption
200 */
201 function setCaption( $caption ) {
202 $this->mCaption = htmlspecialchars( $caption );
203 }
204
205 /**
206 * Set the caption (as HTML)
207 *
208 * @param string $caption
209 */
210 public function setCaptionHtml( $caption ) {
211 $this->mCaption = $caption;
212 }
213
214 /**
215 * Set how many images will be displayed per row.
216 *
217 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
218 * to screensize invalid numbers will be rejected
219 */
220 public function setPerRow( $num ) {
221 if ( $num >= 0 ) {
222 $this->mPerRow = (int)$num;
223 }
224 }
225
226 /**
227 * Set how wide each image will be, in pixels.
228 *
229 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
230 * and those below 0 are ignored.
231 */
232 public function setWidths( $num ) {
233 $parsed = Parser::parseWidthParam( $num, false );
234 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
235 $this->mWidths = $parsed['width'];
236 }
237 }
238
239 /**
240 * Set how high each image will be, in pixels.
241 *
242 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
243 * and those below 0 are ignored.
244 */
245 public function setHeights( $num ) {
246 $parsed = Parser::parseWidthParam( $num, false );
247 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
248 $this->mHeights = $parsed['width'];
249 }
250 }
251
252 /**
253 * Allow setting additional options. This is meant
254 * to allow extensions to add additional parameters to
255 * <gallery> parser tag.
256 *
257 * @param array $options Attributes of gallery tag
258 */
259 public function setAdditionalOptions( $options ) {
260 }
261
262 /**
263 * Add an image to the gallery.
264 *
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)
271 */
272 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
273 if ( $title instanceof File ) {
274 // Old calling convention
275 $title = $title->getTitle();
276 }
277 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
278 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
279 }
280
281 /**
282 * Add an image at the beginning of the gallery.
283 *
284 * @param Title $title Title object of the image that is added to the gallery
285 * @param string $html Additional HTML text to be shown. The name and size
286 * of the image are always shown.
287 * @param string $alt Alt text for the image
288 * @param string $link Override image link (optional)
289 * @param array $handlerOpts Array of options for image handler (aka page number)
290 */
291 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
292 if ( $title instanceof File ) {
293 // Old calling convention
294 $title = $title->getTitle();
295 }
296 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
297 }
298
299 /**
300 * Returns the list of images this gallery contains
301 * @return array
302 */
303 public function getImages() {
304 return $this->mImages;
305 }
306
307 /**
308 * isEmpty() returns true if the gallery contains no images
309 * @return bool
310 */
311 function isEmpty() {
312 return empty( $this->mImages );
313 }
314
315 /**
316 * Enable/Disable showing of the dimensions of an image in the gallery.
317 * Enabled by default.
318 *
319 * @param bool $f Set to false to disable
320 */
321 function setShowDimensions( $f ) {
322 $this->mShowDimensions = (bool)$f;
323 }
324
325 /**
326 * Enable/Disable showing of the file size of an image in the gallery.
327 * Enabled by default.
328 *
329 * @param bool $f Set to false to disable
330 */
331 function setShowBytes( $f ) {
332 $this->mShowBytes = (bool)$f;
333 }
334
335 /**
336 * Enable/Disable showing of the filename of an image in the gallery.
337 * Enabled by default.
338 *
339 * @param bool $f Set to false to disable
340 */
341 function setShowFilename( $f ) {
342 $this->mShowFilename = (bool)$f;
343 }
344
345 /**
346 * Set arbitrary attributes to go on the HTML gallery output element.
347 * Should be suitable for a <ul> element.
348 *
349 * Note -- if taking from user input, you should probably run through
350 * Sanitizer::validateAttributes() first.
351 *
352 * @param array $attribs Array of HTML attribute pairs
353 */
354 function setAttributes( $attribs ) {
355 $this->mAttribs = $attribs;
356 }
357
358 /**
359 * Display an html representation of the gallery
360 *
361 * @return string The html
362 */
363 abstract public function toHTML();
364
365 /**
366 * @return int Number of images in the gallery
367 */
368 public function count() {
369 return count( $this->mImages );
370 }
371
372 /**
373 * Set the contextual title
374 *
375 * @param Title|null $title Contextual title
376 */
377 public function setContextTitle( $title ) {
378 $this->contextTitle = $title;
379 }
380
381 /**
382 * Get the contextual title, if applicable
383 *
384 * @return Title|null
385 */
386 public function getContextTitle() {
387 return $this->contextTitle;
388 }
389
390 /**
391 * Determines the correct language to be used for this image gallery
392 * @return Language
393 */
394 protected function getRenderLang() {
395 return $this->mParser
396 ? $this->mParser->getTargetLanguage()
397 : $this->getLanguage();
398 }
399 }