ResourceLoaderImageModule: Synchronize CSS with .background-image-svg LESS mixin
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderImageModule.php
1 <?php
2 /**
3 * Resource loader module for generated and embedded images.
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 * @author Trevor Parscal
22 */
23
24 /**
25 * Resource loader module for generated and embedded images.
26 *
27 * @since 1.25
28 */
29 class ResourceLoaderImageModule extends ResourceLoaderModule {
30
31 /**
32 * Local base path, see __construct()
33 * @var string
34 */
35 protected $localBasePath = '';
36
37 protected $origin = self::ORIGIN_CORE_SITEWIDE;
38
39 protected $images = array();
40 protected $variants = array();
41 protected $prefix = array();
42 protected $targets = array( 'desktop', 'mobile' );
43
44 /**
45 * Constructs a new module from an options array.
46 *
47 * @param array $options List of options; if not given or empty, an empty module will be
48 * constructed
49 * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults
50 * to $IP
51 *
52 * Below is a description for the $options array:
53 * @par Construction options:
54 * @code
55 * array(
56 * // Base path to prepend to all local paths in $options. Defaults to $IP
57 * 'localBasePath' => [base path],
58 * // CSS class prefix to use in all style rules
59 * 'prefix' => [CSS class prefix],
60 * // List of variants that may be used for the image files
61 * 'variants' => array(
62 * // ([image type] is a string, used in generated CSS class
63 * // names and to match variants to images)
64 * [image type] => array(
65 * [variant name] => array(
66 * 'color' => [color string, e.g. '#ffff00'],
67 * 'global' => [boolean, if true, this variant is available
68 * for all images of this type],
69 * ),
70 * )
71 * ),
72 * // List of image files and their options
73 * 'images' => array(
74 * [image type] => array(
75 * [file path string],
76 * [file path string] => array(
77 * 'name' => [image name string, defaults to file name],
78 * 'variants' => [array of variant name strings, variants
79 * available for this image],
80 * ),
81 * )
82 * ),
83 * )
84 * @endcode
85 * @throws MWException
86 */
87 public function __construct( $options = array(), $localBasePath = null ) {
88 $this->localBasePath = self::extractLocalBasePath( $options, $localBasePath );
89
90 if ( !isset( $options['prefix'] ) || !$options['prefix'] ) {
91 throw new MWException(
92 "Required 'prefix' option not given or empty."
93 );
94 }
95
96 foreach ( $options as $member => $option ) {
97 switch ( $member ) {
98 case 'images':
99 if ( !is_array( $option ) ) {
100 throw new MWException(
101 "Invalid collated file path list error. '$option' given, array expected."
102 );
103 }
104 foreach ( $option as $key => $value ) {
105 if ( !is_string( $key ) ) {
106 throw new MWException(
107 "Invalid collated file path list key error. '$key' given, string expected."
108 );
109 }
110 $this->{$member}[$key] = (array)$value;
111 }
112 break;
113
114 case 'variants':
115 if ( !is_array( $option ) ) {
116 throw new MWException(
117 "Invalid variant list error. '$option' given, array expected."
118 );
119 }
120 $this->{$member} = $option;
121 break;
122
123 case 'prefix':
124 $this->{$member} = (string)$option;
125 break;
126 }
127 }
128 }
129
130 /**
131 * Get CSS class prefix used by this module.
132 * @return string
133 */
134 public function getPrefix() {
135 return $this->prefix;
136 }
137
138 /**
139 * Get a ResourceLoaderImage object for given image.
140 * @param string $name Image name
141 * @return ResourceLoaderImage|null
142 */
143 public function getImage( $name ) {
144 $images = $this->getImages();
145 return isset( $images[$name] ) ? $images[$name] : null;
146 }
147
148 /**
149 * Get ResourceLoaderImage objects for all images.
150 * @return ResourceLoaderImage[] Array keyed by image name
151 */
152 public function getImages() {
153 if ( !isset( $this->imageObjects ) ) {
154 $this->imageObjects = array();
155
156 foreach ( $this->images as $type => $list ) {
157 foreach ( $list as $name => $options ) {
158 $imageDesc = is_string( $options ) ? $options : $options['image'];
159
160 $allowedVariants = array_merge(
161 isset( $options['variants'] ) ? $options['variants'] : array(),
162 $this->getGlobalVariants( $type )
163 );
164 if ( isset( $this->variants[$type] ) ) {
165 $variantConfig = array_intersect_key(
166 $this->variants[$type],
167 array_fill_keys( $allowedVariants, true )
168 );
169 } else {
170 $variantConfig = array();
171 }
172
173 $image = new ResourceLoaderImage(
174 $name,
175 $this->getName(),
176 $imageDesc,
177 $this->localBasePath,
178 $variantConfig
179 );
180 $this->imageObjects[ $image->getName() ] = $image;
181 }
182 }
183 }
184
185 return $this->imageObjects;
186 }
187
188 /**
189 * Get list of variants in this module that are 'global' for given type of images, i.e., available
190 * for every image of given type regardless of image options.
191 * @param string $type Image type
192 * @return string[]
193 */
194 public function getGlobalVariants( $type ) {
195 if ( !isset( $this->globalVariants[$type] ) ) {
196 $this->globalVariants[$type] = array();
197
198 if ( isset( $this->variants[$type] ) ) {
199 foreach ( $this->variants[$type] as $name => $config ) {
200 if ( isset( $config['global'] ) && $config['global'] ) {
201 $this->globalVariants[$type][] = $name;
202 }
203 }
204 }
205 }
206
207 return $this->globalVariants[$type];
208 }
209
210 /**
211 * Get the type of given image.
212 * @param string $imageName Image name
213 * @return string
214 */
215 public function getImageType( $imageName ) {
216 foreach ( $this->images as $type => $list ) {
217 foreach ( $list as $key => $value ) {
218 $file = is_int( $key ) ? $value : $key;
219 $options = is_array( $value ) ? $value : array();
220 $name = isset( $options['name'] ) ? $options['name'] : pathinfo( $file, PATHINFO_FILENAME );
221 if ( $name === $imageName ) {
222 return $type;
223 }
224 }
225 }
226 }
227
228 /**
229 * @param ResourceLoaderContext $context
230 * @return array
231 */
232 public function getStyles( ResourceLoaderContext $context ) {
233 // Build CSS rules
234 $rules = array();
235 $script = $context->getResourceLoader()->getLoadScript( $this->getSource() );
236 $prefix = $this->getPrefix();
237
238 foreach ( $this->getImages() as $name => $image ) {
239 $type = $this->getImageType( $name );
240
241 $declarations = $this->getCssDeclarations(
242 $image->getDataUri( $context, null, 'original' ),
243 $image->getUrl( $context, $script, null, 'rasterized' )
244 );
245 $declarations = implode( "\n\t", $declarations );
246 $rules[] = ".$prefix-$type-$name {\n\t$declarations\n}";
247
248 // TODO: Get variant configurations from $context->getSkin()
249 foreach ( $image->getVariants() as $variant ) {
250 $declarations = $this->getCssDeclarations(
251 $image->getDataUri( $context, $variant, 'original' ),
252 $image->getUrl( $context, $script, $variant, 'rasterized' )
253 );
254 $declarations = implode( "\n\t", $declarations );
255 $rules[] = ".$prefix-$type-$name-$variant {\n\t$declarations\n}";
256 }
257 }
258
259 $style = implode( "\n", $rules );
260 if ( $this->getFlip( $context ) ) {
261 $style = CSSJanus::transform( $style, true, false );
262 }
263 return array( 'all' => $style );
264 }
265
266 /**
267 * SVG support using a transparent gradient to guarantee cross-browser
268 * compatibility (browsers able to understand gradient syntax support also SVG).
269 * http://pauginer.tumblr.com/post/36614680636/invisible-gradient-technique
270 *
271 * Keep synchronized with the .background-image-svg LESS mixin in
272 * /resources/src/mediawiki.less/mediawiki.mixins.less.
273 *
274 * @param string $primary Primary URI
275 * @param string $fallback Fallback URI
276 * @return string[] CSS declarations to use given URIs as background-image
277 */
278 protected function getCssDeclarations( $primary, $fallback ) {
279 return array(
280 "background-image: url($fallback);",
281 "background-image: -webkit-linear-gradient(transparent, transparent), url($primary);",
282 "background-image: linear-gradient(transparent, transparent), url($primary);",
283 "background-image: -o-linear-gradient(transparent, transparent), url($fallback);",
284 );
285 }
286
287 /**
288 * @return bool
289 */
290 public function supportsURLLoading() {
291 return false;
292 }
293
294 /**
295 * Extract a local base path from module definition information.
296 *
297 * @param array $options Module definition
298 * @param string $localBasePath Path to use if not provided in module definition. Defaults
299 * to $IP
300 * @return string Local base path
301 */
302 public static function extractLocalBasePath( $options, $localBasePath = null ) {
303 global $IP;
304
305 if ( $localBasePath === null ) {
306 $localBasePath = $IP;
307 }
308
309 if ( array_key_exists( 'localBasePath', $options ) ) {
310 $localBasePath = (string)$options['localBasePath'];
311 }
312
313 return $localBasePath;
314 }
315 }