ResourceLoaderOOUIModule.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  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.
  7. *
  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.
  12. *
  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
  17. *
  18. * @file
  19. */
  20. /**
  21. * Convenience methods for dealing with OOUI themes and their relations to MW skins.
  22. *
  23. * @since 1.30
  24. */
  25. trait ResourceLoaderOOUIModule {
  26. protected static $knownScriptsModules = [ 'core' ];
  27. protected static $knownStylesModules = [ 'core', 'widgets', 'toolbars', 'windows' ];
  28. protected static $knownImagesModules = [
  29. 'indicators', 'textures',
  30. // Extra icons
  31. 'icons-accessibility',
  32. 'icons-alerts',
  33. 'icons-content',
  34. 'icons-editing-advanced',
  35. 'icons-editing-citation',
  36. 'icons-editing-core',
  37. 'icons-editing-list',
  38. 'icons-editing-styling',
  39. 'icons-interactions',
  40. 'icons-layout',
  41. 'icons-location',
  42. 'icons-media',
  43. 'icons-moderation',
  44. 'icons-movement',
  45. 'icons-user',
  46. 'icons-wikimedia',
  47. ];
  48. // Note that keys must be lowercase, values TitleCase.
  49. protected static $builtinSkinThemeMap = [
  50. 'default' => 'WikimediaUI',
  51. ];
  52. // Note that keys must be TitleCase.
  53. protected static $builtinThemePaths = [
  54. 'WikimediaUI' => [
  55. 'scripts' => 'resources/lib/ooui/oojs-ui-wikimediaui.js',
  56. 'styles' => 'resources/lib/ooui/oojs-ui-{module}-wikimediaui.css',
  57. 'images' => 'resources/lib/ooui/themes/wikimediaui/{module}.json',
  58. ],
  59. 'Apex' => [
  60. 'scripts' => 'resources/lib/ooui/oojs-ui-apex.js',
  61. 'styles' => 'resources/lib/ooui/oojs-ui-{module}-apex.css',
  62. 'images' => 'resources/lib/ooui/themes/apex/{module}.json',
  63. ],
  64. ];
  65. /**
  66. * Return a map of skin names (in lowercase) to OOUI theme names, defining which theme a given
  67. * skin should use.
  68. *
  69. * @return array
  70. */
  71. public static function getSkinThemeMap() {
  72. $themeMap = self::$builtinSkinThemeMap;
  73. $themeMap += ExtensionRegistry::getInstance()->getAttribute( 'SkinOOUIThemes' );
  74. return $themeMap;
  75. }
  76. /**
  77. * Return a map of theme names to lists of paths from which a given theme should be loaded.
  78. *
  79. * Keys are theme names, values are associative arrays. Keys of the inner array are 'scripts',
  80. * 'styles', or 'images', and values are string paths.
  81. *
  82. * Additionally, the string '{module}' in paths represents the name of the module to load.
  83. *
  84. * @return array
  85. */
  86. protected static function getThemePaths() {
  87. $themePaths = self::$builtinThemePaths;
  88. return $themePaths;
  89. }
  90. /**
  91. * Return a path to load given module of given theme from.
  92. *
  93. * @param string $theme OOUI theme name, for example 'WikimediaUI' or 'Apex'
  94. * @param string $kind Kind of the module: 'scripts', 'styles', or 'images'
  95. * @param string $module Module name, for valid values see $knownScriptsModules,
  96. * $knownStylesModules, $knownImagesModules
  97. * @return string
  98. */
  99. protected function getThemePath( $theme, $kind, $module ) {
  100. $paths = self::getThemePaths();
  101. $path = $paths[ $theme ][ $kind ];
  102. $path = str_replace( '{module}', $module, $path );
  103. return $path;
  104. }
  105. /**
  106. * @param string $theme See getThemePath()
  107. * @param string $module See getThemePath()
  108. * @return string
  109. */
  110. protected function getThemeScriptsPath( $theme, $module ) {
  111. if ( !in_array( $module, self::$knownScriptsModules ) ) {
  112. throw new InvalidArgumentException( "Invalid OOUI scripts module '$module'" );
  113. }
  114. return $this->getThemePath( $theme, 'scripts', $module );
  115. }
  116. /**
  117. * @param string $theme See getThemePath()
  118. * @param string $module See getThemePath()
  119. * @return string
  120. */
  121. protected function getThemeStylesPath( $theme, $module ) {
  122. if ( !in_array( $module, self::$knownStylesModules ) ) {
  123. throw new InvalidArgumentException( "Invalid OOUI styles module '$module'" );
  124. }
  125. return $this->getThemePath( $theme, 'styles', $module );
  126. }
  127. /**
  128. * @param string $theme See getThemePath()
  129. * @param string $module See getThemePath()
  130. * @return string
  131. */
  132. protected function getThemeImagesPath( $theme, $module ) {
  133. if ( !in_array( $module, self::$knownImagesModules ) ) {
  134. throw new InvalidArgumentException( "Invalid OOUI images module '$module'" );
  135. }
  136. return $this->getThemePath( $theme, 'images', $module );
  137. }
  138. }