Use PHPUnit_Framework_TestCase in a few utils/ tests
[mediawiki.git] / includes / resourceloader / ResourceLoaderImageModule.php
blob9246df729ba04588366247c117e5b98968007f4d
1 <?php
2 /**
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
20 * @file
21 * @author Trevor Parscal
24 /**
25 * Resource loader module for generated and embedded images.
27 * @since 1.25
29 class ResourceLoaderImageModule extends ResourceLoaderModule {
31 /**
32 * Local base path, see __construct()
33 * @var string
35 protected $localBasePath = '';
37 protected $origin = self::ORIGIN_CORE_SITEWIDE;
39 protected $images = array();
40 protected $variants = array();
41 protected $prefix = array();
42 protected $targets = array( 'desktop', 'mobile' );
44 /**
45 * Constructs a new module from an options array.
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
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
84 public function __construct( $options = array(), $localBasePath = null ) {
85 $this->localBasePath = self::extractLocalBasePath( $options, $localBasePath );
87 if ( !isset( $options['prefix'] ) || !$options['prefix'] ) {
88 throw new MWException(
89 "Required 'prefix' option not given or empty."
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."
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."
107 $this->{$member}[$key] = (array)$value;
109 break;
111 case 'variants':
112 if ( !is_array( $option ) ) {
113 throw new MWException(
114 "Invalid variant list error. '$option' given, array expected."
117 $this->{$member} = $option;
118 break;
120 case 'prefix':
121 $this->{$member} = (string)$option;
122 break;
128 * Get CSS class prefix used by this module.
129 * @return string
131 public function getPrefix() {
132 return $this->prefix;
136 * Get a ResourceLoaderImage object for given image.
137 * @param string $name Image name
138 * @return ResourceLoaderImage|null
140 public function getImage( $name ) {
141 $images = $this->getImages();
142 return isset( $images[$name] ) ? $images[$name] : null;
146 * Get ResourceLoaderImage objects for all images.
147 * @return ResourceLoaderImage[] Array keyed by image name
149 public function getImages() {
150 if ( !isset( $this->imageObjects ) ) {
151 $this->imageObjects = array();
153 foreach ( $this->images as $type => $list ) {
154 foreach ( $list as $name => $options ) {
155 $imageDesc = is_string( $options ) ? $options : $options['image'];
157 $allowedVariants = array_merge(
158 isset( $options['variants'] ) ? $options['variants'] : array(),
159 $this->getGlobalVariants( $type )
161 $variantConfig = array_intersect_key(
162 $this->variants[$type],
163 array_fill_keys( $allowedVariants, true )
166 $image = new ResourceLoaderImage( $name, $this->getName(), $imageDesc, $this->localBasePath, $variantConfig );
167 $this->imageObjects[ $image->getName() ] = $image;
172 return $this->imageObjects;
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[]
181 public function getGlobalVariants( $type ) {
182 if ( !isset( $this->globalVariants[$type] ) ) {
183 $this->globalVariants[$type] = array();
185 foreach ( $this->variants[$type] as $name => $config ) {
186 if ( isset( $config['global'] ) && $config['global'] ) {
187 $this->globalVariants[$type][] = $name;
192 return $this->globalVariants[$type];
196 * Get the type of given image.
197 * @param string $imageName Image name
198 * @return string
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;
214 * @param ResourceLoaderContext $context
215 * @return array
217 public function getStyles( ResourceLoaderContext $context ) {
218 // Build CSS rules
219 $rules = array();
220 $script = $context->getResourceLoader()->getLoadScript( $this->getSource() );
221 $prefix = $this->getPrefix();
223 foreach ( $this->getImages() as $name => $image ) {
224 $type = $this->getImageType( $name );
226 $declarations = $this->getCssDeclarations(
227 $image->getDataUri( $context, null, 'original' ),
228 $image->getUrl( $context, $script, null, 'rasterized' )
230 $declarations = implode( "\n\t", $declarations );
231 $rules[] = ".$prefix-$type-$name {\n\t$declarations\n}";
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' )
239 $declarations = implode( "\n\t", $declarations );
240 $rules[] = ".$prefix-$type-$name-$variant {\n\t$declarations\n}";
244 $style = implode( "\n", $rules );
245 if ( $this->getFlip( $context ) ) {
246 $style = CSSJanus::transform( $style, true, false );
248 return array( 'all' => $style );
252 * @param string $primary Primary URI
253 * @param string $fallback Fallback URI
254 * @return string[] CSS declarations to use given URIs as background-image
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);",
268 * @return bool
270 public function supportsURLLoading() {
271 return false;
275 * Extract a local base path from module definition information.
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
282 public static function extractLocalBasePath( $options, $localBasePath = null ) {
283 global $IP;
285 if ( $localBasePath === null ) {
286 $localBasePath = $IP;
289 if ( array_key_exists( 'localBasePath', $options ) ) {
290 $localBasePath = (string)$options['localBasePath'];
293 return $localBasePath;