MediaWiki REL1_32
ResourceLoaderSkinModule.php
Go to the documentation of this file.
1<?php
28 public $targets = [ 'desktop', 'mobile' ];
29
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
84 return $this->getLogoPreloadlinks();
85 }
86
91 private function getLogoPreloadlinks() {
92 $logo = $this->getLogoData( $this->getConfig() );
93
94 $tags = [];
95 $logosPerDppx = [];
96 $logos = [];
97
98 $preloadLinks = [];
99
100 if ( !is_array( $logo ) ) {
101 // No media queries required if we only have one variant
102 $preloadLinks[ $logo ] = [ 'as' => 'image' ];
103 return $preloadLinks;
104 }
105
106 if ( isset( $logo['svg'] ) ) {
107 // No media queries required if we only have a 1x and svg variant
108 // because all preload-capable browsers support SVGs
109 $preloadLinks [ $logo['svg'] ] = [ 'as' => 'image' ];
110 return $preloadLinks;
111 }
112
113 foreach ( $logo as $dppx => $src ) {
114 // Keys are in this format: "1.5x"
115 $dppx = substr( $dppx, 0, -1 );
116 $logosPerDppx[$dppx] = $src;
117 }
118
119 // Because PHP can't have floats as array keys
120 uksort( $logosPerDppx, function ( $a, $b ) {
121 $a = floatval( $a );
122 $b = floatval( $b );
123 // Sort from smallest to largest (e.g. 1x, 1.5x, 2x)
124 return $a <=> $b;
125 } );
126
127 foreach ( $logosPerDppx as $dppx => $src ) {
128 $logos[] = [ 'dppx' => $dppx, 'src' => $src ];
129 }
130
131 $logosCount = count( $logos );
132 // Logic must match ResourceLoaderSkinModule:
133 // - 1x applies to resolution < 1.5dppx
134 // - 1.5x applies to resolution >= 1.5dppx && < 2dppx
135 // - 2x applies to resolution >= 2dppx
136 // Note that min-resolution and max-resolution are both inclusive.
137 for ( $i = 0; $i < $logosCount; $i++ ) {
138 if ( $i === 0 ) {
139 // Smallest dppx
140 // min-resolution is ">=" (larger than or equal to)
141 // "not min-resolution" is essentially "<"
142 $media_query = 'not all and (min-resolution: ' . $logos[ 1 ]['dppx'] . 'dppx)';
143 } elseif ( $i !== $logosCount - 1 ) {
144 // In between
145 // Media query expressions can only apply "not" to the entire expression
146 // (e.g. can't express ">= 1.5 and not >= 2).
147 // Workaround: Use <= 1.9999 in place of < 2.
148 $upper_bound = floatval( $logos[ $i + 1 ]['dppx'] ) - 0.000001;
149 $media_query = '(min-resolution: ' . $logos[ $i ]['dppx'] .
150 'dppx) and (max-resolution: ' . $upper_bound . 'dppx)';
151 } else {
152 // Largest dppx
153 $media_query = '(min-resolution: ' . $logos[ $i ]['dppx'] . 'dppx)';
154 }
155
156 $preloadLinks[ $logos[$i]['src'] ] = [ 'as' => 'image', 'media' => $media_query ];
157 }
158
159 return $preloadLinks;
160 }
161
170 private function normalizeStyles( &$styles ) {
171 foreach ( $styles as $key => $val ) {
172 if ( !is_array( $val ) ) {
173 $styles[$key] = [ $val ];
174 }
175 }
176 }
177
186 protected function getLogoData( Config $conf ) {
187 $logo = $conf->get( 'Logo' );
188 $logoHD = $conf->get( 'LogoHD' );
189
190 $logo1Url = OutputPage::transformResourcePath( $conf, $logo );
191
192 if ( !$logoHD ) {
193 return $logo1Url;
194 }
195
196 $logoUrls = [
197 '1x' => $logo1Url,
198 ];
199
200 if ( isset( $logoHD['svg'] ) ) {
201 $logoUrls['svg'] = OutputPage::transformResourcePath(
202 $conf,
203 $logoHD['svg']
204 );
205 } else {
206 // Only 1.5x and 2x are supported
207 if ( isset( $logoHD['1.5x'] ) ) {
208 $logoUrls['1.5x'] = OutputPage::transformResourcePath(
209 $conf,
210 $logoHD['1.5x']
211 );
212 }
213 if ( isset( $logoHD['2x'] ) ) {
214 $logoUrls['2x'] = OutputPage::transformResourcePath(
215 $conf,
216 $logoHD['2x']
217 );
218 }
219 }
220
221 return $logoUrls;
222 }
223
229 // Regardless of whether the files are specified, we always
230 // provide mw-wiki-logo styles.
231 return false;
232 }
233
235 $summary = parent::getDefinitionSummary( $context );
236 $summary[] = [
237 'logo' => $this->getConfig()->get( 'Logo' ),
238 'logoHD' => $this->getConfig()->get( 'LogoHD' ),
239 ];
240 return $summary;
241 }
242}
Object passed around to modules which contains information about the state of a specific loader reque...
ResourceLoader module based on local JavaScript/CSS files.
array $styles
List of paths to CSS files to always include.
$targets
All skins are assumed to be compatible with mobile.
getDefinitionSummary(ResourceLoaderContext $context)
Get the definition summary for this module.
normalizeStyles(&$styles)
Ensure all media keys use array values.
getStyles(ResourceLoaderContext $context)
getPreloadLinks(ResourceLoaderContext $context)
isKnownEmpty(ResourceLoaderContext $context)
getLogoPreloadlinks()
Helper method for getPreloadLinks()
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2885
Interface for configuration instances.
Definition Config.php:28
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".