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 * Add an image to the gallery.
227 * @param Title $title Title object of the image that is added to the gallery
228 * @param string $html Additional HTML text to be shown. The name and size
229 * of the image are always shown.
230 * @param string $alt Alt text for the image
231 * @param string $link Override image link (optional)
232 * @param array $handlerOpts Array of options for image handler (aka page number)
234 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
235 if ( $title instanceof File
) {
236 // Old calling convention
237 $title = $title->getTitle();
239 $this->mImages
[] = array( $title, $html, $alt, $link, $handlerOpts );
240 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
244 * Add an image at the beginning of the gallery.
246 * @param Title $title Title object of the image that is added to the gallery
247 * @param string $html Additional HTML text to be shown. The name and size
248 * of the image are always shown.
249 * @param string $alt Alt text for the image
250 * @param string $link Override image link (optional)
251 * @param array $handlerOpts Array of options for image handler (aka page number)
253 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
254 if ( $title instanceof File
) {
255 // Old calling convention
256 $title = $title->getTitle();
258 array_unshift( $this->mImages
, array( &$title, $html, $alt, $link, $handlerOpts ) );
262 * Returns the list of images this gallery contains
265 public function getImages() {
266 return $this->mImages
;
270 * isEmpty() returns true if the gallery contains no images
274 return empty( $this->mImages
);
278 * Enable/Disable showing of the file size of an image in the gallery.
279 * Enabled by default.
281 * @param bool $f Set to false to disable
283 function setShowBytes( $f ) {
284 $this->mShowBytes
= (bool)$f;
288 * Enable/Disable showing of the filename of an image in the gallery.
289 * Enabled by default.
291 * @param bool $f Set to false to disable
293 function setShowFilename( $f ) {
294 $this->mShowFilename
= (bool)$f;
298 * Set arbitrary attributes to go on the HTML gallery output element.
299 * Should be suitable for a <ul> element.
301 * Note -- if taking from user input, you should probably run through
302 * Sanitizer::validateAttributes() first.
304 * @param array $attribs Array of HTML attribute pairs
306 function setAttributes( $attribs ) {
307 $this->mAttribs
= $attribs;
311 * Display an html representation of the gallery
313 * @return string The html
315 abstract public function toHTML();
318 * @return int Number of images in the gallery
320 public function count() {
321 return count( $this->mImages
);
325 * Set the contextual title
327 * @param Title $title Contextual title
329 public function setContextTitle( $title ) {
330 $this->contextTitle
= $title;
334 * Get the contextual title, if applicable
336 * @return Title|bool Title or false
338 public function getContextTitle() {
339 return is_object( $this->contextTitle
) && $this->contextTitle
instanceof Title
340 ?
$this->contextTitle
345 * Determines the correct language to be used for this image gallery
348 protected function getRenderLang() {
349 return $this->mParser
350 ?
$this->mParser
->getTargetLanguage()
351 : $this->getLanguage();