Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / gallery / ImageGalleryBase.php
blobc89c6b6c1e3b703b33347eb80087e5c7a625eae2
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
34 protected $mImages;
36 /**
37 * @var bool Whether to show the filesize in bytes in categories
39 protected $mShowBytes;
41 /**
42 * @var bool Whether to show the filename. Default: true
44 protected $mShowFilename;
46 /**
47 * @var string Gallery mode. Default: traditional
49 protected $mMode;
51 /**
52 * @var bool|string Gallery caption. Default: false
54 protected $mCaption = false;
56 /**
57 * @var bool Hide blacklisted images?
59 protected $mHideBadImages;
61 /**
62 * @var Parser Registered parser object for output callbacks
64 public $mParser;
66 /**
67 * @var Title Contextual title, used when images are being screened against
68 * the bad image list
70 protected $contextTitle = false;
72 /** @var array */
73 protected $mAttribs = array();
75 /** @var bool */
76 static private $modeMapping = false;
78 /**
79 * Get a new image gallery. This is the method other callers
80 * should use to get a gallery.
82 * @param string|bool $mode Mode to use. False to use the default
83 * @param IContextSource|null $context
84 * @return ImageGalleryBase
85 * @throws MWException
87 static function factory( $mode = false, IContextSource $context = null ) {
88 global $wgContLang;
89 self::loadModes();
90 if ( !$context ) {
91 $context = RequestContext::getMainAndWarn( __METHOD__ );
93 if ( !$mode ) {
94 $galleryOpions = $context->getConfig()->get( 'GalleryOptions' );
95 $mode = $galleryOpions['mode'];
98 $mode = $wgContLang->lc( $mode );
100 if ( isset( self::$modeMapping[$mode] ) ) {
101 return new self::$modeMapping[$mode]( $mode, $context );
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 Hooks::run( 'GalleryGetModes', array( &self::$modeMapping ) );
122 * Create a new image gallery object.
124 * You should not call this directly, but instead use
125 * ImageGalleryBase::factory().
126 * @param string $mode
127 * @param IContextSource|null $context
129 function __construct( $mode = 'traditional', IContextSource $context = null ) {
130 if ( $context ) {
131 $this->setContext( $context );
134 $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
135 $this->mImages = array();
136 $this->mShowBytes = $galleryOptions['showBytes'];
137 $this->mShowFilename = true;
138 $this->mParser = false;
139 $this->mHideBadImages = false;
140 $this->mPerRow = $galleryOptions['imagesPerRow'];
141 $this->mWidths = $galleryOptions['imageWidth'];
142 $this->mHeights = $galleryOptions['imageHeight'];
143 $this->mCaptionLength = $galleryOptions['captionLength'];
144 $this->mMode = $mode;
148 * Register a parser object. If you do not set this
149 * and the output of this gallery ends up in parser
150 * cache, the javascript will break!
152 * @note This also triggers using the page's target
153 * language instead of the user language.
155 * @param Parser $parser
157 function setParser( $parser ) {
158 $this->mParser = $parser;
162 * Set bad image flag
163 * @param bool $flag
165 function setHideBadImages( $flag = true ) {
166 $this->mHideBadImages = $flag;
170 * Set the caption (as plain text)
172 * @param string $caption Caption
174 function setCaption( $caption ) {
175 $this->mCaption = htmlspecialchars( $caption );
179 * Set the caption (as HTML)
181 * @param string $caption Caption
183 public function setCaptionHtml( $caption ) {
184 $this->mCaption = $caption;
188 * Set how many images will be displayed per row.
190 * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
191 * to screensize invalid numbers will be rejected
193 public function setPerRow( $num ) {
194 if ( $num >= 0 ) {
195 $this->mPerRow = (int)$num;
200 * Set how wide each image will be, in pixels.
202 * @param int $num Integer > 0; invalid numbers will be ignored
204 public function setWidths( $num ) {
205 if ( $num > 0 ) {
206 $this->mWidths = (int)$num;
211 * Set how high each image will be, in pixels.
213 * @param int $num Integer > 0; invalid numbers will be ignored
215 public function setHeights( $num ) {
216 if ( $num > 0 ) {
217 $this->mHeights = (int)$num;
222 * Allow setting additional options. This is meant
223 * to allow extensions to add additional parameters to
224 * <gallery> parser tag.
226 * @param array $options Attributes of gallery tag
228 public function setAdditionalOptions( $options ) {
232 * Add an image to the gallery.
234 * @param Title $title Title object of the image that is added to the gallery
235 * @param string $html Additional HTML text to be shown. The name and size
236 * of the image are always shown.
237 * @param string $alt Alt text for the image
238 * @param string $link Override image link (optional)
239 * @param array $handlerOpts Array of options for image handler (aka page number)
241 function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
242 if ( $title instanceof File ) {
243 // Old calling convention
244 $title = $title->getTitle();
246 $this->mImages[] = array( $title, $html, $alt, $link, $handlerOpts );
247 wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
251 * Add an image at the beginning of the gallery.
253 * @param Title $title Title object of the image that is added to the gallery
254 * @param string $html Additional HTML text to be shown. The name and size
255 * of the image are always shown.
256 * @param string $alt Alt text for the image
257 * @param string $link Override image link (optional)
258 * @param array $handlerOpts Array of options for image handler (aka page number)
260 function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = array() ) {
261 if ( $title instanceof File ) {
262 // Old calling convention
263 $title = $title->getTitle();
265 array_unshift( $this->mImages, array( &$title, $html, $alt, $link, $handlerOpts ) );
269 * Returns the list of images this gallery contains
270 * @return array
272 public function getImages() {
273 return $this->mImages;
277 * isEmpty() returns true if the gallery contains no images
278 * @return bool
280 function isEmpty() {
281 return empty( $this->mImages );
285 * Enable/Disable showing of the file size of an image in the gallery.
286 * Enabled by default.
288 * @param bool $f Set to false to disable
290 function setShowBytes( $f ) {
291 $this->mShowBytes = (bool)$f;
295 * Enable/Disable showing of the filename of an image in the gallery.
296 * Enabled by default.
298 * @param bool $f Set to false to disable
300 function setShowFilename( $f ) {
301 $this->mShowFilename = (bool)$f;
305 * Set arbitrary attributes to go on the HTML gallery output element.
306 * Should be suitable for a <ul> element.
308 * Note -- if taking from user input, you should probably run through
309 * Sanitizer::validateAttributes() first.
311 * @param array $attribs Array of HTML attribute pairs
313 function setAttributes( $attribs ) {
314 $this->mAttribs = $attribs;
318 * Display an html representation of the gallery
320 * @return string The html
322 abstract public function toHTML();
325 * @return int Number of images in the gallery
327 public function count() {
328 return count( $this->mImages );
332 * Set the contextual title
334 * @param Title $title Contextual title
336 public function setContextTitle( $title ) {
337 $this->contextTitle = $title;
341 * Get the contextual title, if applicable
343 * @return Title|bool Title or false
345 public function getContextTitle() {
346 return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
347 ? $this->contextTitle
348 : false;
352 * Determines the correct language to be used for this image gallery
353 * @return Language
355 protected function getRenderLang() {
356 return $this->mParser
357 ? $this->mParser->getTargetLanguage()
358 : $this->getLanguage();