MediaWiki  1.34.0
ResourceLoaderSkinModule.php
Go to the documentation of this file.
1 <?php
31  public $targets = [ 'desktop', 'mobile' ];
32 
38  $logo = $this->getLogoData( $this->getConfig() );
39  $styles = parent::getStyles( $context );
40  $this->normalizeStyles( $styles );
41 
42  $default = !is_array( $logo ) ? $logo : $logo['1x'];
43  $styles['all'][] = '.mw-wiki-logo { background-image: ' .
44  CSSMin::buildUrlValue( $default ) .
45  '; }';
46 
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; }';
55  } else {
56  if ( isset( $logo['1.5x'] ) ) {
57  $styles[
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; }';
65  }
66  if ( isset( $logo['2x'] ) ) {
67  $styles[
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; }';
75  }
76  }
77  }
78 
79  return $styles;
80  }
81 
87  return $this->getLogoPreloadlinks();
88  }
89 
94  private function getLogoPreloadlinks() {
95  $logo = $this->getLogoData( $this->getConfig() );
96 
97  $logosPerDppx = [];
98  $logos = [];
99 
100  $preloadLinks = [];
101 
102  if ( !is_array( $logo ) ) {
103  // No media queries required if we only have one variant
104  $preloadLinks[$logo] = [ 'as' => 'image' ];
105  return $preloadLinks;
106  }
107 
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;
113  }
114 
115  foreach ( $logo as $dppx => $src ) {
116  // Keys are in this format: "1.5x"
117  $dppx = substr( $dppx, 0, -1 );
118  $logosPerDppx[$dppx] = $src;
119  }
120 
121  // Because PHP can't have floats as array keys
122  uksort( $logosPerDppx, function ( $a, $b ) {
123  $a = floatval( $a );
124  $b = floatval( $b );
125  // Sort from smallest to largest (e.g. 1x, 1.5x, 2x)
126  return $a <=> $b;
127  } );
128 
129  foreach ( $logosPerDppx as $dppx => $src ) {
130  $logos[] = [
131  'dppx' => $dppx,
132  'src' => $src
133  ];
134  }
135 
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++ ) {
143  if ( $i === 0 ) {
144  // Smallest dppx
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 ) {
149  // In between
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)';
156  } else {
157  // Largest dppx
158  $media_query = '(min-resolution: ' . $logos[$i]['dppx'] . 'dppx)';
159  }
160 
161  $preloadLinks[$logos[$i]['src']] = [
162  'as' => 'image',
163  'media' => $media_query
164  ];
165  }
166 
167  return $preloadLinks;
168  }
169 
178  private function normalizeStyles( &$styles ) {
179  foreach ( $styles as $key => $val ) {
180  if ( !is_array( $val ) ) {
181  $styles[$key] = [ $val ];
182  }
183  }
184  }
185 
194  protected function getLogoData( Config $conf ) {
195  $logo = $conf->get( 'Logo' );
196  $logoHD = $conf->get( 'LogoHD' );
197 
198  $logo1Url = OutputPage::transformResourcePath( $conf, $logo );
199 
200  if ( !$logoHD ) {
201  return $logo1Url;
202  }
203 
204  $logoUrls = [
205  '1x' => $logo1Url,
206  ];
207 
208  if ( isset( $logoHD['svg'] ) ) {
209  $logoUrls['svg'] = OutputPage::transformResourcePath(
210  $conf,
211  $logoHD['svg']
212  );
213  } else {
214  // Only 1.5x and 2x are supported
215  if ( isset( $logoHD['1.5x'] ) ) {
216  $logoUrls['1.5x'] = OutputPage::transformResourcePath(
217  $conf,
218  $logoHD['1.5x']
219  );
220  }
221  if ( isset( $logoHD['2x'] ) ) {
222  $logoUrls['2x'] = OutputPage::transformResourcePath(
223  $conf,
224  $logoHD['2x']
225  );
226  }
227  }
228 
229  return $logoUrls;
230  }
231 
237  // Regardless of whether the files are specified, we always
238  // provide mw-wiki-logo styles.
239  return false;
240  }
241 
243  $summary = parent::getDefinitionSummary( $context );
244  $summary[] = [
245  'logo' => $this->getConfig()->get( 'Logo' ),
246  'logoHD' => $this->getConfig()->get( 'LogoHD' ),
247  ];
248  return $summary;
249  }
250 }
ResourceLoaderSkinModule\getPreloadLinks
getPreloadLinks(ResourceLoaderContext $context)
Definition: ResourceLoaderSkinModule.php:86
ResourceLoaderContext
Context object that contains information about the state of a specific ResourceLoader web request.
Definition: ResourceLoaderContext.php:33
ResourceLoaderFileModule\$styles
array $styles
List of paths to CSS files to always include.
Definition: ResourceLoaderFileModule.php:89
ResourceLoaderSkinModule\getDefinitionSummary
getDefinitionSummary(ResourceLoaderContext $context)
Get the definition summary for this module.
Definition: ResourceLoaderSkinModule.php:242
ResourceLoaderFileModule
Module based on local JavaScript/CSS files.
Definition: ResourceLoaderFileModule.php:35
Config
Interface for configuration instances.
Definition: Config.php:28
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
ResourceLoaderSkinModule\normalizeStyles
normalizeStyles(&$styles)
Ensure all media keys use array values.
Definition: ResourceLoaderSkinModule.php:178
ResourceLoaderSkinModule\getLogoData
getLogoData(Config $conf)
Definition: ResourceLoaderSkinModule.php:194
ResourceLoaderSkinModule\getLogoPreloadlinks
getLogoPreloadlinks()
Helper method for getPreloadLinks()
Definition: ResourceLoaderSkinModule.php:94
ResourceLoaderSkinModule\getStyles
getStyles(ResourceLoaderContext $context)
Definition: ResourceLoaderSkinModule.php:37
$context
$context
Definition: load.php:45
ResourceLoaderSkinModule\isKnownEmpty
isKnownEmpty(ResourceLoaderContext $context)
Definition: ResourceLoaderSkinModule.php:236
ResourceLoaderSkinModule
Module for skin stylesheets.
Definition: ResourceLoaderSkinModule.php:27
ResourceLoaderModule\getConfig
getConfig()
Definition: ResourceLoaderModule.php:200
ResourceLoaderSkinModule\$targets
$targets
All skins are assumed to be compatible with mobile.
Definition: ResourceLoaderSkinModule.php:31