MediaWiki  1.33.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
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
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
captcha-old.count
count
Definition: captcha-old.py:249
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
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
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
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
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
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
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
Gadget\$requiredSkins
$requiredSkins
Definition: Gadget.php:34
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
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
scripts
The package scripts
Definition: README.txt:1
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1985
Gadget\isEnabled
isEnabled( $user)
Checks whether this gadget is enabled for given user.
Definition: Gadget.php:160
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$skin
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned $skin
Definition: hooks.txt:1985
$content
$content
Definition: pageupdater.txt:72
Gadget\getDefinition
getDefinition()
Definition: Gadget.php:220
Gadget\getCategory
getCategory()
Definition: Gadget.php:142
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
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:22
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