Merge "Default is not necessary for toggle fields"
[mediawiki.git] / includes / gallery / ImageGalleryBase.php
blobf8b8c505b145318910c9f40e3b0677c49651b073
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 $mImages, $mShowBytes, $mShowFilename, $mMode;
32 var $mCaption = false;
34 /**
35 * Hide blacklisted images?
37 var $mHideBadImages;
39 /**
40 * Registered parser object for output callbacks
41 * @var Parser
43 var $mParser;
45 /**
46 * Contextual title, used when images are being screened
47 * against the bad image list
49 protected $contextTitle = false;
51 protected $mAttribs = array();
53 static private $modeMapping = false;
55 /**
56 * Get a new image gallery. This is the method other callers
57 * should use to get a gallery.
59 * @param String|bool $mode Mode to use. False to use the default.
61 static function factory( $mode = false ) {
62 global $wgGalleryOptions, $wgContLang;
63 self::loadModes();
64 if ( !$mode ) {
65 $mode = $wgGalleryOptions['mode'];
68 $mode = $wgContLang->lc( $mode );
70 if ( isset( self::$modeMapping[$mode] ) ) {
71 return new self::$modeMapping[$mode]( $mode );
72 } else {
73 throw new MWException( "No gallery class registered for mode $mode" );
77 static private function loadModes() {
78 if ( self::$modeMapping === false ) {
79 self::$modeMapping = array(
80 'traditional' => 'TraditionalImageGallery',
81 'nolines' => 'NolinesImageGallery',
82 'packed' => 'PackedImageGallery',
83 'packed-hover' => 'PackedHoverImageGallery',
84 'packed-overlay' => 'PackedOverlayImageGallery',
86 // Allow extensions to make a new gallery format.
87 wfRunHooks( 'GalleryGetModes', self::$modeMapping );
91 /**
92 * Create a new image gallery object.
94 * You should not call this directly, but instead use
95 * ImageGalleryBase::factory().
97 function __construct( $mode = 'traditional' ) {
98 global $wgGalleryOptions;
99 $this->mImages = array();
100 $this->mShowBytes = $wgGalleryOptions['showBytes'];
101 $this->mShowFilename = true;
102 $this->mParser = false;
103 $this->mHideBadImages = false;
104 $this->mPerRow = $wgGalleryOptions['imagesPerRow'];
105 $this->mWidths = $wgGalleryOptions['imageWidth'];
106 $this->mHeights = $wgGalleryOptions['imageHeight'];
107 $this->mCaptionLength = $wgGalleryOptions['captionLength'];
108 $this->mMode = $mode;
112 * Register a parser object. If you do not set this
113 * and the output of this gallery ends up in parser
114 * cache, the javascript will break!
116 * @note This also triggers using the page's target
117 * language instead of the user language.
119 * @param $parser Parser
121 function setParser( $parser ) {
122 $this->mParser = $parser;
126 * Set bad image flag
128 function setHideBadImages( $flag = true ) {
129 $this->mHideBadImages = $flag;
133 * Set the caption (as plain text)
135 * @param string $caption Caption
137 function setCaption( $caption ) {
138 $this->mCaption = htmlspecialchars( $caption );
142 * Set the caption (as HTML)
144 * @param string $caption Caption
146 public function setCaptionHtml( $caption ) {
147 $this->mCaption = $caption;
151 * Set how many images will be displayed per row.
153 * @param $num Integer >= 0; If perrow=0 the gallery layout will adapt to screensize
154 * invalid numbers will be rejected
156 public function setPerRow( $num ) {
157 if ( $num >= 0 ) {
158 $this->mPerRow = (int)$num;
163 * Set how wide each image will be, in pixels.
165 * @param $num Integer > 0; invalid numbers will be ignored
167 public function setWidths( $num ) {
168 if ( $num > 0 ) {
169 $this->mWidths = (int)$num;
174 * Set how high each image will be, in pixels.
176 * @param $num Integer > 0; invalid numbers will be ignored
178 public function setHeights( $num ) {
179 if ( $num > 0 ) {
180 $this->mHeights = (int)$num;
185 * Allow setting additional options. This is meant
186 * to allow extensions to add additional parameters to
187 * <gallery> parser tag.
189 * @param Array $options Attributes of gallery tag.
191 public function setAdditionalOptions( $options ) { }
194 * Instruct the class to use a specific skin for rendering
196 * @param $skin Skin object
197 * @deprecated since 1.18 Not used anymore
199 function useSkin( $skin ) {
200 wfDeprecated( __METHOD__, '1.18' );
201 /* no op */
205 * Add an image to the gallery.
207 * @param $title Title object of the image that is added to the gallery
208 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
209 * @param $alt String: Alt text for the image
210 * @param $link String: Override image link (optional)
211 * @param $handlerOpts Array: Array of options for image handler (aka page number)
213 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
214 if ( $title instanceof File ) {
215 // Old calling convention
216 $title = $title->getTitle();
218 $this->mImages[] = array( $title, $html, $alt, $link, $handlerOpts );
219 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
223 * Add an image at the beginning of the gallery.
225 * @param $title Title object of the image that is added to the gallery
226 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
227 * @param $alt String: Alt text for the image
228 * @param $link String: Override image link (optional)
229 * @param $handlerOpts Array: Array of options for image handler (aka page number)
231 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
232 if ( $title instanceof File ) {
233 // Old calling convention
234 $title = $title->getTitle();
236 array_unshift( $this->mImages, array( &$title, $html, $alt, $link, $handlerOpts ) );
240 * isEmpty() returns true if the gallery contains no images
241 * @return bool
243 function isEmpty() {
244 return empty( $this->mImages );
248 * Enable/Disable showing of the file size of an image in the gallery.
249 * Enabled by default.
251 * @param $f Boolean: set to false to disable.
253 function setShowBytes( $f ) {
254 $this->mShowBytes = (bool)$f;
258 * Enable/Disable showing of the filename of an image in the gallery.
259 * Enabled by default.
261 * @param $f Boolean: set to false to disable.
263 function setShowFilename( $f ) {
264 $this->mShowFilename = (bool)$f;
268 * Set arbitrary attributes to go on the HTML gallery output element.
269 * Should be suitable for a <ul> element.
271 * Note -- if taking from user input, you should probably run through
272 * Sanitizer::validateAttributes() first.
274 * @param array $attribs of HTML attribute pairs
276 function setAttributes( $attribs ) {
277 $this->mAttribs = $attribs;
281 * Display an html representation of the gallery
283 * @return String The html
285 abstract public function toHTML();
288 * @return Integer: number of images in the gallery
290 public function count() {
291 return count( $this->mImages );
295 * Set the contextual title
297 * @param $title Title: contextual title
299 public function setContextTitle( $title ) {
300 $this->contextTitle = $title;
304 * Get the contextual title, if applicable
306 * @return mixed Title or false
308 public function getContextTitle() {
309 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
310 ? $this->contextTitle
311 : false;
315 * Determines the correct language to be used for this image gallery
316 * @return Language object
318 protected function getRenderLang() {
319 return $this->mParser
320 ? $this->mParser->getTargetLanguage()
321 : $this->getLanguage();
324 /* Old constants no longer used.
325 const THUMB_PADDING = 30;
326 const GB_PADDING = 5;
327 const GB_BORDERS = 8;