Merge "Add and use Title::getOtherPage()"
[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 $variantConfig = array_intersect_key(
162 $this->variants[$type],
163 array_fill_keys( $allowedVariants, true )
164 );
165
166 $image = new ResourceLoaderImage( $name, $this->getName(), $imageDesc, $this->localBasePath, $variantConfig );
167 $this->imageObjects[ $image->getName() ] = $image;
168 }
169 }
170 }
171
172 return $this->imageObjects;
173 }
174
175 /**
176 * Get list of variants in this module that are 'global' for given type of images, i.e., available
177 * for every image of given type regardless of image options.
178 * @param string $type Image type
179 * @return string[]
180 */
181 public function getGlobalVariants( $type ) {
182 if ( !isset( $this->globalVariants[$type] ) ) {
183 $this->globalVariants[$type] = array();
184
185 foreach ( $this->variants[$type] as $name => $config ) {
186 if ( isset( $config['global'] ) && $config['global'] ) {
187 $this->globalVariants[$type][] = $name;
188 }
189 }
190 }
191
192 return $this->globalVariants[$type];
193 }
194
195 /**
196 * Get the type of given image.
197 * @param string $imageName Image name
198 * @return string
199 */
200 public function getImageType( $imageName ) {
201 foreach ( $this->images as $type => $list ) {
202 foreach ( $list as $key => $value ) {
203 $file = is_int( $key ) ? $value : $key;
204 $options = is_array( $value ) ? $value : array();
205 $name = isset( $options['name'] ) ? $options['name'] : pathinfo( $file, PATHINFO_FILENAME );
206 if ( $name === $imageName ) {
207 return $type;
208 }
209 }
210 }
211 }
212
213 /**
214 * @param ResourceLoaderContext $context
215 * @return array
216 */
217 public function getStyles( ResourceLoaderContext $context ) {
218 // Build CSS rules
219 $rules = array();
220 $script = $context->getResourceLoader()->getLoadScript( $this->getSource() );
221 $prefix = $this->getPrefix();
222
223 foreach ( $this->getImages() as $name => $image ) {
224 $type = $this->getImageType( $name );
225
226 $declarations = $this->getCssDeclarations(
227 $image->getDataUri( $context, null, 'original' ),
228 $image->getUrl( $context, $script, null, 'rasterized' )
229 );
230 $declarations = implode( "\n\t", $declarations );
231 $rules[] = ".$prefix-$type-$name {\n\t$declarations\n}";
232
233 // TODO: Get variant configurations from $context->getSkin()
234 foreach ( $image->getVariants() as $variant ) {
235 $declarations = $this->getCssDeclarations(
236 $image->getDataUri( $context, $variant, 'original' ),
237 $image->getUrl( $context, $script, $variant, 'rasterized' )
238 );
239 $declarations = implode( "\n\t", $declarations );
240 $rules[] = ".$prefix-$type-$name-$variant {\n\t$declarations\n}";
241 }
242 }
243
244 $style = implode( "\n", $rules );
245 if ( $this->getFlip( $context ) ) {
246 $style = CSSJanus::transform( $style, true, false );
247 }
248 return array( 'all' => $style );
249 }
250
251 /**
252 * @param string $primary Primary URI
253 * @param string $fallback Fallback URI
254 * @return string[] CSS declarations to use given URIs as background-image
255 */
256 protected function getCssDeclarations( $primary, $fallback ) {
257 // SVG support using a transparent gradient to guarantee cross-browser
258 // compatibility (browsers able to understand gradient syntax support also SVG).
259 // http://pauginer.tumblr.com/post/36614680636/invisible-gradient-technique
260 return array(
261 "background-image: url($fallback);",
262 "background-image: -webkit-linear-gradient(transparent, transparent), url($primary);",
263 "background-image: linear-gradient(transparent, transparent), url($primary);",
264 );
265 }
266
267 /**
268 * @return bool
269 */
270 public function supportsURLLoading() {
271 return false;
272 }
273
274 /**
275 * Extract a local base path from module definition information.
276 *
277 * @param array $options Module definition
278 * @param string $localBasePath Path to use if not provided in module definition. Defaults
279 * to $IP
280 * @return string Local base path
281 */
282 public static function extractLocalBasePath( $options, $localBasePath = null ) {
283 global $IP;
284
285 if ( $localBasePath === null ) {
286 $localBasePath = $IP;
287 }
288
289 if ( array_key_exists( 'localBasePath', $options ) ) {
290 $localBasePath = (string)$options['localBasePath'];
291 }
292
293 return $localBasePath;
294 }
295 }