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