ResourceLoaderOOUIImageModule.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * Secret special sauce.
  22. *
  23. * @since 1.26
  24. */
  25. class ResourceLoaderOOUIImageModule extends ResourceLoaderImageModule {
  26. use ResourceLoaderOOUIModule;
  27. protected function loadFromDefinition() {
  28. if ( $this->definition === null ) {
  29. // Do nothing if definition was already processed
  30. return;
  31. }
  32. $themes = self::getSkinThemeMap();
  33. // For backwards-compatibility, allow missing 'themeImages'
  34. $module = $this->definition['themeImages'] ?? '';
  35. $definition = [];
  36. foreach ( $themes as $skin => $theme ) {
  37. // Find the path to the JSON file which contains the actual image definitions for this theme
  38. if ( $module ) {
  39. $dataPath = $this->getThemeImagesPath( $theme, $module );
  40. } else {
  41. // Backwards-compatibility for things that probably shouldn't have used this class...
  42. $dataPath =
  43. $this->definition['rootPath'] . '/' .
  44. strtolower( $theme ) . '/' .
  45. $this->definition['name'] . '.json';
  46. }
  47. $localDataPath = $this->localBasePath . '/' . $dataPath;
  48. // If there's no file for this module of this theme, that's okay, it will just use the defaults
  49. if ( !file_exists( $localDataPath ) ) {
  50. continue;
  51. }
  52. $data = json_decode( file_get_contents( $localDataPath ), true );
  53. // Expand the paths to images (since they are relative to the JSON file that defines them, not
  54. // our base directory)
  55. $fixPath = function ( &$path ) use ( $dataPath ) {
  56. $path = dirname( $dataPath ) . '/' . $path;
  57. };
  58. array_walk( $data['images'], function ( &$value ) use ( $fixPath ) {
  59. if ( is_string( $value['file'] ) ) {
  60. $fixPath( $value['file'] );
  61. } elseif ( is_array( $value['file'] ) ) {
  62. array_walk_recursive( $value['file'], $fixPath );
  63. }
  64. } );
  65. // Convert into a definition compatible with the parent vanilla ResourceLoaderImageModule
  66. foreach ( $data as $key => $value ) {
  67. switch ( $key ) {
  68. // Images and color variants are defined per-theme, here converted to per-skin
  69. case 'images':
  70. case 'variants':
  71. $definition[$key][$skin] = $data[$key];
  72. break;
  73. // Other options must be identical for each theme (or only defined in the default one)
  74. default:
  75. if ( !isset( $definition[$key] ) ) {
  76. $definition[$key] = $data[$key];
  77. } elseif ( $definition[$key] !== $data[$key] ) {
  78. throw new Exception(
  79. "Mismatched OOUI theme images definition: " .
  80. "key '$key' of theme '$theme' for module '$module' " .
  81. "does not match other themes"
  82. );
  83. }
  84. break;
  85. }
  86. }
  87. }
  88. // Extra selectors to allow using the same icons for old-style MediaWiki UI code
  89. if ( substr( $module, 0, 5 ) === 'icons' ) {
  90. $definition['selectorWithoutVariant'] = '.oo-ui-icon-{name}, .mw-ui-icon-{name}:before';
  91. $definition['selectorWithVariant'] = '.oo-ui-image-{variant}.oo-ui-icon-{name}, ' .
  92. '.mw-ui-icon-{name}-{variant}:before';
  93. }
  94. // Fields from module definition silently override keys from JSON files
  95. $this->definition += $definition;
  96. parent::loadFromDefinition();
  97. }
  98. }