Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderSkinModule.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * Module for skin stylesheets.
23 *
24 * @ingroup ResourceLoader
25 * @internal
26 */
27 class ResourceLoaderSkinModule extends ResourceLoaderFileModule {
28 /**
29 * All skins are assumed to be compatible with mobile
30 */
31 public $targets = [ 'desktop', 'mobile' ];
32
33 /**
34 * @param ResourceLoaderContext $context
35 * @return array
36 */
37 public function getStyles( ResourceLoaderContext $context ) {
38 $logo = $this->getLogoData( $this->getConfig() );
39 $styles = parent::getStyles( $context );
40 $this->normalizeStyles( $styles );
41
42 $default = !is_array( $logo ) ? $logo : $logo['1x'];
43 $styles['all'][] = '.mw-wiki-logo { background-image: ' .
44 CSSMin::buildUrlValue( $default ) .
45 '; }';
46
47 if ( is_array( $logo ) ) {
48 if ( isset( $logo['svg'] ) ) {
49 $styles['all'][] = '.mw-wiki-logo { ' .
50 'background-image: -webkit-linear-gradient(transparent, transparent), ' .
51 CSSMin::buildUrlValue( $logo['svg'] ) . '; ' .
52 'background-image: linear-gradient(transparent, transparent), ' .
53 CSSMin::buildUrlValue( $logo['svg'] ) . ';' .
54 'background-size: 135px auto; }';
55 } else {
56 if ( isset( $logo['1.5x'] ) ) {
57 $styles[
58 '(-webkit-min-device-pixel-ratio: 1.5), ' .
59 '(min--moz-device-pixel-ratio: 1.5), ' .
60 '(min-resolution: 1.5dppx), ' .
61 '(min-resolution: 144dpi)'
62 ][] = '.mw-wiki-logo { background-image: ' .
63 CSSMin::buildUrlValue( $logo['1.5x'] ) . ';' .
64 'background-size: 135px auto; }';
65 }
66 if ( isset( $logo['2x'] ) ) {
67 $styles[
68 '(-webkit-min-device-pixel-ratio: 2), ' .
69 '(min--moz-device-pixel-ratio: 2), ' .
70 '(min-resolution: 2dppx), ' .
71 '(min-resolution: 192dpi)'
72 ][] = '.mw-wiki-logo { background-image: ' .
73 CSSMin::buildUrlValue( $logo['2x'] ) . ';' .
74 'background-size: 135px auto; }';
75 }
76 }
77 }
78
79 return $styles;
80 }
81
82 /**
83 * @param ResourceLoaderContext $context
84 * @return array
85 */
86 public function getPreloadLinks( ResourceLoaderContext $context ) {
87 return $this->getLogoPreloadlinks();
88 }
89
90 /**
91 * Helper method for getPreloadLinks()
92 * @return array
93 */
94 private function getLogoPreloadlinks() {
95 $logo = $this->getLogoData( $this->getConfig() );
96
97 $logosPerDppx = [];
98 $logos = [];
99
100 $preloadLinks = [];
101
102 if ( !is_array( $logo ) ) {
103 // No media queries required if we only have one variant
104 $preloadLinks[$logo] = [ 'as' => 'image' ];
105 return $preloadLinks;
106 }
107
108 if ( isset( $logo['svg'] ) ) {
109 // No media queries required if we only have a 1x and svg variant
110 // because all preload-capable browsers support SVGs
111 $preloadLinks[$logo['svg']] = [ 'as' => 'image' ];
112 return $preloadLinks;
113 }
114
115 foreach ( $logo as $dppx => $src ) {
116 // Keys are in this format: "1.5x"
117 $dppx = substr( $dppx, 0, -1 );
118 $logosPerDppx[$dppx] = $src;
119 }
120
121 // Because PHP can't have floats as array keys
122 uksort( $logosPerDppx, function ( $a, $b ) {
123 $a = floatval( $a );
124 $b = floatval( $b );
125 // Sort from smallest to largest (e.g. 1x, 1.5x, 2x)
126 return $a <=> $b;
127 } );
128
129 foreach ( $logosPerDppx as $dppx => $src ) {
130 $logos[] = [
131 'dppx' => $dppx,
132 'src' => $src
133 ];
134 }
135
136 $logosCount = count( $logos );
137 // Logic must match ResourceLoaderSkinModule:
138 // - 1x applies to resolution < 1.5dppx
139 // - 1.5x applies to resolution >= 1.5dppx && < 2dppx
140 // - 2x applies to resolution >= 2dppx
141 // Note that min-resolution and max-resolution are both inclusive.
142 for ( $i = 0; $i < $logosCount; $i++ ) {
143 if ( $i === 0 ) {
144 // Smallest dppx
145 // min-resolution is ">=" (larger than or equal to)
146 // "not min-resolution" is essentially "<"
147 $media_query = 'not all and (min-resolution: ' . $logos[1]['dppx'] . 'dppx)';
148 } elseif ( $i !== $logosCount - 1 ) {
149 // In between
150 // Media query expressions can only apply "not" to the entire expression
151 // (e.g. can't express ">= 1.5 and not >= 2).
152 // Workaround: Use <= 1.9999 in place of < 2.
153 $upper_bound = floatval( $logos[$i + 1]['dppx'] ) - 0.000001;
154 $media_query = '(min-resolution: ' . $logos[$i]['dppx'] .
155 'dppx) and (max-resolution: ' . $upper_bound . 'dppx)';
156 } else {
157 // Largest dppx
158 $media_query = '(min-resolution: ' . $logos[$i]['dppx'] . 'dppx)';
159 }
160
161 $preloadLinks[$logos[$i]['src']] = [
162 'as' => 'image',
163 'media' => $media_query
164 ];
165 }
166
167 return $preloadLinks;
168 }
169
170 /**
171 * Ensure all media keys use array values.
172 *
173 * Normalises arrays returned by the ResourceLoaderFileModule::getStyles() method.
174 *
175 * @param array &$styles Associative array, keys are strings (media queries),
176 * values are strings or arrays
177 */
178 private function normalizeStyles( &$styles ) {
179 foreach ( $styles as $key => $val ) {
180 if ( !is_array( $val ) ) {
181 $styles[$key] = [ $val ];
182 }
183 }
184 }
185
186 /**
187 * @since 1.31
188 * @param Config $conf
189 * @return string|array Single url if no variants are defined,
190 * or an array of logo urls keyed by dppx in form "<float>x".
191 * Key "1x" is always defined. Key "svg" may also be defined,
192 * in which case variants other than "1x" are omitted.
193 */
194 protected function getLogoData( Config $conf ) {
195 $logo = $conf->get( 'Logo' );
196 $logoHD = $conf->get( 'LogoHD' );
197
198 $logo1Url = OutputPage::transformResourcePath( $conf, $logo );
199
200 if ( !$logoHD ) {
201 return $logo1Url;
202 }
203
204 $logoUrls = [
205 '1x' => $logo1Url,
206 ];
207
208 if ( isset( $logoHD['svg'] ) ) {
209 $logoUrls['svg'] = OutputPage::transformResourcePath(
210 $conf,
211 $logoHD['svg']
212 );
213 } else {
214 // Only 1.5x and 2x are supported
215 if ( isset( $logoHD['1.5x'] ) ) {
216 $logoUrls['1.5x'] = OutputPage::transformResourcePath(
217 $conf,
218 $logoHD['1.5x']
219 );
220 }
221 if ( isset( $logoHD['2x'] ) ) {
222 $logoUrls['2x'] = OutputPage::transformResourcePath(
223 $conf,
224 $logoHD['2x']
225 );
226 }
227 }
228
229 return $logoUrls;
230 }
231
232 /**
233 * @param ResourceLoaderContext $context
234 * @return bool
235 */
236 public function isKnownEmpty( ResourceLoaderContext $context ) {
237 // Regardless of whether the files are specified, we always
238 // provide mw-wiki-logo styles.
239 return false;
240 }
241
242 public function getDefinitionSummary( ResourceLoaderContext $context ) {
243 $summary = parent::getDefinitionSummary( $context );
244 $summary[] = [
245 'logo' => $this->getConfig()->get( 'Logo' ),
246 'logoHD' => $this->getConfig()->get( 'LogoHD' ),
247 ];
248 return $summary;
249 }
250 }