| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use MediaWiki\Context\ContextSource; |
| 24 |
use MediaWiki\Context\IContextSource; |
| 25 |
use MediaWiki\Context\RequestContext; |
| 26 |
use MediaWiki\HookContainer\HookRunner; |
| 27 |
use MediaWiki\Language\Language; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\MediaWikiServices; |
| 30 |
use MediaWiki\Parser\Parser; |
| 31 |
use MediaWiki\Title\Title; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
abstract class ImageGalleryBase extends ContextSource { |
| 41 |
public const LOADING_DEFAULT = 1; |
| 42 |
public const LOADING_LAZY = 2; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected $mImages; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected $mShowBytes; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected $mShowDimensions; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected $mShowFilename; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected $mMode; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
protected $mCaption = false; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
protected $mCaptionLength = true; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
protected $mHideBadImages; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public $mParser; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
protected $contextTitle = null; |
| 99 |
|
| 100 |
|
| 101 |
protected $mAttribs = []; |
| 102 |
|
| 103 |
|
| 104 |
protected $mPerRow; |
| 105 |
|
| 106 |
|
| 107 |
protected $mWidths; |
| 108 |
|
| 109 |
|
| 110 |
protected $mHeights; |
| 111 |
|
| 112 |
|
| 113 |
private static $modeMapping; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static function factory( $mode = false, ?IContextSource $context = null ) { |
| 125 |
self::loadModes(); |
| 126 |
if ( !$context ) { |
| 127 |
$context = RequestContext::getMainAndWarn( __METHOD__ ); |
| 128 |
} |
| 129 |
if ( !$mode ) { |
| 130 |
$galleryOptions = $context->getConfig()->get( MainConfigNames::GalleryOptions ); |
| 131 |
$mode = $galleryOptions['mode']; |
| 132 |
} |
| 133 |
|
| 134 |
$mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode ); |
| 135 |
|
| 136 |
if ( isset( self::$modeMapping[$mode] ) ) { |
| 137 |
$class = self::$modeMapping[$mode]; |
| 138 |
return new $class( $mode, $context ); |
| 139 |
} else { |
| 140 |
throw new ImageGalleryClassNotFoundException( "No gallery class registered for mode $mode" ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
private static function loadModes() { |
| 145 |
if ( self::$modeMapping === null ) { |
| 146 |
self::$modeMapping = [ |
| 147 |
'traditional' => TraditionalImageGallery::class, |
| 148 |
'nolines' => NolinesImageGallery::class, |
| 149 |
'packed' => PackedImageGallery::class, |
| 150 |
'packed-hover' => PackedHoverImageGallery::class, |
| 151 |
'packed-overlay' => PackedOverlayImageGallery::class, |
| 152 |
'slideshow' => SlideshowImageGallery::class, |
| 153 |
]; |
| 154 |
|
| 155 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
| 156 |
->onGalleryGetModes( self::$modeMapping ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function __construct( $mode = 'traditional', ?IContextSource $context = null ) { |
| 174 |
if ( $context ) { |
| 175 |
$this->setContext( $context ); |
| 176 |
} |
| 177 |
|
| 178 |
$galleryOptions = $this->getConfig()->get( MainConfigNames::GalleryOptions ); |
| 179 |
$this->mImages = []; |
| 180 |
$this->mShowBytes = $galleryOptions['showBytes']; |
| 181 |
$this->mShowDimensions = $galleryOptions['showDimensions']; |
| 182 |
$this->mShowFilename = true; |
| 183 |
$this->mParser = false; |
| 184 |
$this->mHideBadImages = false; |
| 185 |
$this->mPerRow = $galleryOptions['imagesPerRow']; |
| 186 |
$this->mWidths = $galleryOptions['imageWidth']; |
| 187 |
$this->mHeights = $galleryOptions['imageHeight']; |
| 188 |
$this->mCaptionLength = $galleryOptions['captionLength']; |
| 189 |
$this->mMode = $mode; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function setParser( $parser ) { |
| 203 |
$this->mParser = $parser; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public function setHideBadImages( $flag = true ) { |
| 210 |
$this->mHideBadImages = $flag; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function setCaption( $caption ) { |
| 219 |
$this->mCaption = htmlspecialchars( $caption ); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function setCaptionHtml( $caption ) { |
| 228 |
$this->mCaption = $caption; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function setPerRow( $num ) { |
| 238 |
if ( $num >= 0 ) { |
| 239 |
$this->mPerRow = (int)$num; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public function setWidths( $num ) { |
| 250 |
$parser = $this->mParser; |
| 251 |
if ( !$parser ) { |
| 252 |
wfDeprecated( __METHOD__ . ' without parser', '1.43' ); |
| 253 |
$parser = MediaWikiServices::getInstance()->getParser(); |
| 254 |
} |
| 255 |
$parsed = $parser->parseWidthParam( $num, false ); |
| 256 |
if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) { |
| 257 |
$this->mWidths = $parsed['width']; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function setHeights( $num ) { |
| 268 |
$parser = $this->mParser; |
| 269 |
if ( !$parser ) { |
| 270 |
wfDeprecated( __METHOD__ . ' without parser', '1.43' ); |
| 271 |
$parser = MediaWikiServices::getInstance()->getParser(); |
| 272 |
} |
| 273 |
$parsed = $parser->parseWidthParam( $num, false ); |
| 274 |
if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) { |
| 275 |
$this->mHeights = $parsed['width']; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public function setAdditionalOptions( $options ) { |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public function add( |
| 304 |
$title, |
| 305 |
$html = '', |
| 306 |
$alt = '', |
| 307 |
$link = '', |
| 308 |
$handlerOpts = [], |
| 309 |
$loading = self::LOADING_DEFAULT, |
| 310 |
?array $imageOptions = null |
| 311 |
) { |
| 312 |
if ( $title instanceof File ) { |
| 313 |
|
| 314 |
$title = $title->getTitle(); |
| 315 |
} |
| 316 |
$this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ]; |
| 317 |
wfDebug( 'ImageGallery::add ' . $title->getText() ); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public function insert( |
| 333 |
$title, |
| 334 |
$html = '', |
| 335 |
$alt = '', |
| 336 |
$link = '', |
| 337 |
$handlerOpts = [], |
| 338 |
$loading = self::LOADING_DEFAULT, |
| 339 |
?array $imageOptions = null |
| 340 |
) { |
| 341 |
if ( $title instanceof File ) { |
| 342 |
|
| 343 |
$title = $title->getTitle(); |
| 344 |
} |
| 345 |
array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts, $loading, $imageOptions ] ); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function getImages() { |
| 354 |
return $this->mImages; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function isEmpty() { |
| 362 |
return $this->mImages === []; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public function setShowDimensions( $f ) { |
| 372 |
$this->mShowDimensions = (bool)$f; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function setShowBytes( $f ) { |
| 382 |
$this->mShowBytes = (bool)$f; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public function setShowFilename( $f ) { |
| 392 |
$this->mShowFilename = (bool)$f; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public function setAttributes( $attribs ) { |
| 405 |
$this->mAttribs = $attribs; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
abstract public function toHTML(); |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public function count() { |
| 419 |
return count( $this->mImages ); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public function setContextTitle( $title ) { |
| 428 |
$this->contextTitle = $title; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public function getContextTitle() { |
| 437 |
return $this->contextTitle; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
protected function getRenderLang() { |
| 445 |
return $this->mParser |
| 446 |
? $this->mParser->getTargetLanguage() |
| 447 |
: $this->getLanguage(); |
| 448 |
} |
| 449 |
} |