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