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