Merge "Perform a permission check on the title when changing the page language"
[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',
117 'nolines' => 'NolinesImageGallery',
118 'packed' => 'PackedImageGallery',
119 'packed-hover' => 'PackedHoverImageGallery',
120 'packed-overlay' => 'PackedOverlayImageGallery',
121 'slideshow' => 'SlideshowImageGallery',
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 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 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 int $num Integer > 0; invalid numbers will be ignored
211 */
212 public function setWidths( $num ) {
213 if ( $num > 0 ) {
214 $this->mWidths = (int)$num;
215 }
216 }
217
218 /**
219 * Set how high each image will be, in pixels.
220 *
221 * @param int $num Integer > 0; invalid numbers will be ignored
222 */
223 public function setHeights( $num ) {
224 if ( $num > 0 ) {
225 $this->mHeights = (int)$num;
226 }
227 }
228
229 /**
230 * Allow setting additional options. This is meant
231 * to allow extensions to add additional parameters to
232 * <gallery> parser tag.
233 *
234 * @param array $options Attributes of gallery tag
235 */
236 public function setAdditionalOptions( $options ) {
237 }
238
239 /**
240 * Add an image to the gallery.
241 *
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)
248 */
249 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
250 if ( $title instanceof File ) {
251 // Old calling convention
252 $title = $title->getTitle();
253 }
254 $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
255 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
256 }
257
258 /**
259 * Add an image at the beginning of the gallery.
260 *
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)
267 */
268 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
269 if ( $title instanceof File ) {
270 // Old calling convention
271 $title = $title->getTitle();
272 }
273 array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
274 }
275
276 /**
277 * Returns the list of images this gallery contains
278 * @return array
279 */
280 public function getImages() {
281 return $this->mImages;
282 }
283
284 /**
285 * isEmpty() returns true if the gallery contains no images
286 * @return bool
287 */
288 function isEmpty() {
289 return empty( $this->mImages );
290 }
291
292 /**
293 * Enable/Disable showing of the dimensions of an image in the gallery.
294 * Enabled by default.
295 *
296 * @param bool $f Set to false to disable
297 */
298 function setShowDimensions( $f ) {
299 $this->mShowDimensions = (bool)$f;
300 }
301
302 /**
303 * Enable/Disable showing of the file size of an image in the gallery.
304 * Enabled by default.
305 *
306 * @param bool $f Set to false to disable
307 */
308 function setShowBytes( $f ) {
309 $this->mShowBytes = (bool)$f;
310 }
311
312 /**
313 * Enable/Disable showing of the filename of an image in the gallery.
314 * Enabled by default.
315 *
316 * @param bool $f Set to false to disable
317 */
318 function setShowFilename( $f ) {
319 $this->mShowFilename = (bool)$f;
320 }
321
322 /**
323 * Set arbitrary attributes to go on the HTML gallery output element.
324 * Should be suitable for a <ul> element.
325 *
326 * Note -- if taking from user input, you should probably run through
327 * Sanitizer::validateAttributes() first.
328 *
329 * @param array $attribs Array of HTML attribute pairs
330 */
331 function setAttributes( $attribs ) {
332 $this->mAttribs = $attribs;
333 }
334
335 /**
336 * Display an html representation of the gallery
337 *
338 * @return string The html
339 */
340 abstract public function toHTML();
341
342 /**
343 * @return int Number of images in the gallery
344 */
345 public function count() {
346 return count( $this->mImages );
347 }
348
349 /**
350 * Set the contextual title
351 *
352 * @param Title $title Contextual title
353 */
354 public function setContextTitle( $title ) {
355 $this->contextTitle = $title;
356 }
357
358 /**
359 * Get the contextual title, if applicable
360 *
361 * @return Title|bool Title or false
362 */
363 public function getContextTitle() {
364 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
365 ? $this->contextTitle
366 : false;
367 }
368
369 /**
370 * Determines the correct language to be used for this image gallery
371 * @return Language
372 */
373 protected function getRenderLang() {
374 return $this->mParser
375 ? $this->mParser->getTargetLanguage()
376 : $this->getLanguage();
377 }
378 }