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
37 * @var bool Whether to show the filesize in bytes in categories
39 protected $mShowBytes;
42 * @var bool Whether to show the filename. Default: true
44 protected $mShowFilename;
47 * @var string Gallery mode. Default: traditional
52 * @var bool|string Gallery caption. Default: false
54 protected $mCaption = false;
57 * @var bool Hide blacklisted images?
59 protected $mHideBadImages;
62 * @var Parser Registered parser object for output callbacks
67 * @var Title Contextual title, used when images are being screened against
70 protected $contextTitle = false;
73 protected $mAttribs = array();
76 static private $modeMapping = false;
79 * Get a new image gallery. This is the method other callers
80 * should use to get a gallery.
82 * @param string|bool $mode Mode to use. False to use the default
83 * @param IContextSource|null $context
84 * @return ImageGalleryBase
87 static function factory( $mode = false, IContextSource
$context = null ) {
91 $context = RequestContext
::getMainAndWarn( __METHOD__
);
94 $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
95 $mode = $galleryOptions['mode'];
98 $mode = $wgContLang->lc( $mode );
100 if ( isset( self
::$modeMapping[$mode] ) ) {
101 $class = self
::$modeMapping[$mode];
102 return new $class( $mode, $context );
104 throw new MWException( "No gallery class registered for mode $mode" );
108 private static function loadModes() {
109 if ( self
::$modeMapping === false ) {
110 self
::$modeMapping = array(
111 'traditional' => 'TraditionalImageGallery',
112 'nolines' => 'NolinesImageGallery',
113 'packed' => 'PackedImageGallery',
114 'packed-hover' => 'PackedHoverImageGallery',
115 'packed-overlay' => 'PackedOverlayImageGallery',
117 // Allow extensions to make a new gallery format.
118 Hooks
::run( 'GalleryGetModes', array( &self
::$modeMapping ) );
123 * Create a new image gallery object.
125 * You should not call this directly, but instead use
126 * ImageGalleryBase::factory().
127 * @param string $mode
128 * @param IContextSource|null $context
130 function __construct( $mode = 'traditional', IContextSource
$context = null ) {
132 $this->setContext( $context );
135 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
136 $this->mImages
= array();
137 $this->mShowBytes
= $galleryOptions['showBytes'];
138 $this->mShowFilename
= true;
139 $this->mParser
= false;
140 $this->mHideBadImages
= false;
141 $this->mPerRow
= $galleryOptions['imagesPerRow'];
142 $this->mWidths
= $galleryOptions['imageWidth'];
143 $this->mHeights
= $galleryOptions['imageHeight'];
144 $this->mCaptionLength
= $galleryOptions['captionLength'];
145 $this->mMode
= $mode;
149 * Register a parser object. If you do not set this
150 * and the output of this gallery ends up in parser
151 * cache, the javascript will break!
153 * @note This also triggers using the page's target
154 * language instead of the user language.
156 * @param Parser $parser
158 function setParser( $parser ) {
159 $this->mParser
= $parser;
166 function setHideBadImages( $flag = true ) {
167 $this->mHideBadImages
= $flag;
171 * Set the caption (as plain text)
173 * @param string $caption Caption
175 function setCaption( $caption ) {
176 $this->mCaption
= htmlspecialchars( $caption );
180 * Set the caption (as HTML)
182 * @param string $caption Caption
184 public function setCaptionHtml( $caption ) {
185 $this->mCaption
= $caption;
189 * Set how many images will be displayed per row.
191 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
192 * to screensize invalid numbers will be rejected
194 public function setPerRow( $num ) {
196 $this->mPerRow
= (int)$num;
201 * Set how wide each image will be, in pixels.
203 * @param int $num Integer > 0; invalid numbers will be ignored
205 public function setWidths( $num ) {
207 $this->mWidths
= (int)$num;
212 * Set how high each image will be, in pixels.
214 * @param int $num Integer > 0; invalid numbers will be ignored
216 public function setHeights( $num ) {
218 $this->mHeights
= (int)$num;
223 * Allow setting additional options. This is meant
224 * to allow extensions to add additional parameters to
225 * <gallery> parser tag.
227 * @param array $options Attributes of gallery tag
229 public function setAdditionalOptions( $options ) {
233 * Add an image to the gallery.
235 * @param Title $title Title object of the image that is added to the gallery
236 * @param string $html Additional HTML text to be shown. The name and size
237 * of the image are always shown.
238 * @param string $alt Alt text for the image
239 * @param string $link Override image link (optional)
240 * @param array $handlerOpts Array of options for image handler (aka page number)
242 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
243 if ( $title instanceof File
) {
244 // Old calling convention
245 $title = $title->getTitle();
247 $this->mImages
[] = array( $title, $html, $alt, $link, $handlerOpts );
248 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
252 * Add an image at the beginning of the gallery.
254 * @param Title $title Title object of the image that is added to the gallery
255 * @param string $html Additional HTML text to be shown. The name and size
256 * of the image are always shown.
257 * @param string $alt Alt text for the image
258 * @param string $link Override image link (optional)
259 * @param array $handlerOpts Array of options for image handler (aka page number)
261 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
262 if ( $title instanceof File
) {
263 // Old calling convention
264 $title = $title->getTitle();
266 array_unshift( $this->mImages
, array( &$title, $html, $alt, $link, $handlerOpts ) );
270 * Returns the list of images this gallery contains
273 public function getImages() {
274 return $this->mImages
;
278 * isEmpty() returns true if the gallery contains no images
282 return empty( $this->mImages
);
286 * Enable/Disable showing of the file size of an image in the gallery.
287 * Enabled by default.
289 * @param bool $f Set to false to disable
291 function setShowBytes( $f ) {
292 $this->mShowBytes
= (bool)$f;
296 * Enable/Disable showing of the filename of an image in the gallery.
297 * Enabled by default.
299 * @param bool $f Set to false to disable
301 function setShowFilename( $f ) {
302 $this->mShowFilename
= (bool)$f;
306 * Set arbitrary attributes to go on the HTML gallery output element.
307 * Should be suitable for a <ul> element.
309 * Note -- if taking from user input, you should probably run through
310 * Sanitizer::validateAttributes() first.
312 * @param array $attribs Array of HTML attribute pairs
314 function setAttributes( $attribs ) {
315 $this->mAttribs
= $attribs;
319 * Display an html representation of the gallery
321 * @return string The html
323 abstract public function toHTML();
326 * @return int Number of images in the gallery
328 public function count() {
329 return count( $this->mImages
);
333 * Set the contextual title
335 * @param Title $title Contextual title
337 public function setContextTitle( $title ) {
338 $this->contextTitle
= $title;
342 * Get the contextual title, if applicable
344 * @return Title|bool Title or false
346 public function getContextTitle() {
347 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
348 ?
$this->contextTitle
353 * Determines the correct language to be used for this image gallery
356 protected function getRenderLang() {
357 return $this->mParser
358 ?
$this->mParser
->getTargetLanguage()
359 : $this->getLanguage();