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.
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.
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
22 * Module for skin stylesheets.
24 * @ingroup ResourceLoader
27 class ResourceLoaderSkinModule
extends ResourceLoaderFileModule
{
29 * All skins are assumed to be compatible with mobile
31 public $targets = [ 'desktop', 'mobile' ];
34 * @param ResourceLoaderContext $context
37 public function getStyles( ResourceLoaderContext
$context ) {
38 $logo = $this->getLogoData( $this->getConfig() );
39 $styles = parent
::getStyles( $context );
40 $this->normalizeStyles( $styles );
42 $default = !is_array( $logo ) ?
$logo : $logo['1x'];
43 $styles['all'][] = '.mw-wiki-logo { background-image: ' .
44 CSSMin
::buildUrlValue( $default ) .
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; }';
56 if ( isset( $logo['1.5x'] ) ) {
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; }';
66 if ( isset( $logo['2x'] ) ) {
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; }';
83 * @param ResourceLoaderContext $context
86 public function getPreloadLinks( ResourceLoaderContext
$context ) {
87 return $this->getLogoPreloadlinks();
91 * Helper method for getPreloadLinks()
94 private function getLogoPreloadlinks() {
95 $logo = $this->getLogoData( $this->getConfig() );
102 if ( !is_array( $logo ) ) {
103 // No media queries required if we only have one variant
104 $preloadLinks[$logo] = [ 'as' => 'image' ];
105 return $preloadLinks;
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;
115 foreach ( $logo as $dppx => $src ) {
116 // Keys are in this format: "1.5x"
117 $dppx = substr( $dppx, 0, -1 );
118 $logosPerDppx[$dppx] = $src;
121 // Because PHP can't have floats as array keys
122 uksort( $logosPerDppx, function ( $a, $b ) {
125 // Sort from smallest to largest (e.g. 1x, 1.5x, 2x)
129 foreach ( $logosPerDppx as $dppx => $src ) {
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++
) {
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 ) {
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)';
158 $media_query = '(min-resolution: ' . $logos[$i]['dppx'] . 'dppx)';
161 $preloadLinks[$logos[$i]['src']] = [
163 'media' => $media_query
167 return $preloadLinks;
171 * Ensure all media keys use array values.
173 * Normalises arrays returned by the ResourceLoaderFileModule::getStyles() method.
175 * @param array &$styles Associative array, keys are strings (media queries),
176 * values are strings or arrays
178 private function normalizeStyles( &$styles ) {
179 foreach ( $styles as $key => $val ) {
180 if ( !is_array( $val ) ) {
181 $styles[$key] = [ $val ];
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.
194 protected function getLogoData( Config
$conf ) {
195 $logo = $conf->get( 'Logo' );
196 $logoHD = $conf->get( 'LogoHD' );
198 $logo1Url = OutputPage
::transformResourcePath( $conf, $logo );
208 if ( isset( $logoHD['svg'] ) ) {
209 $logoUrls['svg'] = OutputPage
::transformResourcePath(
214 // Only 1.5x and 2x are supported
215 if ( isset( $logoHD['1.5x'] ) ) {
216 $logoUrls['1.5x'] = OutputPage
::transformResourcePath(
221 if ( isset( $logoHD['2x'] ) ) {
222 $logoUrls['2x'] = OutputPage
::transformResourcePath(
233 * @param ResourceLoaderContext $context
236 public function isKnownEmpty( ResourceLoaderContext
$context ) {
237 // Regardless of whether the files are specified, we always
238 // provide mw-wiki-logo styles.
242 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
243 $summary = parent
::getDefinitionSummary( $context );
245 'logo' => $this->getConfig()->get( 'Logo' ),
246 'logoHD' => $this->getConfig()->get( 'LogoHD' ),