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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
<?php
/**
* Gadgets extension - lets users select custom javascript gadgets
*
* For more info see http://mediawiki.org/wiki/Extension:Gadgets
*
* @file
* @ingroup Extensions
* @author Daniel Kinzler, brightbyte.de
* @copyright © 2007 Daniel Kinzler
* @license GNU General Public Licence 2.0 or later
*/
/**
* Wrapper for one gadget.
*/
class Gadget {
/**
* Increment this when changing class structure
*/
const GADGET_CLASS_VERSION = 9;
const CACHE_TTL = 86400;
private $scripts = array(),
$styles = array(),
$dependencies = array(),
$messages = array(),
$name,
$definition,
$resourceLoaded = false,
$requiredRights = array(),
$requiredSkins = array(),
$targets = array( 'desktop' ),
$onByDefault = false,
$hidden = false,
$position = 'bottom',
$category;
/** @var array|bool Result of loadStructuredList() */
private static $definitionCache;
public function __construct( array $options ) {
foreach ( $options as $member => $option ) {
switch ( $member ) {
case 'scripts':
case 'styles':
case 'dependencies':
case 'messages':
case 'name':
case 'definition':
case 'resourceLoaded':
case 'requiredRights':
case 'requiredSkins':
case 'targets':
case 'onByDefault':
case 'position':
case 'hidden':
case 'category':
$this->{$member} = $option;
break;
default:
throw new InvalidArgumentException( "Unrecognized '$member' parameter" );
}
}
}
/**
* Whether the provided gadget id is valid
*
* @param string $id
* @return bool
*/
public static function isValidGadgetID( $id ) {
return strlen( $id ) > 0 && ResourceLoader::isValidModuleName( "ext.gadget.$id" );
}
/**
* @return String: Gadget name
*/
public function getName() {
return $this->name;
}
/**
* @return String: Gadget description parsed into HTML
*/
public function getDescription() {
return wfMessage( "gadget-{$this->getName()}" )->parse();
}
/**
* @return String: Wikitext of gadget description
*/
public function getRawDescription() {
return wfMessage( "gadget-{$this->getName()}" )->plain();
}
/**
* @return String: Name of category (aka section) our gadget belongs to. Empty string if none.
*/
public function getCategory() {
return $this->category;
}
/**
* @return String: Name of ResourceLoader module for this gadget
*/
public function getModuleName() {
return "ext.gadget.{$this->name}";
}
/**
* Checks whether this gadget is enabled for given user
*
* @param $user User: user to check against
* @return Boolean
*/
public function isEnabled( $user ) {
return (bool)$user->getOption( "gadget-{$this->name}", $this->onByDefault );
}
/**
* Checks whether given user has permissions to use this gadget
*
* @param $user User: user to check against
* @return Boolean
*/
public function isAllowed( $user ) {
return count( array_intersect( $this->requiredRights, $user->getRights() ) ) == count( $this->requiredRights )
&& ( !count( $this->requiredSkins ) || in_array( $user->getOption( 'skin' ), $this->requiredSkins ) );
}
/**
* @return Boolean: Whether this gadget is on by default for everyone (but can be disabled in preferences)
*/
public function isOnByDefault() {
return $this->onByDefault;
}
/**
* @return bool
*/
public function isHidden() {
return $this->hidden;
}
/**
* @return Boolean: Whether all of this gadget's JS components support ResourceLoader
*/
public function supportsResourceLoader() {
return $this->resourceLoaded;
}
/**
* @return Boolean: Whether this gadget has resources that can be loaded via ResourceLoader
*/
public function hasModule() {
return count( $this->styles )
+ ( $this->supportsResourceLoader() ? count( $this->scripts ) : 0 )
> 0;
}
/**
* @return String: Definition for this gadget from MediaWiki:gadgets-definition
*/
public function getDefinition() {
return $this->definition;
}
/**
* @return Array: Array of pages with JS not prefixed with namespace
*/
public function getScripts() {
return $this->scripts;
}
/**
* @return Array: Array of pages with CSS not prefixed with namespace
*/
public function getStyles() {
return $this->styles;
}
/**
* @return Array: Array of all of this gadget's resources
*/
public function getScriptsAndStyles() {
return array_merge( $this->scripts, $this->styles );
}
/**
* Returns module for ResourceLoader, see getModuleName() for its name.
* If our gadget has no scripts or styles suitable for RL, false will be returned.
* @return Mixed: GadgetResourceLoaderModule or false
*/
public function getModule() {
$pages = array();
foreach ( $this->styles as $style ) {
$pages['MediaWiki:' . $style] = array( 'type' => 'style' );
}
if ( $this->supportsResourceLoader() ) {
foreach ( $this->scripts as $script ) {
$pages['MediaWiki:' . $script] = array( 'type' => 'script' );
}
}
if ( !count( $pages ) ) {
return null;
}
return new GadgetResourceLoaderModule(
$pages,
$this->dependencies,
$this->targets,
$this->position,
$this->messages
);
}
/**
* Returns list of scripts that don't support ResourceLoader
* @return Array
*/
public function getLegacyScripts() {
if ( $this->supportsResourceLoader() ) {
return array();
}
return $this->scripts;
}
/**
* Returns names of resources this gadget depends on
* @return Array
*/
public function getDependencies() {
return $this->dependencies;
}
/**
* @return array
*/
public function getMessages() {
return $this->messages;
}
/**
* Returns array of permissions required by this gadget
* @return Array
*/
public function getRequiredRights() {
return $this->requiredRights;
}
/**
* Returns array of skins where this gadget works
* @return Array
*/
public function getRequiredSkins() {
return $this->requiredSkins;
}
/**
* Returns the position of this Gadget's ResourceLoader module
* @return String: 'bottom' or 'top'
*/
public function getPosition() {
return $this->position;
}
}
|