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