1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the BeforeInitialize class |
4
|
|
|
* |
5
|
|
|
* This file is part of the MediaWiki skin Chameleon. |
6
|
|
|
* |
7
|
|
|
* @copyright 2013 - 2016, Stephan Gambke, mwjames |
8
|
|
|
* @license GNU General Public License, version 3 (or any later version) |
9
|
|
|
* |
10
|
|
|
* The Chameleon skin is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU General Public License as published by the Free |
12
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
13
|
|
|
* later version. |
14
|
|
|
* |
15
|
|
|
* The Chameleon skin is distributed in the hope that it will be useful, but |
16
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
18
|
|
|
* details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU General Public License along |
21
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
* @file |
24
|
|
|
* @ingroup Skins |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Skins\Chameleon\Hooks; |
28
|
|
|
|
29
|
|
|
use Bootstrap\BootstrapManager; |
30
|
|
|
use RuntimeException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/SetupAfterCache |
34
|
|
|
* |
35
|
|
|
* @since 1.0 |
36
|
|
|
* |
37
|
|
|
* @author mwjames |
38
|
|
|
* @author Stephan Gambke |
39
|
|
|
* @since 1.0 |
40
|
|
|
* @ingroup Skins |
41
|
|
|
*/ |
42
|
|
|
class SetupAfterCache { |
43
|
|
|
|
44
|
|
|
protected $bootstrapManager = null; |
45
|
|
|
protected $configuration = array(); |
46
|
|
|
protected $request; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @since 1.0 |
50
|
|
|
* |
51
|
|
|
* @param BootstrapManager $bootstrapManager |
52
|
|
|
* @param array $configuration |
53
|
|
|
* @param \WebRequest $request |
54
|
|
|
*/ |
55
|
1 |
|
public function __construct( BootstrapManager $bootstrapManager, array &$configuration, \WebRequest $request ) { |
56
|
1 |
|
$this->bootstrapManager = $bootstrapManager; |
57
|
1 |
|
$this->configuration = &$configuration; |
58
|
1 |
|
$this->request = $request; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @since 1.0 |
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
13 |
|
public function process() { |
67
|
|
|
|
68
|
13 |
|
$this->setInstallPaths(); |
69
|
13 |
|
$this->addLateSettings(); |
70
|
13 |
|
$this->registerCommonBootstrapModules(); |
71
|
13 |
|
$this->registerExternalLessModules(); |
72
|
12 |
|
$this->registerExternalLessVariables(); |
73
|
|
|
|
74
|
12 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @since 1.0 |
79
|
|
|
* |
80
|
|
|
* @param array $configuration |
81
|
|
|
*/ |
82
|
1 |
|
public function adjustConfiguration( array &$configuration ) { |
83
|
|
|
|
84
|
1 |
|
foreach ( $this->configuration as $key => $value ) { |
85
|
1 |
|
$configuration[ $key ] = $value; |
86
|
1 |
|
} |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set local and remote base path of the Chameleon skin |
91
|
|
|
*/ |
92
|
13 |
|
protected function setInstallPaths() { |
93
|
|
|
|
94
|
13 |
|
$this->configuration[ 'chameleonLocalPath' ] = $this->configuration['wgStyleDirectory'] . '/chameleon'; |
95
|
13 |
|
$this->configuration[ 'chameleonRemotePath' ] = $this->configuration['wgStylePath'] . '/chameleon'; |
96
|
13 |
|
} |
97
|
|
|
|
98
|
13 |
|
protected function addLateSettings() { |
99
|
|
|
|
100
|
13 |
|
$this->addChameleonToVisualEditorSupportedSkins(); |
101
|
13 |
|
$this->addResourceModules(); |
102
|
13 |
|
$this->setLayoutFile(); |
103
|
13 |
|
} |
104
|
|
|
|
105
|
13 |
|
protected function registerCommonBootstrapModules() { |
106
|
|
|
|
107
|
13 |
|
$this->bootstrapManager->addAllBootstrapModules(); |
108
|
|
|
|
109
|
13 |
|
if ( file_exists( $this->configuration[ 'wgStyleDirectory' ] . '/common/shared.css' ) ) { // MW < 1.24 |
110
|
|
|
$this->bootstrapManager->addExternalModule( |
111
|
|
|
$this->configuration[ 'wgStyleDirectory' ] . '/common/shared.css', |
112
|
|
|
$this->configuration[ 'wgStylePath' ] . '/common/' |
113
|
|
|
); |
114
|
|
|
} else { |
115
|
13 |
|
if ( file_exists( $this->configuration[ 'IP' ] . '/resources/src/mediawiki.legacy/shared.css' ) ) { // MW >= 1.24 |
116
|
6 |
|
$this->bootstrapManager->addExternalModule( |
117
|
6 |
|
$this->configuration[ 'IP' ] . '/resources/src/mediawiki.legacy/shared.css', |
118
|
6 |
|
$this->configuration[ 'wgScriptPath' ] . '/resources/src/mediawiki.legacy/' |
119
|
6 |
|
); |
120
|
6 |
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
13 |
|
$this->bootstrapManager->addExternalModule( |
124
|
13 |
|
$this->configuration[ 'chameleonLocalPath' ] . '/resources/styles/core.less', |
125
|
13 |
|
$this->configuration[ 'chameleonRemotePath' ] . '/resources/styles/' |
126
|
13 |
|
); |
127
|
13 |
|
} |
128
|
|
|
|
129
|
13 |
|
protected function registerExternalLessModules() { |
130
|
|
|
|
131
|
13 |
|
if ( $this->hasConfigurationOfTypeArray( 'egChameleonExternalStyleModules' ) ) { |
132
|
|
|
|
133
|
2 |
|
foreach ( $this->configuration[ 'egChameleonExternalStyleModules' ] as $localFile => $remotePath ) { |
134
|
|
|
|
135
|
2 |
|
list( $localFile, $remotePath ) = $this->matchAssociativeElement( $localFile, $remotePath ); |
136
|
|
|
|
137
|
2 |
|
$this->bootstrapManager->addExternalModule( |
138
|
2 |
|
$this->isReadableFile( $localFile ), |
139
|
|
|
$remotePath |
140
|
1 |
|
); |
141
|
1 |
|
} |
142
|
1 |
|
} |
143
|
12 |
|
} |
144
|
|
|
|
145
|
12 |
|
protected function registerExternalLessVariables() { |
146
|
|
|
|
147
|
12 |
|
if ( $this->hasConfigurationOfTypeArray( 'egChameleonExternalLessVariables' ) ) { |
148
|
|
|
|
149
|
1 |
|
foreach ( $this->configuration[ 'egChameleonExternalLessVariables' ] as $key => $value ) { |
150
|
1 |
|
$this->bootstrapManager->setLessVariable( $key, $value ); |
151
|
1 |
|
} |
152
|
1 |
|
} |
153
|
12 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param $id |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
13 |
|
private function hasConfiguration( $id ) { |
160
|
13 |
|
return isset( $this->configuration[ $id ] ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $id |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
13 |
|
private function hasConfigurationOfTypeArray( $id ) { |
168
|
13 |
|
return $this->hasConfiguration( $id ) && is_array( $this->configuration[ $id ] ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $localFile |
173
|
|
|
* @param $remotePath |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
2 |
|
private function matchAssociativeElement( $localFile, $remotePath ) { |
177
|
|
|
|
178
|
2 |
|
if ( is_integer( $localFile ) ) { |
179
|
2 |
|
return array( $remotePath, '' ); |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
return array( $localFile, $remotePath ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $file |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
2 |
|
private function isReadableFile( $file ) { |
190
|
|
|
|
191
|
2 |
|
if ( is_readable( $file ) ) { |
192
|
1 |
|
return $file; |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
throw new RuntimeException( "Expected an accessible {$file} file" ); |
196
|
|
|
} |
197
|
|
|
|
198
|
13 |
|
protected function addChameleonToVisualEditorSupportedSkins() { |
199
|
|
|
|
200
|
|
|
// if Visual Editor is installed and there is a setting to enable or disable it |
201
|
13 |
|
if ( $this->hasConfiguration( 'wgVisualEditorSupportedSkins' ) && $this->hasConfiguration( 'egChameleonEnableVisualEditor' ) ) { |
202
|
|
|
|
203
|
|
|
// if VE should be enabled |
204
|
3 |
|
if ( $this->configuration[ 'egChameleonEnableVisualEditor' ] === true ) { |
205
|
|
|
|
206
|
|
|
// if Chameleon is not yet in the list of VE-enabled skins |
207
|
2 |
|
if ( !in_array( 'chameleon', $this->configuration[ 'wgVisualEditorSupportedSkins' ] ) ) { |
208
|
1 |
|
$this->configuration[ 'wgVisualEditorSupportedSkins' ][] = 'chameleon'; |
209
|
1 |
|
} |
210
|
|
|
|
211
|
2 |
|
} else { |
212
|
|
|
// remove all entries of Chameleon from the list of VE-enabled skins |
213
|
1 |
|
$this->configuration[ 'wgVisualEditorSupportedSkins' ] = array_diff( |
214
|
1 |
|
$this->configuration[ 'wgVisualEditorSupportedSkins' ], |
215
|
1 |
|
array( 'chameleon' ) |
216
|
1 |
|
); |
217
|
|
|
} |
218
|
3 |
|
} |
219
|
13 |
|
} |
220
|
|
|
|
221
|
13 |
|
protected function addResourceModules() { |
222
|
13 |
|
$this->configuration[ 'wgResourceModules' ][ 'skin.chameleon.jquery-sticky' ] = array( |
223
|
13 |
|
'localBasePath' => $this->configuration[ 'chameleonLocalPath' ] . '/resources/js', |
224
|
13 |
|
'remoteBasePath' => $this->configuration[ 'chameleonRemotePath' ] . '/resources/js', |
225
|
13 |
|
'group' => 'skin.chameleon', |
226
|
13 |
|
'skinScripts' => array( 'chameleon' => array( 'jquery-sticky/jquery.sticky.js', 'Components/Modifications/sticky.js' ) ) |
227
|
13 |
|
); |
228
|
13 |
|
} |
229
|
|
|
|
230
|
13 |
|
protected function setLayoutFile() { |
231
|
|
|
|
232
|
13 |
|
$layout = $this->request->getVal( 'uselayout' ); |
233
|
|
|
|
234
|
13 |
|
if ( $layout !== null && |
235
|
13 |
|
$this->hasConfigurationOfTypeArray( 'egChameleonAvailableLayoutFiles' ) && |
236
|
13 |
|
array_key_exists( $layout, $this->configuration[ 'egChameleonAvailableLayoutFiles' ] ) ) { |
237
|
|
|
|
238
|
1 |
|
$this->configuration[ 'egChameleonLayoutFile' ] = $this->configuration[ 'egChameleonAvailableLayoutFiles' ][ $layout ]; |
239
|
1 |
|
} |
240
|
13 |
|
} |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|