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 since 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 since 1.23 (was declared "var") and will be removed in 1.24
44 * @var bool Whether to show the filename. Default: true
45 * @deprecated since 1.23 (was declared "var") and will be removed in 1.24
47 public $mShowFilename;
50 * @var string Gallery mode. Default: traditional
51 * @deprecated since 1.23 (was declared "var") and will be removed in 1.24
56 * @var bool|string Gallery caption. Default: false
57 * @deprecated since 1.23 (was declared "var") and will be removed in 1.24
59 public $mCaption = false;
62 * @var bool Hide blacklisted images?
63 * @deprecated since 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
89 * @param IContextSource|null $context
92 static function factory( $mode = false, IContextSource
$context = null ) {
96 $context = RequestContext
::getMainAndWarn( __METHOD__
);
99 $galleryOpions = $context->getConfig()->get( 'GalleryOptions' );
100 $mode = $galleryOpions['mode'];
103 $mode = $wgContLang->lc( $mode );
105 if ( isset( self
::$modeMapping[$mode] ) ) {
106 return new self
::$modeMapping[$mode]( $mode, $context );
108 throw new MWException( "No gallery class registered for mode $mode" );
112 private static function loadModes() {
113 if ( self
::$modeMapping === false ) {
114 self
::$modeMapping = array(
115 'traditional' => 'TraditionalImageGallery',
116 'nolines' => 'NolinesImageGallery',
117 'packed' => 'PackedImageGallery',
118 'packed-hover' => 'PackedHoverImageGallery',
119 'packed-overlay' => 'PackedOverlayImageGallery',
121 // Allow extensions to make a new gallery format.
122 wfRunHooks( 'GalleryGetModes', self
::$modeMapping );
127 * Create a new image gallery object.
129 * You should not call this directly, but instead use
130 * ImageGalleryBase::factory().
131 * @param string $mode
132 * @param IContextSource|null $context
134 function __construct( $mode = 'traditional', IContextSource
$context = null ) {
136 $this->setContext( $context );
139 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
140 $this->mImages
= array();
141 $this->mShowBytes
= $galleryOptions['showBytes'];
142 $this->mShowFilename
= true;
143 $this->mParser
= false;
144 $this->mHideBadImages
= false;
145 $this->mPerRow
= $galleryOptions['imagesPerRow'];
146 $this->mWidths
= $galleryOptions['imageWidth'];
147 $this->mHeights
= $galleryOptions['imageHeight'];
148 $this->mCaptionLength
= $galleryOptions['captionLength'];
149 $this->mMode
= $mode;
153 * Register a parser object. If you do not set this
154 * and the output of this gallery ends up in parser
155 * cache, the javascript will break!
157 * @note This also triggers using the page's target
158 * language instead of the user language.
160 * @param Parser $parser
162 function setParser( $parser ) {
163 $this->mParser
= $parser;
170 function setHideBadImages( $flag = true ) {
171 $this->mHideBadImages
= $flag;
175 * Set the caption (as plain text)
177 * @param string $caption Caption
179 function setCaption( $caption ) {
180 $this->mCaption
= htmlspecialchars( $caption );
184 * Set the caption (as HTML)
186 * @param string $caption Caption
188 public function setCaptionHtml( $caption ) {
189 $this->mCaption
= $caption;
193 * Set how many images will be displayed per row.
195 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
196 * to screensize invalid numbers will be rejected
198 public function setPerRow( $num ) {
200 $this->mPerRow
= (int)$num;
205 * Set how wide each image will be, in pixels.
207 * @param int $num Integer > 0; invalid numbers will be ignored
209 public function setWidths( $num ) {
211 $this->mWidths
= (int)$num;
216 * Set how high each image will be, in pixels.
218 * @param int $num Integer > 0; invalid numbers will be ignored
220 public function setHeights( $num ) {
222 $this->mHeights
= (int)$num;
227 * Allow setting additional options. This is meant
228 * to allow extensions to add additional parameters to
229 * <gallery> parser tag.
231 * @param array $options Attributes of gallery tag
233 public function setAdditionalOptions( $options ) {
237 * Add an image to the gallery.
239 * @param Title $title Title object of the image that is added to the gallery
240 * @param string $html Additional HTML text to be shown. The name and size
241 * of the image are always shown.
242 * @param string $alt Alt text for the image
243 * @param string $link Override image link (optional)
244 * @param array $handlerOpts Array of options for image handler (aka page number)
246 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
247 if ( $title instanceof File
) {
248 // Old calling convention
249 $title = $title->getTitle();
251 $this->mImages
[] = array( $title, $html, $alt, $link, $handlerOpts );
252 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
256 * Add an image at the beginning of the gallery.
258 * @param Title $title Title object of the image that is added to the gallery
259 * @param string $html Additional HTML text to be shown. The name and size
260 * of the image are always shown.
261 * @param string $alt Alt text for the image
262 * @param string $link Override image link (optional)
263 * @param array $handlerOpts Array of options for image handler (aka page number)
265 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
266 if ( $title instanceof File
) {
267 // Old calling convention
268 $title = $title->getTitle();
270 array_unshift( $this->mImages
, array( &$title, $html, $alt, $link, $handlerOpts ) );
274 * Returns the list of images this gallery contains
277 public function getImages() {
278 return $this->mImages
;
282 * isEmpty() returns true if the gallery contains no images
286 return empty( $this->mImages
);
290 * Enable/Disable showing of the file size of an image in the gallery.
291 * Enabled by default.
293 * @param bool $f Set to false to disable
295 function setShowBytes( $f ) {
296 $this->mShowBytes
= (bool)$f;
300 * Enable/Disable showing of the filename of an image in the gallery.
301 * Enabled by default.
303 * @param bool $f Set to false to disable
305 function setShowFilename( $f ) {
306 $this->mShowFilename
= (bool)$f;
310 * Set arbitrary attributes to go on the HTML gallery output element.
311 * Should be suitable for a <ul> element.
313 * Note -- if taking from user input, you should probably run through
314 * Sanitizer::validateAttributes() first.
316 * @param array $attribs Array of HTML attribute pairs
318 function setAttributes( $attribs ) {
319 $this->mAttribs
= $attribs;
323 * Display an html representation of the gallery
325 * @return string The html
327 abstract public function toHTML();
330 * @return int Number of images in the gallery
332 public function count() {
333 return count( $this->mImages
);
337 * Set the contextual title
339 * @param Title $title Contextual title
341 public function setContextTitle( $title ) {
342 $this->contextTitle
= $title;
346 * Get the contextual title, if applicable
348 * @return Title|bool Title or false
350 public function getContextTitle() {
351 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
352 ?
$this->contextTitle
357 * Determines the correct language to be used for this image gallery
360 protected function getRenderLang() {
361 return $this->mParser
362 ?
$this->mParser
->getTargetLanguage()
363 : $this->getLanguage();