ResourceLoaderOOUIIconPackModule.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * Allows loading arbitrary sets of OOUI icons.
  22. *
  23. * @ingroup ResourceLoader
  24. * @since 1.34
  25. */
  26. class ResourceLoaderOOUIIconPackModule extends ResourceLoaderOOUIImageModule {
  27. public function __construct( $options = [], $localBasePath = null ) {
  28. parent::__construct( $options, $localBasePath );
  29. if ( !isset( $this->definition['icons'] ) || !$this->definition['icons'] ) {
  30. throw new InvalidArgumentException( "Parameter 'icons' must be given." );
  31. }
  32. // A few things check for the "icons" prefix on this value, so specify it even though
  33. // we don't use it for actually loading the data, like in the other modules.
  34. $this->definition['themeImages'] = 'icons';
  35. }
  36. private function getIcons() {
  37. return $this->definition['icons'];
  38. }
  39. protected function loadOOUIDefinition( $theme, $unused ) {
  40. // This is shared between instances of this class, so we only have to load the JSON files once
  41. static $data = [];
  42. if ( !isset( $data[$theme] ) ) {
  43. $data[$theme] = [];
  44. // Load and merge the JSON data for all "icons-foo" modules
  45. foreach ( self::$knownImagesModules as $module ) {
  46. if ( substr( $module, 0, 5 ) === 'icons' ) {
  47. $moreData = $this->readJSONFile( $this->getThemeImagesPath( $theme, $module ) );
  48. if ( $moreData ) {
  49. $data[$theme] = array_replace_recursive( $data[$theme], $moreData );
  50. }
  51. }
  52. }
  53. }
  54. $definition = $data[$theme];
  55. // Filter out the data for all other icons, leaving only the ones we want for this module
  56. $iconsNames = $this->getIcons();
  57. foreach ( array_keys( $definition['images'] ) as $iconName ) {
  58. if ( !in_array( $iconName, $iconsNames ) ) {
  59. unset( $definition['images'][$iconName] );
  60. }
  61. }
  62. return $definition;
  63. }
  64. public static function extractLocalBasePath( $options, $localBasePath = null ) {
  65. global $IP;
  66. if ( $localBasePath === null ) {
  67. $localBasePath = $IP;
  68. }
  69. // Ignore any 'localBasePath' present in $options, this always refers to files in MediaWiki core
  70. return $localBasePath;
  71. }
  72. }