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