]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/resourceloader/ResourceLoaderOOUIFileModule.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / resourceloader / ResourceLoaderOOUIFileModule.php
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 /**
22  * ResourceLoaderFileModule which magically loads the right skinScripts and skinStyles for every
23  * skin, using the specified OOUI theme for each.
24  *
25  * @since 1.30
26  */
27 class ResourceLoaderOOUIFileModule extends ResourceLoaderFileModule {
28         use ResourceLoaderOOUIModule;
29
30         public function __construct( $options = [] ) {
31                 if ( isset( $options[ 'themeScripts' ] ) ) {
32                         $skinScripts = $this->getSkinSpecific( $options[ 'themeScripts' ], 'scripts' );
33                         if ( !isset( $options['skinScripts'] ) ) {
34                                 $options['skinScripts'] = [];
35                         }
36                         $this->extendSkinSpecific( $options['skinScripts'], $skinScripts );
37                 }
38                 if ( isset( $options[ 'themeStyles' ] ) ) {
39                         $skinStyles = $this->getSkinSpecific( $options[ 'themeStyles' ], 'styles' );
40                         if ( !isset( $options['skinStyles'] ) ) {
41                                 $options['skinStyles'] = [];
42                         }
43                         $this->extendSkinSpecific( $options['skinStyles'], $skinStyles );
44                 }
45
46                 parent::__construct( $options );
47         }
48
49         /**
50          * Helper function to generate values for 'skinStyles' and 'skinScripts'.
51          *
52          * @param string $module Module to generate skinStyles/skinScripts for:
53          *   'core', 'widgets', 'toolbars', 'windows'
54          * @param string $which 'scripts' or 'styles'
55          * @return array
56          */
57         private function getSkinSpecific( $module, $which ) {
58                 $themes = self::getSkinThemeMap();
59
60                 return array_combine(
61                         array_keys( $themes ),
62                         array_map( function ( $theme ) use ( $module, $which ) {
63                                 if ( $which === 'scripts' ) {
64                                         return $this->getThemeScriptsPath( $theme, $module );
65                                 } else {
66                                         return $this->getThemeStylesPath( $theme, $module );
67                                 }
68                         }, array_values( $themes ) )
69                 );
70         }
71
72         /**
73          * Prepend the $extraSkinSpecific assoc. array to the $skinSpecific assoc. array.
74          * Both of them represent a 'skinScripts' or 'skinStyles' definition.
75          *
76          * @param array &$skinSpecific
77          * @param array $extraSkinSpecific
78          */
79         private function extendSkinSpecific( &$skinSpecific, $extraSkinSpecific ) {
80                 // For each skin where skinStyles/skinScripts are defined, add our ones at the beginning
81                 foreach ( $skinSpecific as $skin => $files ) {
82                         if ( !is_array( $files ) ) {
83                                 $files = [ $files ];
84                         }
85                         if ( isset( $extraSkinSpecific[$skin] ) ) {
86                                 $skinSpecific[$skin] = array_merge( [ $extraSkinSpecific[$skin] ], $files );
87                         } elseif ( isset( $extraSkinSpecific['default'] ) ) {
88                                 $skinSpecific[$skin] = array_merge( [ $extraSkinSpecific['default'] ], $files );
89                         }
90                 }
91                 // Add our remaining skinStyles/skinScripts for skins that did not have them defined
92                 foreach ( $extraSkinSpecific as $skin => $file ) {
93                         if ( !isset( $skinSpecific[$skin] ) ) {
94                                 $skinSpecific[$skin] = $file;
95                         }
96                 }
97         }
98 }