Merge "MimeAnalyzer: Add testcases for mp3 detection"
[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->getLogo( $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['1.5x'] ) ) {
46 $styles[
47 '(-webkit-min-device-pixel-ratio: 1.5), ' .
48 '(min--moz-device-pixel-ratio: 1.5), ' .
49 '(min-resolution: 1.5dppx), ' .
50 '(min-resolution: 144dpi)'
51 ][] = '.mw-wiki-logo { background-image: ' .
52 CSSMin::buildUrlValue( $logo['1.5x'] ) . ';' .
53 'background-size: 135px auto; }';
54 }
55 if ( isset( $logo['2x'] ) ) {
56 $styles[
57 '(-webkit-min-device-pixel-ratio: 2), ' .
58 '(min--moz-device-pixel-ratio: 2),' .
59 '(min-resolution: 2dppx), ' .
60 '(min-resolution: 192dpi)'
61 ][] = '.mw-wiki-logo { background-image: ' .
62 CSSMin::buildUrlValue( $logo['2x'] ) . ';' .
63 'background-size: 135px auto; }';
64 }
65 }
66
67 return $styles;
68 }
69
70 /**
71 * Ensure all media keys use array values.
72 *
73 * Normalises arrays returned by the ResourceLoaderFileModule::getStyles() method.
74 *
75 * @param array &$styles Associative array, keys are strings (media queries),
76 * values are strings or arrays
77 */
78 private function normalizeStyles( &$styles ) {
79 foreach ( $styles as $key => $val ) {
80 if ( !is_array( $val ) ) {
81 $styles[$key] = [ $val ];
82 }
83 }
84 }
85
86 /**
87 * @param Config $conf
88 * @return string|array Single url if no variants are defined
89 * or array of logo urls keyed by dppx in form "<float>x".
90 * Key "1x" is always defined.
91 */
92 public static function getLogo( Config $conf ) {
93 $logo = $conf->get( 'Logo' );
94 $logoHD = $conf->get( 'LogoHD' );
95
96 $logo1Url = OutputPage::transformResourcePath( $conf, $logo );
97
98 if ( !$logoHD ) {
99 return $logo1Url;
100 }
101
102 $logoUrls = [
103 '1x' => $logo1Url,
104 ];
105
106 // Only 1.5x and 2x are supported
107 if ( isset( $logoHD['1.5x'] ) ) {
108 $logoUrls['1.5x'] = OutputPage::transformResourcePath(
109 $conf,
110 $logoHD['1.5x']
111 );
112 }
113 if ( isset( $logoHD['2x'] ) ) {
114 $logoUrls['2x'] = OutputPage::transformResourcePath(
115 $conf,
116 $logoHD['2x']
117 );
118 }
119
120 return $logoUrls;
121 }
122
123 /**
124 * @param ResourceLoaderContext $context
125 * @return bool
126 */
127 public function isKnownEmpty( ResourceLoaderContext $context ) {
128 // Regardless of whether the files are specified, we always
129 // provide mw-wiki-logo styles.
130 return false;
131 }
132
133 public function getDefinitionSummary( ResourceLoaderContext $context ) {
134 $summary = parent::getDefinitionSummary( $context );
135 $summary[] = [
136 'logo' => $this->getConfig()->get( 'Logo' ),
137 'logoHD' => $this->getConfig()->get( 'LogoHD' ),
138 ];
139 return $summary;
140 }
141 }