Merge "jobqueue: change MWException throw statements to using JobQueueError"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderSkinModule.php
1 <?php
2 /**
3 * ResourceLoader module for skin stylesheets.
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 Timo Tijhof
22 */
23
24 class ResourceLoaderSkinModule extends ResourceLoaderFileModule {
25 /**
26 * All skins are assumed to be compatible with mobile
27 */
28 public $targets = [ 'desktop', 'mobile' ];
29
30 /**
31 * @param ResourceLoaderContext $context
32 * @return array
33 */
34 public function getStyles( ResourceLoaderContext $context ) {
35 $logo = $this->getLogoData( $this->getConfig() );
36 $styles = parent::getStyles( $context );
37 $this->normalizeStyles( $styles );
38
39 $default = !is_array( $logo ) ? $logo : $logo['1x'];
40 $styles['all'][] = '.mw-wiki-logo { background-image: ' .
41 CSSMin::buildUrlValue( $default ) .
42 '; }';
43
44 if ( is_array( $logo ) ) {
45 if ( isset( $logo['svg'] ) ) {
46 $styles['all'][] = '.mw-wiki-logo { ' .
47 'background-image: -webkit-linear-gradient(transparent, transparent), ' .
48 CSSMin::buildUrlValue( $logo['svg'] ) . '; ' .
49 'background-image: linear-gradient(transparent, transparent), ' .
50 CSSMin::buildUrlValue( $logo['svg'] ) . ';' .
51 'background-size: 135px auto; }';
52 } else {
53 if ( isset( $logo['1.5x'] ) ) {
54 $styles[
55 '(-webkit-min-device-pixel-ratio: 1.5), ' .
56 '(min--moz-device-pixel-ratio: 1.5), ' .
57 '(min-resolution: 1.5dppx), ' .
58 '(min-resolution: 144dpi)'
59 ][] = '.mw-wiki-logo { background-image: ' .
60 CSSMin::buildUrlValue( $logo['1.5x'] ) . ';' .
61 'background-size: 135px auto; }';
62 }
63 if ( isset( $logo['2x'] ) ) {
64 $styles[
65 '(-webkit-min-device-pixel-ratio: 2), ' .
66 '(min--moz-device-pixel-ratio: 2), ' .
67 '(min-resolution: 2dppx), ' .
68 '(min-resolution: 192dpi)'
69 ][] = '.mw-wiki-logo { background-image: ' .
70 CSSMin::buildUrlValue( $logo['2x'] ) . ';' .
71 'background-size: 135px auto; }';
72 }
73 }
74 }
75
76 return $styles;
77 }
78
79 /**
80 * @param ResourceLoaderContext $context
81 * @return array
82 */
83 public function getPreloadLinks( ResourceLoaderContext $context ) {
84 return $this->getLogoPreloadlinks();
85 }
86
87 /**
88 * Helper method for getPreloadLinks()
89 * @return array
90 */
91 private function getLogoPreloadlinks() {
92 $logo = $this->getLogoData( $this->getConfig() );
93
94 $logosPerDppx = [];
95 $logos = [];
96
97 $preloadLinks = [];
98
99 if ( !is_array( $logo ) ) {
100 // No media queries required if we only have one variant
101 $preloadLinks[ $logo ] = [ 'as' => 'image' ];
102 return $preloadLinks;
103 }
104
105 if ( isset( $logo['svg'] ) ) {
106 // No media queries required if we only have a 1x and svg variant
107 // because all preload-capable browsers support SVGs
108 $preloadLinks [ $logo['svg'] ] = [ 'as' => 'image' ];
109 return $preloadLinks;
110 }
111
112 foreach ( $logo as $dppx => $src ) {
113 // Keys are in this format: "1.5x"
114 $dppx = substr( $dppx, 0, -1 );
115 $logosPerDppx[$dppx] = $src;
116 }
117
118 // Because PHP can't have floats as array keys
119 uksort( $logosPerDppx, function ( $a, $b ) {
120 $a = floatval( $a );
121 $b = floatval( $b );
122 // Sort from smallest to largest (e.g. 1x, 1.5x, 2x)
123 return $a <=> $b;
124 } );
125
126 foreach ( $logosPerDppx as $dppx => $src ) {
127 $logos[] = [ 'dppx' => $dppx, 'src' => $src ];
128 }
129
130 $logosCount = count( $logos );
131 // Logic must match ResourceLoaderSkinModule:
132 // - 1x applies to resolution < 1.5dppx
133 // - 1.5x applies to resolution >= 1.5dppx && < 2dppx
134 // - 2x applies to resolution >= 2dppx
135 // Note that min-resolution and max-resolution are both inclusive.
136 for ( $i = 0; $i < $logosCount; $i++ ) {
137 if ( $i === 0 ) {
138 // Smallest dppx
139 // min-resolution is ">=" (larger than or equal to)
140 // "not min-resolution" is essentially "<"
141 $media_query = 'not all and (min-resolution: ' . $logos[ 1 ]['dppx'] . 'dppx)';
142 } elseif ( $i !== $logosCount - 1 ) {
143 // In between
144 // Media query expressions can only apply "not" to the entire expression
145 // (e.g. can't express ">= 1.5 and not >= 2).
146 // Workaround: Use <= 1.9999 in place of < 2.
147 $upper_bound = floatval( $logos[ $i + 1 ]['dppx'] ) - 0.000001;
148 $media_query = '(min-resolution: ' . $logos[ $i ]['dppx'] .
149 'dppx) and (max-resolution: ' . $upper_bound . 'dppx)';
150 } else {
151 // Largest dppx
152 $media_query = '(min-resolution: ' . $logos[ $i ]['dppx'] . 'dppx)';
153 }
154
155 $preloadLinks[ $logos[$i]['src'] ] = [ 'as' => 'image', 'media' => $media_query ];
156 }
157
158 return $preloadLinks;
159 }
160
161 /**
162 * Ensure all media keys use array values.
163 *
164 * Normalises arrays returned by the ResourceLoaderFileModule::getStyles() method.
165 *
166 * @param array &$styles Associative array, keys are strings (media queries),
167 * values are strings or arrays
168 */
169 private function normalizeStyles( &$styles ) {
170 foreach ( $styles as $key => $val ) {
171 if ( !is_array( $val ) ) {
172 $styles[$key] = [ $val ];
173 }
174 }
175 }
176
177 /**
178 * @since 1.31
179 * @param Config $conf
180 * @return string|array Single url if no variants are defined,
181 * or an array of logo urls keyed by dppx in form "<float>x".
182 * Key "1x" is always defined. Key "svg" may also be defined,
183 * in which case variants other than "1x" are omitted.
184 */
185 protected function getLogoData( Config $conf ) {
186 $logo = $conf->get( 'Logo' );
187 $logoHD = $conf->get( 'LogoHD' );
188
189 $logo1Url = OutputPage::transformResourcePath( $conf, $logo );
190
191 if ( !$logoHD ) {
192 return $logo1Url;
193 }
194
195 $logoUrls = [
196 '1x' => $logo1Url,
197 ];
198
199 if ( isset( $logoHD['svg'] ) ) {
200 $logoUrls['svg'] = OutputPage::transformResourcePath(
201 $conf,
202 $logoHD['svg']
203 );
204 } else {
205 // Only 1.5x and 2x are supported
206 if ( isset( $logoHD['1.5x'] ) ) {
207 $logoUrls['1.5x'] = OutputPage::transformResourcePath(
208 $conf,
209 $logoHD['1.5x']
210 );
211 }
212 if ( isset( $logoHD['2x'] ) ) {
213 $logoUrls['2x'] = OutputPage::transformResourcePath(
214 $conf,
215 $logoHD['2x']
216 );
217 }
218 }
219
220 return $logoUrls;
221 }
222
223 /**
224 * @param ResourceLoaderContext $context
225 * @return bool
226 */
227 public function isKnownEmpty( ResourceLoaderContext $context ) {
228 // Regardless of whether the files are specified, we always
229 // provide mw-wiki-logo styles.
230 return false;
231 }
232
233 public function getDefinitionSummary( ResourceLoaderContext $context ) {
234 $summary = parent::getDefinitionSummary( $context );
235 $summary[] = [
236 'logo' => $this->getConfig()->get( 'Logo' ),
237 'logoHD' => $this->getConfig()->get( 'LogoHD' ),
238 ];
239 return $summary;
240 }
241 }