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
90 * @return ImageGalleryBase
93 static function factory( $mode = false, IContextSource
$context = null ) {
97 $context = RequestContext
::getMainAndWarn( __METHOD__
);
100 $galleryOpions = $context->getConfig()->get( 'GalleryOptions' );
101 $mode = $galleryOpions['mode'];
104 $mode = $wgContLang->lc( $mode );
106 if ( isset( self
::$modeMapping[$mode] ) ) {
107 return new self
::$modeMapping[$mode]( $mode, $context );
109 throw new MWException( "No gallery class registered for mode $mode" );
113 private static function loadModes() {
114 if ( self
::$modeMapping === false ) {
115 self
::$modeMapping = array(
116 'traditional' => 'TraditionalImageGallery',
117 'nolines' => 'NolinesImageGallery',
118 'packed' => 'PackedImageGallery',
119 'packed-hover' => 'PackedHoverImageGallery',
120 'packed-overlay' => 'PackedOverlayImageGallery',
122 // Allow extensions to make a new gallery format.
123 Hooks
::run( 'GalleryGetModes', array( &self
::$modeMapping ) );
128 * Create a new image gallery object.
130 * You should not call this directly, but instead use
131 * ImageGalleryBase::factory().
132 * @param string $mode
133 * @param IContextSource|null $context
135 function __construct( $mode = 'traditional', IContextSource
$context = null ) {
137 $this->setContext( $context );
140 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
141 $this->mImages
= array();
142 $this->mShowBytes
= $galleryOptions['showBytes'];
143 $this->mShowFilename
= true;
144 $this->mParser
= false;
145 $this->mHideBadImages
= false;
146 $this->mPerRow
= $galleryOptions['imagesPerRow'];
147 $this->mWidths
= $galleryOptions['imageWidth'];
148 $this->mHeights
= $galleryOptions['imageHeight'];
149 $this->mCaptionLength
= $galleryOptions['captionLength'];
150 $this->mMode
= $mode;
154 * Register a parser object. If you do not set this
155 * and the output of this gallery ends up in parser
156 * cache, the javascript will break!
158 * @note This also triggers using the page's target
159 * language instead of the user language.
161 * @param Parser $parser
163 function setParser( $parser ) {
164 $this->mParser
= $parser;
171 function setHideBadImages( $flag = true ) {
172 $this->mHideBadImages
= $flag;
176 * Set the caption (as plain text)
178 * @param string $caption Caption
180 function setCaption( $caption ) {
181 $this->mCaption
= htmlspecialchars( $caption );
185 * Set the caption (as HTML)
187 * @param string $caption Caption
189 public function setCaptionHtml( $caption ) {
190 $this->mCaption
= $caption;
194 * Set how many images will be displayed per row.
196 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
197 * to screensize invalid numbers will be rejected
199 public function setPerRow( $num ) {
201 $this->mPerRow
= (int)$num;
206 * Set how wide each image will be, in pixels.
208 * @param int $num Integer > 0; invalid numbers will be ignored
210 public function setWidths( $num ) {
212 $this->mWidths
= (int)$num;
217 * Set how high each image will be, in pixels.
219 * @param int $num Integer > 0; invalid numbers will be ignored
221 public function setHeights( $num ) {
223 $this->mHeights
= (int)$num;
228 * Allow setting additional options. This is meant
229 * to allow extensions to add additional parameters to
230 * <gallery> parser tag.
232 * @param array $options Attributes of gallery tag
234 public function setAdditionalOptions( $options ) {
238 * Add an image to the gallery.
240 * @param Title $title Title object of the image that is added to the gallery
241 * @param string $html Additional HTML text to be shown. The name and size
242 * of the image are always shown.
243 * @param string $alt Alt text for the image
244 * @param string $link Override image link (optional)
245 * @param array $handlerOpts Array of options for image handler (aka page number)
247 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
248 if ( $title instanceof File
) {
249 // Old calling convention
250 $title = $title->getTitle();
252 $this->mImages
[] = array( $title, $html, $alt, $link, $handlerOpts );
253 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
257 * Add an image at the beginning of the gallery.
259 * @param Title $title Title object of the image that is added to the gallery
260 * @param string $html Additional HTML text to be shown. The name and size
261 * of the image are always shown.
262 * @param string $alt Alt text for the image
263 * @param string $link Override image link (optional)
264 * @param array $handlerOpts Array of options for image handler (aka page number)
266 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
267 if ( $title instanceof File
) {
268 // Old calling convention
269 $title = $title->getTitle();
271 array_unshift( $this->mImages
, array( &$title, $html, $alt, $link, $handlerOpts ) );
275 * Returns the list of images this gallery contains
278 public function getImages() {
279 return $this->mImages
;
283 * isEmpty() returns true if the gallery contains no images
287 return empty( $this->mImages
);
291 * Enable/Disable showing of the file size of an image in the gallery.
292 * Enabled by default.
294 * @param bool $f Set to false to disable
296 function setShowBytes( $f ) {
297 $this->mShowBytes
= (bool)$f;
301 * Enable/Disable showing of the filename of an image in the gallery.
302 * Enabled by default.
304 * @param bool $f Set to false to disable
306 function setShowFilename( $f ) {
307 $this->mShowFilename
= (bool)$f;
311 * Set arbitrary attributes to go on the HTML gallery output element.
312 * Should be suitable for a <ul> element.
314 * Note -- if taking from user input, you should probably run through
315 * Sanitizer::validateAttributes() first.
317 * @param array $attribs Array of HTML attribute pairs
319 function setAttributes( $attribs ) {
320 $this->mAttribs
= $attribs;
324 * Display an html representation of the gallery
326 * @return string The html
328 abstract public function toHTML();
331 * @return int Number of images in the gallery
333 public function count() {
334 return count( $this->mImages
);
338 * Set the contextual title
340 * @param Title $title Contextual title
342 public function setContextTitle( $title ) {
343 $this->contextTitle
= $title;
347 * Get the contextual title, if applicable
349 * @return Title|bool Title or false
351 public function getContextTitle() {
352 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
353 ?
$this->contextTitle
358 * Determines the correct language to be used for this image gallery
361 protected function getRenderLang() {
362 return $this->mParser
363 ?
$this->mParser
->getTargetLanguage()
364 : $this->getLanguage();