3 * Resource loader module for generated and embedded images.
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
21 * @author Trevor Parscal
25 * Resource loader module for generated and embedded images.
29 class ResourceLoaderImageModule
extends ResourceLoaderModule
{
32 * Local base path, see __construct()
35 protected $localBasePath = '';
37 protected $origin = self
::ORIGIN_CORE_SITEWIDE
;
39 protected $images = array();
40 protected $variants = array();
41 protected $prefix = null;
42 protected $selectorWithoutVariant = '.{prefix}-{type}-{name}';
43 protected $selectorWithVariant = '.{prefix}-{type}-{name}-{variant}';
44 protected $targets = array( 'desktop', 'mobile' );
47 * Constructs a new module from an options array.
49 * @param array $options List of options; if not given or empty, an empty module will be
51 * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults
54 * Below is a description for the $options array:
55 * @par Construction options:
58 * // Base path to prepend to all local paths in $options. Defaults to $IP
59 * 'localBasePath' => [base path],
60 * // CSS class prefix to use in all style rules
61 * 'prefix' => [CSS class prefix],
62 * // Alternatively: Format of CSS selector to use in all style rules
63 * 'selector' => [CSS selector template, variables: {prefix} {type} {name} {variant}],
64 * // Alternatively: When using variants
65 * 'selectorWithoutVariant' => [CSS selector template, variables: {prefix} {type} {name}],
66 * 'selectorWithVariant' => [CSS selector template, variables: {prefix} {type} {name} {variant}],
67 * // List of variants that may be used for the image files
68 * 'variants' => array(
69 * // ([image type] is a string, used in generated CSS class
70 * // names and to match variants to images)
71 * [image type] => array(
72 * [variant name] => array(
73 * 'color' => [color string, e.g. '#ffff00'],
74 * 'global' => [boolean, if true, this variant is available
75 * for all images of this type],
79 * // List of image files and their options
81 * [image type] => array(
83 * [file path string] => array(
84 * 'name' => [image name string, defaults to file name],
85 * 'variants' => [array of variant name strings, variants
86 * available for this image],
94 public function __construct( $options = array(), $localBasePath = null ) {
95 $this->localBasePath
= self
::extractLocalBasePath( $options, $localBasePath );
97 // Accepted combinations:
100 // * selectorWithoutVariant + selectorWithVariant
101 // * prefix + selector
102 // * prefix + selectorWithoutVariant + selectorWithVariant
104 $prefix = isset( $options['prefix'] ) && $options['prefix'];
105 $selector = isset( $options['selector'] ) && $options['selector'];
106 $selectorWithoutVariant = isset( $options['selectorWithoutVariant'] ) && $options['selectorWithoutVariant'];
107 $selectorWithVariant = isset( $options['selectorWithVariant'] ) && $options['selectorWithVariant'];
109 if ( $selectorWithoutVariant && !$selectorWithVariant ) {
110 throw new MWException( "Given 'selectorWithoutVariant' but no 'selectorWithVariant'." );
112 if ( $selectorWithVariant && !$selectorWithoutVariant ) {
113 throw new MWException( "Given 'selectorWithVariant' but no 'selectorWithoutVariant'." );
115 if ( $selector && $selectorWithVariant ) {
116 throw new MWException( "Incompatible 'selector' and 'selectorWithVariant'+'selectorWithoutVariant' given." );
118 if ( !$prefix && !$selector && !$selectorWithVariant ) {
119 throw new MWException( "None of 'prefix', 'selector' or 'selectorWithVariant'+'selectorWithoutVariant' given." );
122 foreach ( $options as $member => $option ) {
125 if ( !is_array( $option ) ) {
126 throw new MWException(
127 "Invalid collated file path list error. '$option' given, array expected."
130 foreach ( $option as $key => $value ) {
131 if ( !is_string( $key ) ) {
132 throw new MWException(
133 "Invalid collated file path list key error. '$key' given, string expected."
136 $this->{$member}[$key] = (array)$value;
141 if ( !is_array( $option ) ) {
142 throw new MWException(
143 "Invalid variant list error. '$option' given, array expected."
146 $this->{$member} = $option;
150 case 'selectorWithoutVariant':
151 case 'selectorWithVariant':
152 $this->{$member} = (string)$option;
156 $this->selectorWithoutVariant
= $this->selectorWithVariant
= (string)$option;
162 * Get CSS class prefix used by this module.
165 public function getPrefix() {
166 return $this->prefix
;
170 * Get CSS selector templates used by this module.
173 public function getSelectors() {
175 'selectorWithoutVariant' => $this->selectorWithoutVariant
,
176 'selectorWithVariant' => $this->selectorWithVariant
,
181 * Get a ResourceLoaderImage object for given image.
182 * @param string $name Image name
183 * @return ResourceLoaderImage|null
185 public function getImage( $name ) {
186 $images = $this->getImages();
187 return isset( $images[$name] ) ?
$images[$name] : null;
191 * Get ResourceLoaderImage objects for all images.
192 * @return ResourceLoaderImage[] Array keyed by image name
194 public function getImages() {
195 if ( !isset( $this->imageObjects
) ) {
196 $this->imageObjects
= array();
198 foreach ( $this->images
as $type => $list ) {
199 foreach ( $list as $name => $options ) {
200 $imageDesc = is_string( $options ) ?
$options : $options['image'];
202 $allowedVariants = array_merge(
203 is_array( $options ) && isset( $options['variants'] ) ?
$options['variants'] : array(),
204 $this->getGlobalVariants( $type )
206 if ( isset( $this->variants
[$type] ) ) {
207 $variantConfig = array_intersect_key(
208 $this->variants
[$type],
209 array_fill_keys( $allowedVariants, true )
212 $variantConfig = array();
215 $image = new ResourceLoaderImage(
219 $this->localBasePath
,
222 $this->imageObjects
[ $image->getName() ] = $image;
227 return $this->imageObjects
;
231 * Get list of variants in this module that are 'global' for given type of images, i.e., available
232 * for every image of given type regardless of image options.
233 * @param string $type Image type
236 public function getGlobalVariants( $type ) {
237 if ( !isset( $this->globalVariants
[$type] ) ) {
238 $this->globalVariants
[$type] = array();
240 if ( isset( $this->variants
[$type] ) ) {
241 foreach ( $this->variants
[$type] as $name => $config ) {
242 if ( isset( $config['global'] ) && $config['global'] ) {
243 $this->globalVariants
[$type][] = $name;
249 return $this->globalVariants
[$type];
253 * Get the type of given image.
254 * @param string $imageName Image name
257 public function getImageType( $imageName ) {
258 foreach ( $this->images
as $type => $list ) {
259 foreach ( $list as $key => $value ) {
260 $file = is_int( $key ) ?
$value : $key;
261 $options = is_array( $value ) ?
$value : array();
262 $name = isset( $options['name'] ) ?
$options['name'] : pathinfo( $file, PATHINFO_FILENAME
);
263 if ( $name === $imageName ) {
271 * @param ResourceLoaderContext $context
274 public function getStyles( ResourceLoaderContext
$context ) {
277 $script = $context->getResourceLoader()->getLoadScript( $this->getSource() );
278 $selectors = $this->getSelectors();
280 $needsTypeWithoutVariant = strpos( $selectors['selectorWithoutVariant'], '{type}' ) !== false;
281 $needsTypeWithVariant = strpos( $selectors['selectorWithVariant'], '{type}' ) !== false;
283 foreach ( $this->getImages() as $name => $image ) {
284 $type = $this->getImageType( $name );
286 $declarations = $this->getCssDeclarations(
287 $image->getDataUri( $context, null, 'original' ),
288 $image->getUrl( $context, $script, null, 'rasterized' )
290 $declarations = implode( "\n\t", $declarations );
292 $selectors['selectorWithoutVariant'],
294 '{prefix}' => $this->getPrefix(),
297 // Somewhat expensive, don't compute if not needed
298 '{type}' => $needsTypeWithoutVariant ?
$this->getImageType( $name ) : null,
301 $rules[] = "$selector {\n\t$declarations\n}";
303 // TODO: Get variant configurations from $context->getSkin()
304 foreach ( $image->getVariants() as $variant ) {
305 $declarations = $this->getCssDeclarations(
306 $image->getDataUri( $context, $variant, 'original' ),
307 $image->getUrl( $context, $script, $variant, 'rasterized' )
309 $declarations = implode( "\n\t", $declarations );
311 $selectors['selectorWithVariant'],
313 '{prefix}' => $this->getPrefix(),
315 '{variant}' => $variant,
316 // Somewhat expensive, don't compute if not needed
317 '{type}' => $needsTypeWithVariant ?
$this->getImageType( $name ) : null,
320 $rules[] = "$selector {\n\t$declarations\n}";
324 $style = implode( "\n", $rules );
325 if ( $this->getFlip( $context ) ) {
326 $style = CSSJanus
::transform( $style, true, false );
328 return array( 'all' => $style );
332 * SVG support using a transparent gradient to guarantee cross-browser
333 * compatibility (browsers able to understand gradient syntax support also SVG).
334 * http://pauginer.tumblr.com/post/36614680636/invisible-gradient-technique
336 * Keep synchronized with the .background-image-svg LESS mixin in
337 * /resources/src/mediawiki.less/mediawiki.mixins.less.
339 * @param string $primary Primary URI
340 * @param string $fallback Fallback URI
341 * @return string[] CSS declarations to use given URIs as background-image
343 protected function getCssDeclarations( $primary, $fallback ) {
345 "background-image: url($fallback);",
346 "background-image: -webkit-linear-gradient(transparent, transparent), url($primary);",
347 "background-image: linear-gradient(transparent, transparent), url($primary);",
348 "background-image: -o-linear-gradient(transparent, transparent), url($fallback);",
355 public function supportsURLLoading() {
360 * Extract a local base path from module definition information.
362 * @param array $options Module definition
363 * @param string $localBasePath Path to use if not provided in module definition. Defaults
365 * @return string Local base path
367 public static function extractLocalBasePath( $options, $localBasePath = null ) {
370 if ( $localBasePath === null ) {
371 $localBasePath = $IP;
374 if ( array_key_exists( 'localBasePath', $options ) ) {
375 $localBasePath = (string)$options['localBasePath'];
378 return $localBasePath;