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