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.
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.
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
26 * Add images to the gallery using add(), then render that list to HTML using toHTML().
30 abstract class ImageGalleryBase
extends ContextSource
{
32 * @var array Gallery images
33 * @deprecated in 1.23 (was declared "var") and will be removed in 1.24
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
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
47 public $mShowFilename;
50 * @var string Gallery mode. Default: traditional
51 * @deprecated in 1.23 (was declared "var") and will be removed in 1.24
56 * @var bool|string Gallery caption. Default: false
57 * @deprecated in 1.23 (was declared "var") and will be removed in 1.24
59 public $mCaption = false;
62 * @var bool Hide blacklisted images?
63 * @deprecated in 1.23 (was declared "var") and will be removed in 1.24
65 public $mHideBadImages;
68 * @var Parser Registered parser object for output callbacks
73 * @var Title Contextual title, used when images are being screened against
76 protected $contextTitle = false;
79 protected $mAttribs = array();
82 static private $modeMapping = false;
85 * Get a new image gallery. This is the method other callers
86 * should use to get a gallery.
88 * @param string|bool $mode Mode to use. False to use the default
91 static function factory( $mode = false ) {
92 global $wgGalleryOptions, $wgContLang;
95 $mode = $wgGalleryOptions['mode'];
98 $mode = $wgContLang->lc( $mode );
100 if ( isset( self
::$modeMapping[$mode] ) ) {
101 return new self
::$modeMapping[$mode]( $mode );
103 throw new MWException( "No gallery class registered for mode $mode" );
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',
116 // Allow extensions to make a new gallery format.
117 wfRunHooks( 'GalleryGetModes', self
::$modeMapping );
122 * Create a new image gallery object.
124 * You should not call this directly, but instead use
125 * ImageGalleryBase::factory().
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;
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!
146 * @note This also triggers using the page's target
147 * language instead of the user language.
149 * @param Parser $parser
151 function setParser( $parser ) {
152 $this->mParser
= $parser;
158 function setHideBadImages( $flag = true ) {
159 $this->mHideBadImages
= $flag;
163 * Set the caption (as plain text)
165 * @param string $caption Caption
167 function setCaption( $caption ) {
168 $this->mCaption
= htmlspecialchars( $caption );
172 * Set the caption (as HTML)
174 * @param string $caption Caption
176 public function setCaptionHtml( $caption ) {
177 $this->mCaption
= $caption;
181 * Set how many images will be displayed per row.
183 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
184 * to screensize invalid numbers will be rejected
186 public function setPerRow( $num ) {
188 $this->mPerRow
= (int)$num;
193 * Set how wide each image will be, in pixels.
195 * @param int $num Integer > 0; invalid numbers will be ignored
197 public function setWidths( $num ) {
199 $this->mWidths
= (int)$num;
204 * Set how high each image will be, in pixels.
206 * @param int $num Integer > 0; invalid numbers will be ignored
208 public function setHeights( $num ) {
210 $this->mHeights
= (int)$num;
215 * Allow setting additional options. This is meant
216 * to allow extensions to add additional parameters to
217 * <gallery> parser tag.
219 * @param array $options Attributes of gallery tag
221 public function setAdditionalOptions( $options ) {
225 * Instruct the class to use a specific skin for rendering
228 * @deprecated since 1.18 Not used anymore
230 function useSkin( $skin ) {
231 wfDeprecated( __METHOD__
, '1.18' );
236 * Add an image to the gallery.
238 * @param Title $title Title object of the image that is added to the gallery
239 * @param string $html Additional HTML text to be shown. The name and size
240 * of the image are always shown.
241 * @param string $alt Alt text for the image
242 * @param string $link Override image link (optional)
243 * @param array $handlerOpts Array of options for image handler (aka page number)
245 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
246 if ( $title instanceof File
) {
247 // Old calling convention
248 $title = $title->getTitle();
250 $this->mImages
[] = array( $title, $html, $alt, $link, $handlerOpts );
251 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
255 * Add an image at the beginning of the gallery.
257 * @param Title $title Title object of the image that is added to the gallery
258 * @param string $html Additional HTML text to be shown. The name and size
259 * of the image are always shown.
260 * @param string $alt Alt text for the image
261 * @param string $link Override image link (optional)
262 * @param array $handlerOpts Array of options for image handler (aka page number)
264 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
265 if ( $title instanceof File
) {
266 // Old calling convention
267 $title = $title->getTitle();
269 array_unshift( $this->mImages
, array( &$title, $html, $alt, $link, $handlerOpts ) );
273 * isEmpty() returns true if the gallery contains no images
277 return empty( $this->mImages
);
281 * Enable/Disable showing of the file size of an image in the gallery.
282 * Enabled by default.
284 * @param bool $f Set to false to disable
286 function setShowBytes( $f ) {
287 $this->mShowBytes
= (bool)$f;
291 * Enable/Disable showing of the filename of an image in the gallery.
292 * Enabled by default.
294 * @param bool $f Set to false to disable
296 function setShowFilename( $f ) {
297 $this->mShowFilename
= (bool)$f;
301 * Set arbitrary attributes to go on the HTML gallery output element.
302 * Should be suitable for a <ul> element.
304 * Note -- if taking from user input, you should probably run through
305 * Sanitizer::validateAttributes() first.
307 * @param array $attribs Array of HTML attribute pairs
309 function setAttributes( $attribs ) {
310 $this->mAttribs
= $attribs;
314 * Display an html representation of the gallery
316 * @return string The html
318 abstract public function toHTML();
321 * @return int Number of images in the gallery
323 public function count() {
324 return count( $this->mImages
);
328 * Set the contextual title
330 * @param Title $title Contextual title
332 public function setContextTitle( $title ) {
333 $this->contextTitle
= $title;
337 * Get the contextual title, if applicable
339 * @return Title|bool Title or false
341 public function getContextTitle() {
342 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
343 ?
$this->contextTitle
348 * Determines the correct language to be used for this image gallery
351 protected function getRenderLang() {
352 return $this->mParser
353 ?
$this->mParser
->getTargetLanguage()
354 : $this->getLanguage();