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