|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Image gallery. |
|
4
|
|
|
* |
|
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. |
|
9
|
|
|
* |
|
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. |
|
14
|
|
|
* |
|
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 |
|
19
|
|
|
* |
|
20
|
|
|
* @file |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Image gallery |
|
25
|
|
|
* |
|
26
|
|
|
* Add images to the gallery using add(), then render that list to HTML using toHTML(). |
|
27
|
|
|
* |
|
28
|
|
|
* @ingroup Media |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class ImageGalleryBase extends ContextSource { |
|
31
|
|
|
/** |
|
32
|
|
|
* @var array Gallery images |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $mImages; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var bool Whether to show the filesize in bytes in categories |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $mShowBytes; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var bool Whether to show the filename. Default: true |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $mShowFilename; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string Gallery mode. Default: traditional |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $mMode; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var bool|string Gallery caption. Default: false |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $mCaption = false; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var bool Hide blacklisted images? |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $mHideBadImages; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var Parser Registered parser object for output callbacks |
|
63
|
|
|
*/ |
|
64
|
|
|
public $mParser; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var Title Contextual title, used when images are being screened against |
|
68
|
|
|
* the bad image list |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $contextTitle = false; |
|
71
|
|
|
|
|
72
|
|
|
/** @var array */ |
|
73
|
|
|
protected $mAttribs = []; |
|
74
|
|
|
|
|
75
|
|
|
/** @var bool */ |
|
76
|
|
|
static private $modeMapping = false; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get a new image gallery. This is the method other callers |
|
80
|
|
|
* should use to get a gallery. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string|bool $mode Mode to use. False to use the default |
|
83
|
|
|
* @param IContextSource|null $context |
|
84
|
|
|
* @return ImageGalleryBase |
|
85
|
|
|
* @throws MWException |
|
86
|
|
|
*/ |
|
87
|
|
|
static function factory( $mode = false, IContextSource $context = null ) { |
|
88
|
|
|
global $wgContLang; |
|
89
|
|
|
self::loadModes(); |
|
90
|
|
|
if ( !$context ) { |
|
91
|
|
|
$context = RequestContext::getMainAndWarn( __METHOD__ ); |
|
92
|
|
|
} |
|
93
|
|
|
if ( !$mode ) { |
|
94
|
|
|
$galleryOptions = $context->getConfig()->get( 'GalleryOptions' ); |
|
95
|
|
|
$mode = $galleryOptions['mode']; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$mode = $wgContLang->lc( $mode ); |
|
99
|
|
|
|
|
100
|
|
|
if ( isset( self::$modeMapping[$mode] ) ) { |
|
101
|
|
|
$class = self::$modeMapping[$mode]; |
|
102
|
|
|
return new $class( $mode, $context ); |
|
103
|
|
|
} else { |
|
104
|
|
|
throw new MWException( "No gallery class registered for mode $mode" ); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private static function loadModes() { |
|
109
|
|
|
if ( self::$modeMapping === false ) { |
|
110
|
|
|
self::$modeMapping = [ |
|
|
|
|
|
|
111
|
|
|
'traditional' => 'TraditionalImageGallery', |
|
112
|
|
|
'nolines' => 'NolinesImageGallery', |
|
113
|
|
|
'packed' => 'PackedImageGallery', |
|
114
|
|
|
'packed-hover' => 'PackedHoverImageGallery', |
|
115
|
|
|
'packed-overlay' => 'PackedOverlayImageGallery', |
|
116
|
|
|
'slideshow' => 'SlideshowImageGallery', |
|
117
|
|
|
]; |
|
118
|
|
|
// Allow extensions to make a new gallery format. |
|
119
|
|
|
Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] ); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Create a new image gallery object. |
|
125
|
|
|
* |
|
126
|
|
|
* You should not call this directly, but instead use |
|
127
|
|
|
* ImageGalleryBase::factory(). |
|
128
|
|
|
* @param string $mode |
|
129
|
|
|
* @param IContextSource|null $context |
|
130
|
|
|
*/ |
|
131
|
|
|
function __construct( $mode = 'traditional', IContextSource $context = null ) { |
|
132
|
|
|
if ( $context ) { |
|
133
|
|
|
$this->setContext( $context ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$galleryOptions = $this->getConfig()->get( 'GalleryOptions' ); |
|
137
|
|
|
$this->mImages = []; |
|
138
|
|
|
$this->mShowBytes = $galleryOptions['showBytes']; |
|
139
|
|
|
$this->mShowFilename = true; |
|
140
|
|
|
$this->mParser = false; |
|
|
|
|
|
|
141
|
|
|
$this->mHideBadImages = false; |
|
142
|
|
|
$this->mPerRow = $galleryOptions['imagesPerRow']; |
|
|
|
|
|
|
143
|
|
|
$this->mWidths = $galleryOptions['imageWidth']; |
|
|
|
|
|
|
144
|
|
|
$this->mHeights = $galleryOptions['imageHeight']; |
|
|
|
|
|
|
145
|
|
|
$this->mCaptionLength = $galleryOptions['captionLength']; |
|
|
|
|
|
|
146
|
|
|
$this->mMode = $mode; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Register a parser object. If you do not set this |
|
151
|
|
|
* and the output of this gallery ends up in parser |
|
152
|
|
|
* cache, the javascript will break! |
|
153
|
|
|
* |
|
154
|
|
|
* @note This also triggers using the page's target |
|
155
|
|
|
* language instead of the user language. |
|
156
|
|
|
* |
|
157
|
|
|
* @param Parser $parser |
|
158
|
|
|
*/ |
|
159
|
|
|
function setParser( $parser ) { |
|
160
|
|
|
$this->mParser = $parser; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Set bad image flag |
|
165
|
|
|
* @param bool $flag |
|
166
|
|
|
*/ |
|
167
|
|
|
function setHideBadImages( $flag = true ) { |
|
168
|
|
|
$this->mHideBadImages = $flag; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set the caption (as plain text) |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $caption Caption |
|
175
|
|
|
*/ |
|
176
|
|
|
function setCaption( $caption ) { |
|
177
|
|
|
$this->mCaption = htmlspecialchars( $caption ); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Set the caption (as HTML) |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $caption Caption |
|
184
|
|
|
*/ |
|
185
|
|
|
public function setCaptionHtml( $caption ) { |
|
186
|
|
|
$this->mCaption = $caption; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Set how many images will be displayed per row. |
|
191
|
|
|
* |
|
192
|
|
|
* @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt |
|
193
|
|
|
* to screensize invalid numbers will be rejected |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setPerRow( $num ) { |
|
196
|
|
|
if ( $num >= 0 ) { |
|
197
|
|
|
$this->mPerRow = (int)$num; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Set how wide each image will be, in pixels. |
|
203
|
|
|
* |
|
204
|
|
|
* @param int $num Integer > 0; invalid numbers will be ignored |
|
205
|
|
|
*/ |
|
206
|
|
|
public function setWidths( $num ) { |
|
207
|
|
|
if ( $num > 0 ) { |
|
208
|
|
|
$this->mWidths = (int)$num; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Set how high each image will be, in pixels. |
|
214
|
|
|
* |
|
215
|
|
|
* @param int $num Integer > 0; invalid numbers will be ignored |
|
216
|
|
|
*/ |
|
217
|
|
|
public function setHeights( $num ) { |
|
218
|
|
|
if ( $num > 0 ) { |
|
219
|
|
|
$this->mHeights = (int)$num; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Allow setting additional options. This is meant |
|
225
|
|
|
* to allow extensions to add additional parameters to |
|
226
|
|
|
* <gallery> parser tag. |
|
227
|
|
|
* |
|
228
|
|
|
* @param array $options Attributes of gallery tag |
|
229
|
|
|
*/ |
|
230
|
|
|
public function setAdditionalOptions( $options ) { |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Add an image to the gallery. |
|
235
|
|
|
* |
|
236
|
|
|
* @param Title $title Title object of the image that is added to the gallery |
|
237
|
|
|
* @param string $html Additional HTML text to be shown. The name and size |
|
238
|
|
|
* of the image are always shown. |
|
239
|
|
|
* @param string $alt Alt text for the image |
|
240
|
|
|
* @param string $link Override image link (optional) |
|
241
|
|
|
* @param array $handlerOpts Array of options for image handler (aka page number) |
|
242
|
|
|
*/ |
|
243
|
|
|
function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) { |
|
244
|
|
|
if ( $title instanceof File ) { |
|
245
|
|
|
// Old calling convention |
|
246
|
|
|
$title = $title->getTitle(); |
|
247
|
|
|
} |
|
248
|
|
|
$this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ]; |
|
249
|
|
|
wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" ); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Add an image at the beginning of the gallery. |
|
254
|
|
|
* |
|
255
|
|
|
* @param Title $title Title object of the image that is added to the gallery |
|
256
|
|
|
* @param string $html Additional HTML text to be shown. The name and size |
|
257
|
|
|
* of the image are always shown. |
|
258
|
|
|
* @param string $alt Alt text for the image |
|
259
|
|
|
* @param string $link Override image link (optional) |
|
260
|
|
|
* @param array $handlerOpts Array of options for image handler (aka page number) |
|
261
|
|
|
*/ |
|
262
|
|
|
function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) { |
|
263
|
|
|
if ( $title instanceof File ) { |
|
264
|
|
|
// Old calling convention |
|
265
|
|
|
$title = $title->getTitle(); |
|
266
|
|
|
} |
|
267
|
|
|
array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] ); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Returns the list of images this gallery contains |
|
272
|
|
|
* @return array |
|
273
|
|
|
*/ |
|
274
|
|
|
public function getImages() { |
|
275
|
|
|
return $this->mImages; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* isEmpty() returns true if the gallery contains no images |
|
280
|
|
|
* @return bool |
|
281
|
|
|
*/ |
|
282
|
|
|
function isEmpty() { |
|
283
|
|
|
return empty( $this->mImages ); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Enable/Disable showing of the file size of an image in the gallery. |
|
288
|
|
|
* Enabled by default. |
|
289
|
|
|
* |
|
290
|
|
|
* @param bool $f Set to false to disable |
|
291
|
|
|
*/ |
|
292
|
|
|
function setShowBytes( $f ) { |
|
293
|
|
|
$this->mShowBytes = (bool)$f; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Enable/Disable showing of the filename of an image in the gallery. |
|
298
|
|
|
* Enabled by default. |
|
299
|
|
|
* |
|
300
|
|
|
* @param bool $f Set to false to disable |
|
301
|
|
|
*/ |
|
302
|
|
|
function setShowFilename( $f ) { |
|
303
|
|
|
$this->mShowFilename = (bool)$f; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Set arbitrary attributes to go on the HTML gallery output element. |
|
308
|
|
|
* Should be suitable for a <ul> element. |
|
309
|
|
|
* |
|
310
|
|
|
* Note -- if taking from user input, you should probably run through |
|
311
|
|
|
* Sanitizer::validateAttributes() first. |
|
312
|
|
|
* |
|
313
|
|
|
* @param array $attribs Array of HTML attribute pairs |
|
314
|
|
|
*/ |
|
315
|
|
|
function setAttributes( $attribs ) { |
|
316
|
|
|
$this->mAttribs = $attribs; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Display an html representation of the gallery |
|
321
|
|
|
* |
|
322
|
|
|
* @return string The html |
|
323
|
|
|
*/ |
|
324
|
|
|
abstract public function toHTML(); |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* @return int Number of images in the gallery |
|
328
|
|
|
*/ |
|
329
|
|
|
public function count() { |
|
330
|
|
|
return count( $this->mImages ); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Set the contextual title |
|
335
|
|
|
* |
|
336
|
|
|
* @param Title $title Contextual title |
|
337
|
|
|
*/ |
|
338
|
|
|
public function setContextTitle( $title ) { |
|
339
|
|
|
$this->contextTitle = $title; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Get the contextual title, if applicable |
|
344
|
|
|
* |
|
345
|
|
|
* @return Title|bool Title or false |
|
346
|
|
|
*/ |
|
347
|
|
|
public function getContextTitle() { |
|
348
|
|
|
return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title |
|
349
|
|
|
? $this->contextTitle |
|
350
|
|
|
: false; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Determines the correct language to be used for this image gallery |
|
355
|
|
|
* @return Language |
|
356
|
|
|
*/ |
|
357
|
|
|
protected function getRenderLang() { |
|
358
|
|
|
return $this->mParser |
|
359
|
|
|
? $this->mParser->getTargetLanguage() |
|
360
|
|
|
: $this->getLanguage(); |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..