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