forked from wikimedia/mediawiki-skins-Metrolook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkinMetrolookHooks.php
226 lines (210 loc) · 6.17 KB
/
SkinMetrolookHooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* Hooks for Metrolook skin
*
* @file
* @ingroup Skins
*/
class MetrolookHooks {
/* Protected Static Members */
protected static $features = array(
'collapsiblenav' => array(
'preferences' => array(
'skinmetrolook-collapsiblenav' => array(
'type' => 'toggle',
'label-message' => 'skinmetrolook-collapsiblenav-preference',
'section' => 'rendering/advancedrendering',
),
),
'requirements' => array(
'skinmetrolook-collapsiblenav' => true,
),
'modules' => array( 'skins.metrolook.collapsibleNav' ),
)
);
protected static $featurescustom = array(
'collapsiblenav-custom' => array(
'preferences' => array(
'skinmetrolook-collapsiblenav-custom' => array(
'type' => 'toggle',
'label-message' => 'skinmetrolook-collapsiblenav-preference',
'section' => 'rendering/advancedrendering',
),
),
'requirements' => array(
'skinmetrolook-collapsiblenav-custom' => true,
),
'modules' => array( 'skins.metrolook.collapsibleNav-custom' ),
)
);
/* Static Methods */
/**
* Checks if a certain option is enabled
*
* This method is public to allow other extensions that use CollapsibleVector to use the
* same configuration as CollapsibleVector itself
*
* @param $name string Name of the feature, should be a key of $features
* @return bool
*/
public static function isEnabled( $name ) {
global $wgMetrolookFeatures, $wgUser, $wgMetrolookSearchBar;
// Features with global set to true are always enabled
if ( !isset( $wgMetrolookFeatures[$name] ) || $wgMetrolookFeatures[$name]['global'] ) {
return true;
}
// Features with user preference control can have any number of preferences
// to be specific values to be enabled
if ( $wgMetrolookSearchBar ) {
if ( $wgMetrolookFeatures[$name]['user'] ) {
if ( isset( self::$features[$name]['requirements'] ) ) {
foreach ( self::$features[$name]['requirements'] as $requirement => $value ) {
// Important! We really do want fuzzy evaluation here
if ( $wgUser->getOption( $requirement ) != $value ) {
return false;
}
}
}
return true;
}
} else {
if ( $wgMetrolookFeatures[$name]['user'] ) {
if ( isset( self::$featurescustom[$name]['requirements'] ) ) {
foreach ( self::$featurescustom[$name]['requirements'] as $requirement => $value ) {
// Important! We really do want fuzzy evaluation here
if ( $wgUser->getOption( $requirement ) != $value ) {
return false;
}
}
}
return true;
}
}
// Features controlled by $wgMetrolookFeatures with both global and user
// set to false are awlways disabled
return false;
}
/* Static Methods */
/**
* BeforePageDisplay hook
*
* Adds the modules to the page
*
* @param $out OutputPage output page
* @param $skin Skin current skin
*/
public static function beforePageDisplay( $out, $skin ) {
global $wgMetrolookSearchBar;
if ( $skin instanceof SkinMetrolook ) {
if ( $wgMetrolookSearchBar ) {
// Add modules for enabled features
foreach ( self::$features as $name => $feature ) {
if ( isset( $feature['modules'] ) && self::isEnabled( $name ) ) {
$out->addModules( $feature['modules'] );
}
}
} else {
// Add modules for enabled features
foreach ( self::$featurescustom as $name => $feature ) {
if ( isset( $feature['modules'] ) && self::isEnabled( $name ) ) {
$out->addModules( $feature['modules'] );
}
}
}
}
return true;
}
/**
* GetPreferences hook
*
* Adds Vector-releated items to the preferences
*
* @param $user User current user
* @param $defaultPreferences array list of default user preference controls
*/
public static function getPreferences( $user, &$defaultPreferences ) {
global $wgMetrolookFeatures, $wgMetrolookSearchBar;
if ( $wgMetrolookSearchBar ) {
foreach ( self::$features as $name => $feature ) {
if (
isset( $feature['preferences'] ) &&
( !isset( $wgMetrolookFeatures[$name] ) || $wgMetrolookFeatures[$name]['user'] )
) {
foreach ( $feature['preferences'] as $key => $options ) {
$defaultPreferences[$key] = $options;
}
}
}
} else {
foreach ( self::$featurescustom as $name => $feature ) {
if (
isset( $feature['preferences'] ) &&
( !isset( $wgMetrolookFeatures[$name] ) || $wgMetrolookFeatures[$name]['user'] )
) {
foreach ( $feature['preferences'] as $key => $options ) {
$defaultPreferences[$key] = $options;
}
}
}
}
return true;
}
/**
* ResourceLoaderGetConfigVars hook
*
* Adds enabled/disabled switches for Vector modules
*/
public static function resourceLoaderGetConfigVars( &$vars ) {
global $wgMetrolookFeatures;
$configurations = array();
if ( $wgMetrolookSearchBar ) {
foreach ( self::$features as $name => $feature ) {
if (
isset( $feature['configurations'] ) &&
( !isset( $wgMetrolookFeatures[$name] ) || self::isEnabled( $name ) )
) {
foreach ( $feature['configurations'] as $configuration ) {
global $$wgConfiguration;
$configurations[$configuration] = $$wgConfiguration;
}
}
}
} else {
foreach ( self::$featurescustom as $name => $feature ) {
if (
isset( $feature['configurations'] ) &&
( !isset( $wgMetrolookFeatures[$name] ) || self::isEnabled( $name ) )
) {
foreach ( $feature['configurations'] as $configuration ) {
global $$wgConfiguration;
$configurations[$configuration] = $$wgConfiguration;
}
}
}
}
if ( count( $configurations ) ) {
$vars = array_merge( $vars, $configurations );
}
return true;
}
/**
* @param $vars array
* @return bool
*/
public static function makeGlobalVariablesScript( &$vars ) {
global $wgMetrolookSearchBar;
// Build and export old-style wgMetrolookEnabledModules object for back compat
$enabledModules = array();
if ( $wgMetrolookSearchBar ) {
foreach ( self::$features as $name => $feature ) {
$enabledModules[$name] = self::isEnabled( $name );
}
} else {
foreach ( self::$featurescustom as $name => $feature ) {
$enabledModules[$name] = self::isEnabled( $name );
}
}
$vars['wgMetrolookEnabledModules'] = $enabledModules;
return true;
}
}