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