MediaWiki  1.34.0
Gadget.php
Go to the documentation of this file.
1 <?php
17 class Gadget {
22 
23  const CACHE_TTL = 86400;
24 
25  private $scripts = [],
26  $styles = [],
28  $peers = [],
29  $messages = [],
30  $name,
32  $resourceLoaded = false,
35  $targets = [ 'desktop' ],
36  $onByDefault = false,
37  $hidden = false,
38  $type = '',
39  $category;
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() {
205  return $this->resourceLoaded;
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() {
296  return $this->requiredRights;
297  }
298 
303  public function getRequiredSkins() {
304  return $this->requiredSkins;
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 }
Gadget\$peers
$peers
Definition: Gadget.php:28
Gadget\isAllowed
isAllowed(User $user)
Checks whether given user has permissions to use this gadget.
Definition: Gadget.php:170
Gadget\getDescription
getDescription()
Definition: Gadget.php:128
Gadget\$hidden
$hidden
Definition: Gadget.php:37
Gadget\getModuleName
static getModuleName( $id)
Definition: Gadget.php:150
Gadget\isSkinSupported
isSkinSupported(Skin $skin)
Check if this gadget is compatible with a skin.
Definition: Gadget.php:195
Gadget\getTargets
getTargets()
Definition: Gadget.php:248
Gadget\supportsResourceLoader
supportsResourceLoader()
Definition: Gadget.php:204
Gadget\CACHE_TTL
const CACHE_TTL
Definition: Gadget.php:23
Gadget\$dependencies
$dependencies
Definition: Gadget.php:27
Gadget\$requiredRights
$requiredRights
Definition: Gadget.php:33
Gadget\isOnByDefault
isOnByDefault()
Definition: Gadget.php:178
Gadget\getStyles
getStyles()
Definition: Gadget.php:234
Gadget\$resourceLoaded
$resourceLoaded
Definition: Gadget.php:32
Gadget\getRequiredRights
getRequiredRights()
Returns array of permissions required by this gadget.
Definition: Gadget.php:295
Gadget\getMessages
getMessages()
Definition: Gadget.php:287
Gadget\$styles
$styles
Definition: Gadget.php:26
Gadget\hasModule
hasModule()
Definition: Gadget.php:211
Gadget\$messages
$messages
Definition: Gadget.php:29
Gadget\getScripts
getScripts()
Definition: Gadget.php:227
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
Gadget\newFromDefinitionContent
static newFromDefinitionContent( $id, GadgetDefinitionContent $content)
Create a object based on the metadata in a GadgetDefinitionContent object.
Definition: Gadget.php:74
Gadget\$scripts
$scripts
Definition: Gadget.php:25
Gadget\getScriptsAndStyles
getScriptsAndStyles()
Definition: Gadget.php:241
Gadget\getPeers
getPeers()
Get list of extra modules that should be loaded when this gadget is enabled.
Definition: Gadget.php:280
Gadget\$name
$name
Definition: Gadget.php:29
Gadget\$definition
$definition
Definition: Gadget.php:29
Gadget\getRawDescription
getRawDescription()
Definition: Gadget.php:135
Gadget\__construct
__construct(array $options)
Definition: Gadget.php:41
Gadget\isHidden
isHidden()
Definition: Gadget.php:185
Gadget\$targets
$targets
Definition: Gadget.php:35
Gadget\$type
$type
Definition: Gadget.php:38
Gadget
Wrapper for one gadget.
Definition: Gadget.php:17
Gadget\$requiredSkins
$requiredSkins
Definition: Gadget.php:34
User\isAllowedAll
isAllowedAll(... $permissions)
Definition: User.php:3572
Gadget\getName
getName()
Definition: Gadget.php:121
Gadget\getRequiredSkins
getRequiredSkins()
Returns array of skins where this gadget works.
Definition: Gadget.php:303
Gadget\newEmptyGadget
static newEmptyGadget( $id)
Get a placeholder object to use if a gadget doesn't exist.
Definition: Gadget.php:104
Gadget\getType
getType()
Returns the load type of this Gadget's ResourceLoader module.
Definition: Gadget.php:311
$content
$content
Definition: router.php:78
GadgetDefinitionContent
Definition: GadgetDefinitionContent.php:23
Gadget\getLegacyScripts
getLegacyScripts()
Returns list of scripts that don't support ResourceLoader.
Definition: Gadget.php:256
Gadget\isValidGadgetID
static isValidGadgetID( $id)
Whether the provided gadget id is valid.
Definition: Gadget.php:114
Gadget\$category
$category
Definition: Gadget.php:38
Gadget\isEnabled
isEnabled( $user)
Checks whether this gadget is enabled for given user.
Definition: Gadget.php:160
Gadget\getDefinition
getDefinition()
Definition: Gadget.php:220
Gadget\getCategory
getCategory()
Definition: Gadget.php:142
Skin\getSkinName
getSkinName()
Definition: Skin.php:158
Gadget\$onByDefault
$onByDefault
Definition: Gadget.php:36
Skin
The main skin class which provides methods and properties for all other skins.
Definition: Skin.php:38
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
Gadget\GADGET_CLASS_VERSION
const GADGET_CLASS_VERSION
Increment this when changing class structure.
Definition: Gadget.php:21
Gadget\getDependencies
getDependencies()
Returns names of resources this gadget depends on.
Definition: Gadget.php:267