Merge "Typography update to Vector skin"
[mediawiki.git] / includes / gallery / ImageGalleryBase.php
blobd2e4689f98e5a4f70d01c1fd98821f2183a26500
1 <?php
2 /**
3 * Image gallery.
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
20 * @file
23 /**
24 * Image gallery
26 * Add images to the gallery using add(), then render that list to HTML using toHTML().
28 * @ingroup Media
30 abstract class ImageGalleryBase extends ContextSource {
31 /** @var array Gallery images */
32 protected $mImages;
34 /** @var bool Whether to show the filesize in bytes in categories */
35 protected $mShowBytes;
37 /** @var bool Whether to show the filename. Default: true */
38 protected $mShowFilename;
40 /** @var string Gallery mode. Default: traditional */
41 protected $mMode;
43 /** @var bool|string Gallery caption. Default: false */
44 protected $mCaption = false;
46 /**
47 * @var bool Hide blacklisted images?
49 protected $mHideBadImages;
51 /**
52 * @var Parser Registered parser object for output callbacks
54 protected $mParser;
56 /**
57 * @var Title Contextual title, used when images are being screened against
58 * the bad image list
60 protected $contextTitle = false;
62 /** @var array */
63 protected $mAttribs = array();
65 /** @var bool */
66 static private $modeMapping = false;
68 /**
69 * Get a new image gallery. This is the method other callers
70 * should use to get a gallery.
72 * @param string|bool $mode Mode to use. False to use the default.
73 * @throws MWException
75 static function factory( $mode = false ) {
76 global $wgGalleryOptions, $wgContLang;
77 self::loadModes();
78 if ( !$mode ) {
79 $mode = $wgGalleryOptions['mode'];
82 $mode = $wgContLang->lc( $mode );
84 if ( isset( self::$modeMapping[$mode] ) ) {
85 return new self::$modeMapping[$mode]( $mode );
86 } else {
87 throw new MWException( "No gallery class registered for mode $mode" );
91 private static function loadModes() {
92 if ( self::$modeMapping === false ) {
93 self::$modeMapping = array(
94 'traditional' => 'TraditionalImageGallery',
95 'nolines' => 'NolinesImageGallery',
96 'packed' => 'PackedImageGallery',
97 'packed-hover' => 'PackedHoverImageGallery',
98 'packed-overlay' => 'PackedOverlayImageGallery',
100 // Allow extensions to make a new gallery format.
101 wfRunHooks( 'GalleryGetModes', self::$modeMapping );
106 * Create a new image gallery object.
108 * You should not call this directly, but instead use
109 * ImageGalleryBase::factory().
111 function __construct( $mode = 'traditional' ) {
112 global $wgGalleryOptions;
113 $this->mImages = array();
114 $this->mShowBytes = $wgGalleryOptions['showBytes'];
115 $this->mShowFilename = true;
116 $this->mParser = false;
117 $this->mHideBadImages = false;
118 $this->mPerRow = $wgGalleryOptions['imagesPerRow'];
119 $this->mWidths = $wgGalleryOptions['imageWidth'];
120 $this->mHeights = $wgGalleryOptions['imageHeight'];
121 $this->mCaptionLength = $wgGalleryOptions['captionLength'];
122 $this->mMode = $mode;
126 * Register a parser object. If you do not set this
127 * and the output of this gallery ends up in parser
128 * cache, the javascript will break!
130 * @note This also triggers using the page's target
131 * language instead of the user language.
133 * @param $parser Parser
135 function setParser( $parser ) {
136 $this->mParser = $parser;
140 * Set bad image flag
142 function setHideBadImages( $flag = true ) {
143 $this->mHideBadImages = $flag;
147 * Set the caption (as plain text)
149 * @param string $caption Caption
151 function setCaption( $caption ) {
152 $this->mCaption = htmlspecialchars( $caption );
156 * Set the caption (as HTML)
158 * @param string $caption Caption
160 public function setCaptionHtml( $caption ) {
161 $this->mCaption = $caption;
165 * Set how many images will be displayed per row.
167 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
168 * to screensize invalid numbers will be rejected
170 public function setPerRow( $num ) {
171 if ( $num >= 0 ) {
172 $this->mPerRow = (int)$num;
177 * Set how wide each image will be, in pixels.
179 * @param int $num Integer > 0; invalid numbers will be ignored
181 public function setWidths( $num ) {
182 if ( $num > 0 ) {
183 $this->mWidths = (int)$num;
188 * Set how high each image will be, in pixels.
190 * @param int $num Integer > 0; invalid numbers will be ignored
192 public function setHeights( $num ) {
193 if ( $num > 0 ) {
194 $this->mHeights = (int)$num;
199 * Allow setting additional options. This is meant
200 * to allow extensions to add additional parameters to
201 * <gallery> parser tag.
203 * @param array $options Attributes of gallery tag.
205 public function setAdditionalOptions( $options ) {
209 * Instruct the class to use a specific skin for rendering
211 * @param Skin $skin
212 * @deprecated since 1.18 Not used anymore
214 function useSkin( $skin ) {
215 wfDeprecated( __METHOD__, '1.18' );
216 /* no op */
220 * Add an image to the gallery.
222 * @param Title $title Title object of the image that is added to the gallery
223 * @param string $html Additional HTML text to be shown. The name and size
224 * of the image are always shown.
225 * @param string $alt Alt text for the image
226 * @param string $link Override image link (optional)
227 * @param array $handlerOpts Array of options for image handler (aka page number)
229 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
230 if ( $title instanceof File ) {
231 // Old calling convention
232 $title = $title->getTitle();
234 $this->mImages[] = array( $title, $html, $alt, $link, $handlerOpts );
235 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
239 * Add an image at the beginning of the gallery.
241 * @param Title $title Title object of the image that is added to the gallery
242 * @param string $html Additional HTML text to be shown. The name and size
243 * of the image are always shown.
244 * @param string $alt Alt text for the image
245 * @param string $link Override image link (optional)
246 * @param array $handlerOpts Array of options for image handler (aka page number)
248 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
249 if ( $title instanceof File ) {
250 // Old calling convention
251 $title = $title->getTitle();
253 array_unshift( $this->mImages, array( &$title, $html, $alt, $link, $handlerOpts ) );
257 * isEmpty() returns true if the gallery contains no images
258 * @return bool
260 function isEmpty() {
261 return empty( $this->mImages );
265 * Enable/Disable showing of the file size of an image in the gallery.
266 * Enabled by default.
268 * @param bool $f Set to false to disable.
270 function setShowBytes( $f ) {
271 $this->mShowBytes = (bool)$f;
275 * Enable/Disable showing of the filename of an image in the gallery.
276 * Enabled by default.
278 * @param bool $f Set to false to disable.
280 function setShowFilename( $f ) {
281 $this->mShowFilename = (bool)$f;
285 * Set arbitrary attributes to go on the HTML gallery output element.
286 * Should be suitable for a <ul> element.
288 * Note -- if taking from user input, you should probably run through
289 * Sanitizer::validateAttributes() first.
291 * @param array $attribs Array of HTML attribute pairs
293 function setAttributes( $attribs ) {
294 $this->mAttribs = $attribs;
298 * Display an html representation of the gallery
300 * @return string The html
302 abstract public function toHTML();
305 * @return int Number of images in the gallery
307 public function count() {
308 return count( $this->mImages );
312 * Set the contextual title
314 * @param Title $title Contextual title
316 public function setContextTitle( $title ) {
317 $this->contextTitle = $title;
321 * Get the contextual title, if applicable
323 * @return Title|bool Title or false
325 public function getContextTitle() {
326 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
327 ? $this->contextTitle
328 : false;
332 * Determines the correct language to be used for this image gallery
333 * @return Language
335 protected function getRenderLang() {
336 return $this->mParser
337 ? $this->mParser->getTargetLanguage()
338 : $this->getLanguage();