ResourceLoaderOOUIImageModule.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * Loads the module definition from JSON files in the format that OOUI uses, converting it to the
  22. * format we use. (Previously known as secret special sauce.)
  23. *
  24. * @since 1.26
  25. */
  26. class ResourceLoaderOOUIImageModule extends ResourceLoaderImageModule {
  27. use ResourceLoaderOOUIModule;
  28. protected function loadFromDefinition() {
  29. if ( $this->definition === null ) {
  30. // Do nothing if definition was already processed
  31. return;
  32. }
  33. $themes = self::getSkinThemeMap();
  34. // For backwards-compatibility, allow missing 'themeImages'
  35. $module = $this->definition['themeImages'] ?? '';
  36. $definition = [];
  37. foreach ( $themes as $skin => $theme ) {
  38. $data = $this->loadOOUIDefinition( $theme, $module );
  39. if ( !$data ) {
  40. // If there's no file for this module of this theme, that's okay, it will just use the defaults
  41. continue;
  42. }
  43. // Convert into a definition compatible with the parent vanilla ResourceLoaderImageModule
  44. foreach ( $data as $key => $value ) {
  45. switch ( $key ) {
  46. // Images and color variants are defined per-theme, here converted to per-skin
  47. case 'images':
  48. case 'variants':
  49. $definition[$key][$skin] = $data[$key];
  50. break;
  51. // Other options must be identical for each theme (or only defined in the default one)
  52. default:
  53. if ( !isset( $definition[$key] ) ) {
  54. $definition[$key] = $data[$key];
  55. } elseif ( $definition[$key] !== $data[$key] ) {
  56. throw new Exception(
  57. "Mismatched OOUI theme images definition: " .
  58. "key '$key' of theme '$theme' for module '$module' " .
  59. "does not match other themes"
  60. );
  61. }
  62. break;
  63. }
  64. }
  65. }
  66. // Extra selectors to allow using the same icons for old-style MediaWiki UI code
  67. if ( substr( $module, 0, 5 ) === 'icons' ) {
  68. $definition['selectorWithoutVariant'] = '.oo-ui-icon-{name}, .mw-ui-icon-{name}:before';
  69. $definition['selectorWithVariant'] = '.oo-ui-image-{variant}.oo-ui-icon-{name}, ' .
  70. '.mw-ui-icon-{name}-{variant}:before';
  71. }
  72. // Fields from module definition silently override keys from JSON files
  73. $this->definition += $definition;
  74. parent::loadFromDefinition();
  75. }
  76. /**
  77. * Load the module definition from the JSON file(s) for the given theme and module.
  78. *
  79. * @since 1.34
  80. * @param string $theme
  81. * @param string $module
  82. * @return array
  83. */
  84. protected function loadOOUIDefinition( $theme, $module ) {
  85. // Find the path to the JSON file which contains the actual image definitions for this theme
  86. if ( $module ) {
  87. $dataPath = $this->getThemeImagesPath( $theme, $module );
  88. if ( !$dataPath ) {
  89. return [];
  90. }
  91. } else {
  92. // Backwards-compatibility for things that probably shouldn't have used this class...
  93. $dataPath =
  94. $this->definition['rootPath'] . '/' .
  95. strtolower( $theme ) . '/' .
  96. $this->definition['name'] . '.json';
  97. }
  98. return $this->readJSONFile( $dataPath );
  99. }
  100. /**
  101. * Read JSON from a file, and transform all paths in it to be relative to the module's base path.
  102. *
  103. * @since 1.34
  104. * @param string $dataPath Path relative to the module's base bath
  105. * @return array|false
  106. */
  107. protected function readJSONFile( $dataPath ) {
  108. $localDataPath = $this->getLocalPath( $dataPath );
  109. if ( !file_exists( $localDataPath ) ) {
  110. return false;
  111. }
  112. $data = json_decode( file_get_contents( $localDataPath ), true );
  113. // Expand the paths to images (since they are relative to the JSON file that defines them, not
  114. // our base directory)
  115. $fixPath = function ( &$path ) use ( $dataPath ) {
  116. if ( $dataPath instanceof ResourceLoaderFilePath ) {
  117. $path = new ResourceLoaderFilePath(
  118. dirname( $dataPath->getPath() ) . '/' . $path,
  119. $dataPath->getLocalBasePath(),
  120. $dataPath->getRemoteBasePath()
  121. );
  122. } else {
  123. $path = dirname( $dataPath ) . '/' . $path;
  124. }
  125. };
  126. array_walk( $data['images'], function ( &$value ) use ( $fixPath ) {
  127. if ( is_string( $value['file'] ) ) {
  128. $fixPath( $value['file'] );
  129. } elseif ( is_array( $value['file'] ) ) {
  130. array_walk_recursive( $value['file'], $fixPath );
  131. }
  132. } );
  133. return $data;
  134. }
  135. }