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