Show block notice on contribs for range-blocked IP
[mediawiki.git] / includes / gallery / ImageGalleryBase.php
blob8d2b94964c206396792f00e82f401809825478bf
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 /**
32 * @var array Gallery images
33 * @deprecated in 1.23 (was declared "var") and will be removed in 1.24
35 public $mImages;
37 /**
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
41 public $mShowBytes;
43 /**
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;
49 /**
50 * @var string Gallery mode. Default: traditional
51 * @deprecated in 1.23 (was declared "var") and will be removed in 1.24
53 public $mMode;
55 /**
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;
61 /**
62 * @var bool Hide blacklisted images?
63 * @deprecated in 1.23 (was declared "var") and will be removed in 1.24
65 public $mHideBadImages;
67 /**
68 * @var Parser Registered parser object for output callbacks
70 public $mParser;
72 /**
73 * @var Title Contextual title, used when images are being screened against
74 * the bad image list
76 protected $contextTitle = false;
78 /** @var array */
79 protected $mAttribs = array();
81 /** @var bool */
82 static private $modeMapping = false;
84 /**
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 * @throws MWException
91 static function factory( $mode = false ) {
92 global $wgGalleryOptions, $wgContLang;
93 self::loadModes();
94 if ( !$mode ) {
95 $mode = $wgGalleryOptions['mode'];
98 $mode = $wgContLang->lc( $mode );
100 if ( isset( self::$modeMapping[$mode] ) ) {
101 return new self::$modeMapping[$mode]( $mode );
102 } else {
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;
156 * Set bad image flag
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 ) {
187 if ( $num >= 0 ) {
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 ) {
198 if ( $num > 0 ) {
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 ) {
209 if ( $num > 0 ) {
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 * isEmpty() returns true if the gallery contains no images
263 * @return bool
265 function isEmpty() {
266 return empty( $this->mImages );
270 * Enable/Disable showing of the file size of an image in the gallery.
271 * Enabled by default.
273 * @param bool $f Set to false to disable
275 function setShowBytes( $f ) {
276 $this->mShowBytes = (bool)$f;
280 * Enable/Disable showing of the filename of an image in the gallery.
281 * Enabled by default.
283 * @param bool $f Set to false to disable
285 function setShowFilename( $f ) {
286 $this->mShowFilename = (bool)$f;
290 * Set arbitrary attributes to go on the HTML gallery output element.
291 * Should be suitable for a <ul> element.
293 * Note -- if taking from user input, you should probably run through
294 * Sanitizer::validateAttributes() first.
296 * @param array $attribs Array of HTML attribute pairs
298 function setAttributes( $attribs ) {
299 $this->mAttribs = $attribs;
303 * Display an html representation of the gallery
305 * @return string The html
307 abstract public function toHTML();
310 * @return int Number of images in the gallery
312 public function count() {
313 return count( $this->mImages );
317 * Set the contextual title
319 * @param Title $title Contextual title
321 public function setContextTitle( $title ) {
322 $this->contextTitle = $title;
326 * Get the contextual title, if applicable
328 * @return Title|bool Title or false
330 public function getContextTitle() {
331 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
332 ? $this->contextTitle
333 : false;
337 * Determines the correct language to be used for this image gallery
338 * @return Language
340 protected function getRenderLang() {
341 return $this->mParser
342 ? $this->mParser->getTargetLanguage()
343 : $this->getLanguage();