resourceloader: Improve coverage of SkinModule
[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 * Ensure all media keys use array values.
81 *
82 * Normalises arrays returned by the ResourceLoaderFileModule::getStyles() method.
83 *
84 * @param array &$styles Associative array, keys are strings (media queries),
85 * values are strings or arrays
86 */
87 private function normalizeStyles( &$styles ) {
88 foreach ( $styles as $key => $val ) {
89 if ( !is_array( $val ) ) {
90 $styles[$key] = [ $val ];
91 }
92 }
93 }
94
95 /**
96 * Non-static proxy to ::getLogo (for overloading in sub classes or tests).
97 *
98 * @codeCoverageIgnore
99 * @since 1.31
100 * @param Config $conf
101 * @return string|array
102 */
103 protected function getLogoData( Config $conf ) {
104 return static::getLogo( $conf );
105 }
106
107 /**
108 * @param Config $conf
109 * @return string|array Single url if no variants are defined,
110 * or an array of logo urls keyed by dppx in form "<float>x".
111 * Key "1x" is always defined. Key "svg" may also be defined,
112 * in which case variants other than "1x" are omitted.
113 */
114 public static function getLogo( Config $conf ) {
115 $logo = $conf->get( 'Logo' );
116 $logoHD = $conf->get( 'LogoHD' );
117
118 $logo1Url = OutputPage::transformResourcePath( $conf, $logo );
119
120 if ( !$logoHD ) {
121 return $logo1Url;
122 }
123
124 $logoUrls = [
125 '1x' => $logo1Url,
126 ];
127
128 if ( isset( $logoHD['svg'] ) ) {
129 $logoUrls['svg'] = OutputPage::transformResourcePath(
130 $conf,
131 $logoHD['svg']
132 );
133 } else {
134 // Only 1.5x and 2x are supported
135 if ( isset( $logoHD['1.5x'] ) ) {
136 $logoUrls['1.5x'] = OutputPage::transformResourcePath(
137 $conf,
138 $logoHD['1.5x']
139 );
140 }
141 if ( isset( $logoHD['2x'] ) ) {
142 $logoUrls['2x'] = OutputPage::transformResourcePath(
143 $conf,
144 $logoHD['2x']
145 );
146 }
147 }
148
149 return $logoUrls;
150 }
151
152 /**
153 * @param ResourceLoaderContext $context
154 * @return bool
155 */
156 public function isKnownEmpty( ResourceLoaderContext $context ) {
157 // Regardless of whether the files are specified, we always
158 // provide mw-wiki-logo styles.
159 return false;
160 }
161
162 public function getDefinitionSummary( ResourceLoaderContext $context ) {
163 $summary = parent::getDefinitionSummary( $context );
164 $summary[] = [
165 'logo' => $this->getConfig()->get( 'Logo' ),
166 'logoHD' => $this->getConfig()->get( 'LogoHD' ),
167 ];
168 return $summary;
169 }
170 }