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