MediaWiki REL1_34
Gadget.php
Go to the documentation of this file.
1<?php
17class Gadget {
22
23 const CACHE_TTL = 86400;
24
25 private $scripts = [],
26 $styles = [],
28 $peers = [],
35 $targets = [ 'desktop' ],
36 $onByDefault = false,
37 $hidden = false,
38 $type = '',
40
41 public function __construct( array $options ) {
42 foreach ( $options as $member => $option ) {
43 switch ( $member ) {
44 case 'scripts':
45 case 'styles':
46 case 'dependencies':
47 case 'peers':
48 case 'messages':
49 case 'name':
50 case 'definition':
51 case 'resourceLoaded':
52 case 'requiredRights':
53 case 'requiredSkins':
54 case 'targets':
55 case 'onByDefault':
56 case 'type':
57 case 'hidden':
58 case 'category':
59 $this->{$member} = $option;
60 break;
61 default:
62 throw new InvalidArgumentException( "Unrecognized '$member' parameter" );
63 }
64 }
65 }
66
75 $data = $content->getAssocArray();
76 $prefixGadgetNs = function ( $page ) {
77 return 'Gadget:' . $page;
78 };
79 $info = [
80 'name' => $id,
81 'resourceLoaded' => true,
82 'requiredRights' => $data['settings']['rights'],
83 'onByDefault' => $data['settings']['default'],
84 'hidden' => $data['settings']['hidden'],
85 'requiredSkins' => $data['settings']['skins'],
86 'category' => $data['settings']['category'],
87 'scripts' => array_map( $prefixGadgetNs, $data['module']['scripts'] ),
88 'styles' => array_map( $prefixGadgetNs, $data['module']['styles'] ),
89 'dependencies' => $data['module']['dependencies'],
90 'peers' => $data['module']['peers'],
91 'messages' => $data['module']['messages'],
92 'type' => $data['module']['type'],
93 ];
94
95 return new self( $info );
96 }
97
104 public static function newEmptyGadget( $id ) {
105 return new self( [ 'name' => $id ] );
106 }
107
114 public static function isValidGadgetID( $id ) {
115 return strlen( $id ) > 0 && ResourceLoader::isValidModuleName( self::getModuleName( $id ) );
116 }
117
121 public function getName() {
122 return $this->name;
123 }
124
128 public function getDescription() {
129 return wfMessage( "gadget-{$this->getName()}" )->parse();
130 }
131
135 public function getRawDescription() {
136 return wfMessage( "gadget-{$this->getName()}" )->plain();
137 }
138
142 public function getCategory() {
143 return $this->category;
144 }
145
150 public static function getModuleName( $id ) {
151 return "ext.gadget.{$id}";
152 }
153
160 public function isEnabled( $user ) {
161 return (bool)$user->getOption( "gadget-{$this->name}", $this->onByDefault );
162 }
163
170 public function isAllowed( User $user ) {
171 return $user->isAllowedAll( ...$this->requiredRights );
172 }
173
178 public function isOnByDefault() {
179 return $this->onByDefault;
180 }
181
185 public function isHidden() {
186 return $this->hidden;
187 }
188
195 public function isSkinSupported( Skin $skin ) {
196 return ( count( $this->requiredSkins ) === 0
197 || in_array( $skin->getSkinName(), $this->requiredSkins )
198 );
199 }
200
204 public function supportsResourceLoader() {
206 }
207
211 public function hasModule() {
212 return count( $this->styles )
213 + ( $this->supportsResourceLoader() ? count( $this->scripts ) : 0 )
214 > 0;
215 }
216
220 public function getDefinition() {
221 return $this->definition;
222 }
223
227 public function getScripts() {
228 return $this->scripts;
229 }
230
234 public function getStyles() {
235 return $this->styles;
236 }
237
241 public function getScriptsAndStyles() {
242 return array_merge( $this->scripts, $this->styles );
243 }
244
248 public function getTargets() {
249 return $this->targets;
250 }
251
256 public function getLegacyScripts() {
257 if ( $this->supportsResourceLoader() ) {
258 return [];
259 }
260 return $this->scripts;
261 }
262
267 public function getDependencies() {
268 return $this->dependencies;
269 }
270
280 public function getPeers() {
281 return $this->peers;
282 }
283
287 public function getMessages() {
288 return $this->messages;
289 }
290
295 public function getRequiredRights() {
297 }
298
303 public function getRequiredSkins() {
305 }
306
311 public function getType() {
312 if ( $this->type === 'styles' || $this->type === 'general' ) {
313 return $this->type;
314 }
315 // Similar to ResourceLoaderWikiModule default
316 if ( $this->styles && !$this->scripts && !$this->dependencies ) {
317 return 'styles';
318 } else {
319 return 'general';
320 }
321 }
322}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Wrapper for one gadget.
Definition Gadget.php:17
const GADGET_CLASS_VERSION
Increment this when changing class structure.
Definition Gadget.php:21
getType()
Returns the load type of this Gadget's ResourceLoader module.
Definition Gadget.php:311
$category
Definition Gadget.php:39
supportsResourceLoader()
Definition Gadget.php:204
$hidden
Definition Gadget.php:37
$messages
Definition Gadget.php:29
getName()
Definition Gadget.php:121
$onByDefault
Definition Gadget.php:36
__construct(array $options)
Definition Gadget.php:41
const CACHE_TTL
Definition Gadget.php:23
getStyles()
Definition Gadget.php:234
$styles
Definition Gadget.php:26
static isValidGadgetID( $id)
Whether the provided gadget id is valid.
Definition Gadget.php:114
$targets
Definition Gadget.php:35
$scripts
Definition Gadget.php:25
$resourceLoaded
Definition Gadget.php:32
getDefinition()
Definition Gadget.php:220
getDescription()
Definition Gadget.php:128
isOnByDefault()
Definition Gadget.php:178
getRawDescription()
Definition Gadget.php:135
getCategory()
Definition Gadget.php:142
static getModuleName( $id)
Definition Gadget.php:150
static newFromDefinitionContent( $id, GadgetDefinitionContent $content)
Create a object based on the metadata in a GadgetDefinitionContent object.
Definition Gadget.php:74
static newEmptyGadget( $id)
Get a placeholder object to use if a gadget doesn't exist.
Definition Gadget.php:104
getScripts()
Definition Gadget.php:227
getPeers()
Get list of extra modules that should be loaded when this gadget is enabled.
Definition Gadget.php:280
getLegacyScripts()
Returns list of scripts that don't support ResourceLoader.
Definition Gadget.php:256
getRequiredSkins()
Returns array of skins where this gadget works.
Definition Gadget.php:303
isAllowed(User $user)
Checks whether given user has permissions to use this gadget.
Definition Gadget.php:170
isSkinSupported(Skin $skin)
Check if this gadget is compatible with a skin.
Definition Gadget.php:195
$requiredRights
Definition Gadget.php:33
getDependencies()
Returns names of resources this gadget depends on.
Definition Gadget.php:267
isHidden()
Definition Gadget.php:185
$definition
Definition Gadget.php:31
getTargets()
Definition Gadget.php:248
isEnabled( $user)
Checks whether this gadget is enabled for given user.
Definition Gadget.php:160
hasModule()
Definition Gadget.php:211
$requiredSkins
Definition Gadget.php:34
getScriptsAndStyles()
Definition Gadget.php:241
getRequiredRights()
Returns array of permissions required by this gadget.
Definition Gadget.php:295
$dependencies
Definition Gadget.php:27
getMessages()
Definition Gadget.php:287
The main skin class which provides methods and properties for all other skins.
Definition Skin.php:38
getSkinName()
Definition Skin.php:158
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
isAllowedAll(... $permissions)
Definition User.php:3678
$content
Definition router.php:78