ResourceLoaderOOUIModule.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * @ingroup ResourceLoader
  24. * @internal
  25. */
  26. trait ResourceLoaderOOUIModule {
  27. protected static $knownScriptsModules = [ 'core' ];
  28. protected static $knownStylesModules = [ 'core', 'widgets', 'toolbars', 'windows' ];
  29. protected static $knownImagesModules = [
  30. 'indicators',
  31. // Extra icons
  32. 'icons-accessibility',
  33. 'icons-alerts',
  34. 'icons-content',
  35. 'icons-editing-advanced',
  36. 'icons-editing-citation',
  37. 'icons-editing-core',
  38. 'icons-editing-list',
  39. 'icons-editing-styling',
  40. 'icons-interactions',
  41. 'icons-layout',
  42. 'icons-location',
  43. 'icons-media',
  44. 'icons-moderation',
  45. 'icons-movement',
  46. 'icons-user',
  47. 'icons-wikimedia',
  48. ];
  49. // Note that keys must be lowercase, values TitleCase.
  50. protected static $builtinSkinThemeMap = [
  51. 'default' => 'WikimediaUI',
  52. ];
  53. // Note that keys must be TitleCase.
  54. protected static $builtinThemePaths = [
  55. 'WikimediaUI' => [
  56. 'scripts' => 'resources/lib/ooui/oojs-ui-wikimediaui.js',
  57. 'styles' => 'resources/lib/ooui/oojs-ui-{module}-wikimediaui.css',
  58. 'images' => 'resources/lib/ooui/themes/wikimediaui/{module}.json',
  59. ],
  60. 'Apex' => [
  61. 'scripts' => 'resources/lib/ooui/oojs-ui-apex.js',
  62. 'styles' => 'resources/lib/ooui/oojs-ui-{module}-apex.css',
  63. 'images' => 'resources/lib/ooui/themes/apex/{module}.json',
  64. ],
  65. ];
  66. /**
  67. * Return a map of skin names (in lowercase) to OOUI theme names, defining which theme a given
  68. * skin should use.
  69. *
  70. * @return array
  71. */
  72. public static function getSkinThemeMap() {
  73. $themeMap = self::$builtinSkinThemeMap;
  74. $themeMap += ExtensionRegistry::getInstance()->getAttribute( 'SkinOOUIThemes' );
  75. return $themeMap;
  76. }
  77. /**
  78. * Return a map of theme names to lists of paths from which a given theme should be loaded.
  79. *
  80. * Keys are theme names, values are associative arrays. Keys of the inner array are 'scripts',
  81. * 'styles', or 'images', and values are paths. Paths may be strings or ResourceLoaderFilePaths.
  82. *
  83. * Additionally, the string '{module}' in paths represents the name of the module to load.
  84. *
  85. * @return array
  86. */
  87. protected static function getThemePaths() {
  88. $themePaths = self::$builtinThemePaths;
  89. $themePaths += ExtensionRegistry::getInstance()->getAttribute( 'OOUIThemePaths' );
  90. list( $defaultLocalBasePath, $defaultRemoteBasePath ) =
  91. ResourceLoaderFileModule::extractBasePaths();
  92. // Allow custom themes' paths to be relative to the skin/extension that defines them,
  93. // like with ResourceModuleSkinStyles
  94. foreach ( $themePaths as $theme => &$paths ) {
  95. list( $localBasePath, $remoteBasePath ) =
  96. ResourceLoaderFileModule::extractBasePaths( $paths );
  97. if ( $localBasePath !== $defaultLocalBasePath || $remoteBasePath !== $defaultRemoteBasePath ) {
  98. foreach ( $paths as &$path ) {
  99. $path = new ResourceLoaderFilePath( $path, $localBasePath, $remoteBasePath );
  100. }
  101. }
  102. }
  103. return $themePaths;
  104. }
  105. /**
  106. * Return a path to load given module of given theme from.
  107. *
  108. * The file at this path may not exist. This should be handled by the caller (throwing an error or
  109. * falling back to default theme).
  110. *
  111. * @param string $theme OOUI theme name, for example 'WikimediaUI' or 'Apex'
  112. * @param string $kind Kind of the module: 'scripts', 'styles', or 'images'
  113. * @param string $module Module name, for valid values see $knownScriptsModules,
  114. * $knownStylesModules, $knownImagesModules
  115. * @return string|ResourceLoaderFilePath
  116. */
  117. protected function getThemePath( $theme, $kind, $module ) {
  118. $paths = self::getThemePaths();
  119. $path = $paths[$theme][$kind];
  120. if ( $path instanceof ResourceLoaderFilePath ) {
  121. $path = new ResourceLoaderFilePath(
  122. str_replace( '{module}', $module, $path->getPath() ),
  123. $path->getLocalBasePath(),
  124. $path->getRemoteBasePath()
  125. );
  126. } else {
  127. $path = str_replace( '{module}', $module, $path );
  128. }
  129. return $path;
  130. }
  131. /**
  132. * @param string $theme See getThemePath()
  133. * @param string $module See getThemePath()
  134. * @return string|ResourceLoaderFilePath
  135. */
  136. protected function getThemeScriptsPath( $theme, $module ) {
  137. if ( !in_array( $module, self::$knownScriptsModules ) ) {
  138. throw new InvalidArgumentException( "Invalid OOUI scripts module '$module'" );
  139. }
  140. return $this->getThemePath( $theme, 'scripts', $module );
  141. }
  142. /**
  143. * @param string $theme See getThemePath()
  144. * @param string $module See getThemePath()
  145. * @return string|ResourceLoaderFilePath
  146. */
  147. protected function getThemeStylesPath( $theme, $module ) {
  148. if ( !in_array( $module, self::$knownStylesModules ) ) {
  149. throw new InvalidArgumentException( "Invalid OOUI styles module '$module'" );
  150. }
  151. return $this->getThemePath( $theme, 'styles', $module );
  152. }
  153. /**
  154. * @param string $theme See getThemePath()
  155. * @param string $module See getThemePath()
  156. * @return string|ResourceLoaderFilePath
  157. */
  158. protected function getThemeImagesPath( $theme, $module ) {
  159. if ( !in_array( $module, self::$knownImagesModules ) ) {
  160. throw new InvalidArgumentException( "Invalid OOUI images module '$module'" );
  161. }
  162. return $this->getThemePath( $theme, 'images', $module );
  163. }
  164. }