Merge "Add tablesUsed to RevisionStoreDbTest"
[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 * @var bool Hide blacklisted images?
63 */
64 protected $mHideBadImages;
65
66 /**
67 * @var Parser Registered parser object for output callbacks
68 */
69 public $mParser;
70
71 /**
72 * @var Title Contextual title, used when images are being screened against
73 * the bad image list
74 */
75 protected $contextTitle = false;
76
77 /** @var array */
78 protected $mAttribs = [];
79
80 /** @var bool */
81 static private $modeMapping = false;
82
83 /**
84 * Get a new image gallery. This is the method other callers
85 * should use to get a gallery.
86 *
87 * @param string|bool $mode Mode to use. False to use the default
88 * @param IContextSource|null $context
89 * @return ImageGalleryBase
90 * @throws MWException
91 */
92 static function factory( $mode = false, IContextSource $context = null ) {
93 global $wgContLang;
94 self::loadModes();
95 if ( !$context ) {
96 $context = RequestContext::getMainAndWarn( __METHOD__ );
97 }
98 if ( !$mode ) {
99 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
100 $mode = $galleryOptions['mode'];
101 }
102
103 $mode = $wgContLang->lc( $mode );
104
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" );
110 }
111 }
112
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,
122 ];
123 // Allow extensions to make a new gallery format.
124 Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
125 }
126 }
127
128 /**
129 * Create a new image gallery object.
130 *
131 * You should not call this directly, but instead use
132 * ImageGalleryBase::factory().
133 * @param string $mode
134 * @param IContextSource|null $context
135 */
136 function __construct( $mode = 'traditional', IContextSource $context = null ) {
137 if ( $context ) {
138 $this->setContext( $context );
139 }
140
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;
153 }
154
155 /**
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!
159 *
160 * @note This also triggers using the page's target
161 * language instead of the user language.
162 *
163 * @param Parser $parser
164 */
165 function setParser( $parser ) {
166 $this->mParser = $parser;
167 }
168
169 /**
170 * Set bad image flag
171 * @param bool $flag
172 */
173 function setHideBadImages( $flag = true ) {
174 $this->mHideBadImages = $flag;
175 }
176
177 /**
178 * Set the caption (as plain text)
179 *
180 * @param string $caption
181 */
182 function setCaption( $caption ) {
183 $this->mCaption = htmlspecialchars( $caption );
184 }
185
186 /**
187 * Set the caption (as HTML)
188 *
189 * @param string $caption
190 */
191 public function setCaptionHtml( $caption ) {
192 $this->mCaption = $caption;
193 }
194
195 /**
196 * Set how many images will be displayed per row.
197 *
198 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
199 * to screensize invalid numbers will be rejected
200 */
201 public function setPerRow( $num ) {
202 if ( $num >= 0 ) {
203 $this->mPerRow = (int)$num;
204 }
205 }
206
207 /**
208 * Set how wide each image will be, in pixels.
209 *
210 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
211 * and those below 0 are ignored.
212 */
213 public function setWidths( $num ) {
214 $parsed = Parser::parseWidthParam( $num, false );
215 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
216 $this->mWidths = $parsed['width'];
217 }
218 }
219
220 /**
221 * Set how high each image will be, in pixels.
222 *
223 * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
224 * and those below 0 are ignored.
225 */
226 public function setHeights( $num ) {
227 $parsed = Parser::parseWidthParam( $num, false );
228 if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
229 $this->mHeights = $parsed['width'];
230 }
231 }
232
233 /**
234 * Allow setting additional options. This is meant
235 * to allow extensions to add additional parameters to
236 * <gallery> parser tag.
237 *
238 * @param array $options Attributes of gallery tag
239 */
240 public function setAdditionalOptions( $options ) {
241 }
242
243 /**
244 * Add an image to the gallery.
245 *
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)
252 */
253 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
254 if ( $title instanceof File ) {
255 // Old calling convention
256 $title = $title->getTitle();
257 }
258 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
259 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
260 }
261
262 /**
263 * Add an image at the beginning of 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 insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
273 if ( $title instanceof File ) {
274 // Old calling convention
275 $title = $title->getTitle();
276 }
277 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
278 }
279
280 /**
281 * Returns the list of images this gallery contains
282 * @return array
283 */
284 public function getImages() {
285 return $this->mImages;
286 }
287
288 /**
289 * isEmpty() returns true if the gallery contains no images
290 * @return bool
291 */
292 function isEmpty() {
293 return empty( $this->mImages );
294 }
295
296 /**
297 * Enable/Disable showing of the dimensions of an image in the gallery.
298 * Enabled by default.
299 *
300 * @param bool $f Set to false to disable
301 */
302 function setShowDimensions( $f ) {
303 $this->mShowDimensions = (bool)$f;
304 }
305
306 /**
307 * Enable/Disable showing of the file size of an image in the gallery.
308 * Enabled by default.
309 *
310 * @param bool $f Set to false to disable
311 */
312 function setShowBytes( $f ) {
313 $this->mShowBytes = (bool)$f;
314 }
315
316 /**
317 * Enable/Disable showing of the filename of an image in the gallery.
318 * Enabled by default.
319 *
320 * @param bool $f Set to false to disable
321 */
322 function setShowFilename( $f ) {
323 $this->mShowFilename = (bool)$f;
324 }
325
326 /**
327 * Set arbitrary attributes to go on the HTML gallery output element.
328 * Should be suitable for a <ul> element.
329 *
330 * Note -- if taking from user input, you should probably run through
331 * Sanitizer::validateAttributes() first.
332 *
333 * @param array $attribs Array of HTML attribute pairs
334 */
335 function setAttributes( $attribs ) {
336 $this->mAttribs = $attribs;
337 }
338
339 /**
340 * Display an html representation of the gallery
341 *
342 * @return string The html
343 */
344 abstract public function toHTML();
345
346 /**
347 * @return int Number of images in the gallery
348 */
349 public function count() {
350 return count( $this->mImages );
351 }
352
353 /**
354 * Set the contextual title
355 *
356 * @param Title $title Contextual title
357 */
358 public function setContextTitle( $title ) {
359 $this->contextTitle = $title;
360 }
361
362 /**
363 * Get the contextual title, if applicable
364 *
365 * @return Title|bool Title or false
366 */
367 public function getContextTitle() {
368 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
369 ? $this->contextTitle
370 : false;
371 }
372
373 /**
374 * Determines the correct language to be used for this image gallery
375 * @return Language
376 */
377 protected function getRenderLang() {
378 return $this->mParser
379 ? $this->mParser->getTargetLanguage()
380 : $this->getLanguage();
381 }
382 }