ResourceLoaderSkinModule.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. class ResourceLoaderSkinModule extends ResourceLoaderFileModule {
  24. /**
  25. * All skins are assumed to be compatible with mobile
  26. */
  27. public $targets = [ 'desktop', 'mobile' ];
  28. /**
  29. * @param ResourceLoaderContext $context
  30. * @return array
  31. */
  32. public function getStyles( ResourceLoaderContext $context ) {
  33. $logo = $this->getLogoData( $this->getConfig() );
  34. $styles = parent::getStyles( $context );
  35. $this->normalizeStyles( $styles );
  36. $default = !is_array( $logo ) ? $logo : $logo['1x'];
  37. $styles['all'][] = '.mw-wiki-logo { background-image: ' .
  38. CSSMin::buildUrlValue( $default ) .
  39. '; }';
  40. if ( is_array( $logo ) ) {
  41. if ( isset( $logo['svg'] ) ) {
  42. $styles['all'][] = '.mw-wiki-logo { ' .
  43. 'background-image: -webkit-linear-gradient(transparent, transparent), ' .
  44. CSSMin::buildUrlValue( $logo['svg'] ) . '; ' .
  45. 'background-image: linear-gradient(transparent, transparent), ' .
  46. CSSMin::buildUrlValue( $logo['svg'] ) . ';' .
  47. 'background-size: 135px auto; }';
  48. } else {
  49. if ( isset( $logo['1.5x'] ) ) {
  50. $styles[
  51. '(-webkit-min-device-pixel-ratio: 1.5), ' .
  52. '(min--moz-device-pixel-ratio: 1.5), ' .
  53. '(min-resolution: 1.5dppx), ' .
  54. '(min-resolution: 144dpi)'
  55. ][] = '.mw-wiki-logo { background-image: ' .
  56. CSSMin::buildUrlValue( $logo['1.5x'] ) . ';' .
  57. 'background-size: 135px auto; }';
  58. }
  59. if ( isset( $logo['2x'] ) ) {
  60. $styles[
  61. '(-webkit-min-device-pixel-ratio: 2), ' .
  62. '(min--moz-device-pixel-ratio: 2), ' .
  63. '(min-resolution: 2dppx), ' .
  64. '(min-resolution: 192dpi)'
  65. ][] = '.mw-wiki-logo { background-image: ' .
  66. CSSMin::buildUrlValue( $logo['2x'] ) . ';' .
  67. 'background-size: 135px auto; }';
  68. }
  69. }
  70. }
  71. return $styles;
  72. }
  73. /**
  74. * @param ResourceLoaderContext $context
  75. * @return array
  76. */
  77. public function getPreloadLinks( ResourceLoaderContext $context ) {
  78. return $this->getLogoPreloadlinks();
  79. }
  80. /**
  81. * Helper method for getPreloadLinks()
  82. * @return array
  83. */
  84. private function getLogoPreloadlinks() {
  85. $logo = $this->getLogoData( $this->getConfig() );
  86. $tags = [];
  87. $logosPerDppx = [];
  88. $logos = [];
  89. $preloadLinks = [];
  90. if ( !is_array( $logo ) ) {
  91. // No media queries required if we only have one variant
  92. $preloadLinks[ $logo ] = [ 'as' => 'image' ];
  93. return $preloadLinks;
  94. }
  95. if ( isset( $logo['svg'] ) ) {
  96. // No media queries required if we only have a 1x and svg variant
  97. // because all preload-capable browsers support SVGs
  98. $preloadLinks [ $logo['svg'] ] = [ 'as' => 'image' ];
  99. return $preloadLinks;
  100. }
  101. foreach ( $logo as $dppx => $src ) {
  102. // Keys are in this format: "1.5x"
  103. $dppx = substr( $dppx, 0, -1 );
  104. $logosPerDppx[$dppx] = $src;
  105. }
  106. // Because PHP can't have floats as array keys
  107. uksort( $logosPerDppx, function ( $a, $b ) {
  108. $a = floatval( $a );
  109. $b = floatval( $b );
  110. // Sort from smallest to largest (e.g. 1x, 1.5x, 2x)
  111. return $a <=> $b;
  112. } );
  113. foreach ( $logosPerDppx as $dppx => $src ) {
  114. $logos[] = [ 'dppx' => $dppx, 'src' => $src ];
  115. }
  116. $logosCount = count( $logos );
  117. // Logic must match ResourceLoaderSkinModule:
  118. // - 1x applies to resolution < 1.5dppx
  119. // - 1.5x applies to resolution >= 1.5dppx && < 2dppx
  120. // - 2x applies to resolution >= 2dppx
  121. // Note that min-resolution and max-resolution are both inclusive.
  122. for ( $i = 0; $i < $logosCount; $i++ ) {
  123. if ( $i === 0 ) {
  124. // Smallest dppx
  125. // min-resolution is ">=" (larger than or equal to)
  126. // "not min-resolution" is essentially "<"
  127. $media_query = 'not all and (min-resolution: ' . $logos[ 1 ]['dppx'] . 'dppx)';
  128. } elseif ( $i !== $logosCount - 1 ) {
  129. // In between
  130. // Media query expressions can only apply "not" to the entire expression
  131. // (e.g. can't express ">= 1.5 and not >= 2).
  132. // Workaround: Use <= 1.9999 in place of < 2.
  133. $upper_bound = floatval( $logos[ $i + 1 ]['dppx'] ) - 0.000001;
  134. $media_query = '(min-resolution: ' . $logos[ $i ]['dppx'] .
  135. 'dppx) and (max-resolution: ' . $upper_bound . 'dppx)';
  136. } else {
  137. // Largest dppx
  138. $media_query = '(min-resolution: ' . $logos[ $i ]['dppx'] . 'dppx)';
  139. }
  140. $preloadLinks[ $logos[$i]['src'] ] = [ 'as' => 'image', 'media' => $media_query ];
  141. }
  142. return $preloadLinks;
  143. }
  144. /**
  145. * Ensure all media keys use array values.
  146. *
  147. * Normalises arrays returned by the ResourceLoaderFileModule::getStyles() method.
  148. *
  149. * @param array &$styles Associative array, keys are strings (media queries),
  150. * values are strings or arrays
  151. */
  152. private function normalizeStyles( &$styles ) {
  153. foreach ( $styles as $key => $val ) {
  154. if ( !is_array( $val ) ) {
  155. $styles[$key] = [ $val ];
  156. }
  157. }
  158. }
  159. /**
  160. * @since 1.31
  161. * @param Config $conf
  162. * @return string|array Single url if no variants are defined,
  163. * or an array of logo urls keyed by dppx in form "<float>x".
  164. * Key "1x" is always defined. Key "svg" may also be defined,
  165. * in which case variants other than "1x" are omitted.
  166. */
  167. protected function getLogoData( Config $conf ) {
  168. $logo = $conf->get( 'Logo' );
  169. $logoHD = $conf->get( 'LogoHD' );
  170. $logo1Url = OutputPage::transformResourcePath( $conf, $logo );
  171. if ( !$logoHD ) {
  172. return $logo1Url;
  173. }
  174. $logoUrls = [
  175. '1x' => $logo1Url,
  176. ];
  177. if ( isset( $logoHD['svg'] ) ) {
  178. $logoUrls['svg'] = OutputPage::transformResourcePath(
  179. $conf,
  180. $logoHD['svg']
  181. );
  182. } else {
  183. // Only 1.5x and 2x are supported
  184. if ( isset( $logoHD['1.5x'] ) ) {
  185. $logoUrls['1.5x'] = OutputPage::transformResourcePath(
  186. $conf,
  187. $logoHD['1.5x']
  188. );
  189. }
  190. if ( isset( $logoHD['2x'] ) ) {
  191. $logoUrls['2x'] = OutputPage::transformResourcePath(
  192. $conf,
  193. $logoHD['2x']
  194. );
  195. }
  196. }
  197. return $logoUrls;
  198. }
  199. /**
  200. * @param ResourceLoaderContext $context
  201. * @return bool
  202. */
  203. public function isKnownEmpty( ResourceLoaderContext $context ) {
  204. // Regardless of whether the files are specified, we always
  205. // provide mw-wiki-logo styles.
  206. return false;
  207. }
  208. public function getDefinitionSummary( ResourceLoaderContext $context ) {
  209. $summary = parent::getDefinitionSummary( $context );
  210. $summary[] = [
  211. 'logo' => $this->getConfig()->get( 'Logo' ),
  212. 'logoHD' => $this->getConfig()->get( 'LogoHD' ),
  213. ];
  214. return $summary;
  215. }
  216. }