| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Ext\Gallery; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\Core\Sanitizer; |
| 7 |
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
| 8 |
use Wikimedia\Parsoid\Ext\Utils; |
| 9 |
|
| 10 |
class Opts { |
| 11 |
/** |
| 12 |
* Parse options from an attribute array. |
| 13 |
* @param ParsoidExtensionAPI $extApi |
| 14 |
* @param array<string,string> $attrs The attribute array |
| 15 |
*/ |
| 16 |
public function __construct( ParsoidExtensionAPI $extApi, array $attrs ) { |
| 17 |
$siteConfig = $extApi->getSiteConfig(); |
| 18 |
|
| 19 |
// Set default values from config |
| 20 |
// The options 'showDimensions' and 'showBytes' for traditional mode are not implemented, |
| 21 |
// They are not used for galleries in wikitext (only on category pages or special pages) |
| 22 |
// The deprecated option 'captionLength' for traditional mode is not implemented. |
| 23 |
$galleryOptions = $siteConfig->galleryOptions(); |
| 24 |
$this->imagesPerRow = $galleryOptions['imagesPerRow']; |
| 25 |
$this->imageWidth = $galleryOptions['imageWidth']; |
| 26 |
$this->imageHeight = $galleryOptions['imageHeight']; |
| 27 |
$this->mode = $galleryOptions['mode']; |
| 28 |
|
| 29 |
// Override values from given attributes |
| 30 |
if ( is_numeric( $attrs['perrow'] ?? null ) ) { |
| 31 |
$this->imagesPerRow = intval( $attrs['perrow'], 10 ); |
| 32 |
} |
| 33 |
|
| 34 |
$maybeDim = Utils::parseMediaDimensions( |
| 35 |
$siteConfig, $attrs['widths'] ?? '', true, false |
| 36 |
); |
| 37 |
if ( $maybeDim && Utils::validateMediaParam( $maybeDim['x'] ) ) { |
| 38 |
$this->imageWidth = $maybeDim['x']; |
| 39 |
} |
| 40 |
|
| 41 |
$maybeDim = Utils::parseMediaDimensions( |
| 42 |
$siteConfig, $attrs['heights'] ?? '', true, false |
| 43 |
); |
| 44 |
if ( $maybeDim && Utils::validateMediaParam( $maybeDim['x'] ) ) { |
| 45 |
$this->imageHeight = $maybeDim['x']; |
| 46 |
} |
| 47 |
|
| 48 |
$mode = strtolower( $attrs['mode'] ?? '' ); |
| 49 |
if ( Mode::byName( $mode ) !== null ) { |
| 50 |
$this->mode = $mode; |
| 51 |
} |
| 52 |
|
| 53 |
$this->showfilename = isset( $attrs['showfilename'] ); |
| 54 |
$this->showthumbnails = isset( $attrs['showthumbnails'] ); |
| 55 |
$this->caption = (bool)( $attrs['caption'] ?? false ); |
| 56 |
|
| 57 |
// TODO: Good contender for T54941 |
| 58 |
$validUlAttrs = Sanitizer::attributesAllowedInternal( 'ul' ); |
| 59 |
$this->attrs = []; |
| 60 |
foreach ( $attrs as $k => $v ) { |
| 61 |
if ( !isset( $validUlAttrs[$k] ) ) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
if ( $k === 'style' ) { |
| 65 |
$v = Sanitizer::checkCss( $v ); |
| 66 |
} |
| 67 |
$this->attrs[$k] = $v; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** @var array<string,string> */ |
| 72 |
public $attrs; |
| 73 |
|
| 74 |
/** @var int */ |
| 75 |
public $imagesPerRow; |
| 76 |
|
| 77 |
/** @var int */ |
| 78 |
public $imageWidth; |
| 79 |
|
| 80 |
/** @var int */ |
| 81 |
public $imageHeight; |
| 82 |
|
| 83 |
/** @var string */ |
| 84 |
public $mode; |
| 85 |
|
| 86 |
/** @var bool */ |
| 87 |
public $showfilename; |
| 88 |
|
| 89 |
/** @var bool */ |
| 90 |
public $showthumbnails; |
| 91 |
|
| 92 |
/** @var bool */ |
| 93 |
public $caption; |
| 94 |
|
| 95 |
} |