MediaWiki  1.30.0
Gadgets_body.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 
74  public static function newFromDefinitionContent( $id, GadgetDefinitionContent $content ) {
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 ) {
171  return count( array_intersect( $this->requiredRights, $user->getRights() ) ) ==
172  count( $this->requiredRights )
173  && ( $this->requiredSkins === true
174  || !count( $this->requiredSkins )
175  || in_array( $user->getOption( 'skin' ), $this->requiredSkins )
176  );
177  }
178 
183  public function isOnByDefault() {
184  return $this->onByDefault;
185  }
186 
190  public function isHidden() {
191  return $this->hidden;
192  }
193 
197  public function supportsResourceLoader() {
198  return $this->resourceLoaded;
199  }
200 
204  public function hasModule() {
205  return count( $this->styles )
206  + ( $this->supportsResourceLoader() ? count( $this->scripts ) : 0 )
207  > 0;
208  }
209 
213  public function getDefinition() {
214  return $this->definition;
215  }
216 
220  public function getScripts() {
221  return $this->scripts;
222  }
223 
227  public function getStyles() {
228  return $this->styles;
229  }
230 
234  public function getScriptsAndStyles() {
235  return array_merge( $this->scripts, $this->styles );
236  }
237 
241  public function getTargets() {
242  return $this->targets;
243  }
244 
249  public function getLegacyScripts() {
250  if ( $this->supportsResourceLoader() ) {
251  return [];
252  }
253  return $this->scripts;
254  }
255 
260  public function getDependencies() {
261  return $this->dependencies;
262  }
263 
273  public function getPeers() {
274  return $this->peers;
275  }
276 
280  public function getMessages() {
281  return $this->messages;
282  }
283 
288  public function getRequiredRights() {
289  return $this->requiredRights;
290  }
291 
296  public function getRequiredSkins() {
297  return $this->requiredSkins;
298  }
299 
304  public function getType() {
305  if ( $this->type === 'styles' || $this->type === 'general' ) {
306  return $this->type;
307  }
308  // Similar to ResourceLoaderWikiModule default
309  if ( $this->styles && !$this->scripts && !$this->dependencies ) {
310  return 'styles';
311  } else {
312  return 'general';
313  }
314  }
315 }
Gadget\$peers
$peers
Definition: Gadgets_body.php:28
Gadget\getDescription
getDescription()
Definition: Gadgets_body.php:128
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
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\$hidden
$hidden
Definition: Gadgets_body.php:37
Gadget\getModuleName
static getModuleName( $id)
Definition: Gadgets_body.php:150
Gadget\getTargets
getTargets()
Definition: Gadgets_body.php:241
Gadget\supportsResourceLoader
supportsResourceLoader()
Definition: Gadgets_body.php:197
Gadget\CACHE_TTL
const CACHE_TTL
Definition: Gadgets_body.php:23
Gadget\$dependencies
$dependencies
Definition: Gadgets_body.php:27
captcha-old.count
count
Definition: captcha-old.py:249
Gadget\$requiredRights
$requiredRights
Definition: Gadgets_body.php:33
Gadget\isOnByDefault
isOnByDefault()
Definition: Gadgets_body.php:183
Gadget\getStyles
getStyles()
Definition: Gadgets_body.php:227
Gadget\$resourceLoaded
$resourceLoaded
Definition: Gadgets_body.php:32
Gadget\getRequiredRights
getRequiredRights()
Returns array of permissions required by this gadget.
Definition: Gadgets_body.php:288
Gadget\getMessages
getMessages()
Definition: Gadgets_body.php:280
Gadget\$styles
$styles
Definition: Gadgets_body.php:26
Gadget\hasModule
hasModule()
Definition: Gadgets_body.php:204
Gadget\$messages
$messages
Definition: Gadgets_body.php:29
Gadget\getScripts
getScripts()
Definition: Gadgets_body.php:220
Gadget\newFromDefinitionContent
static newFromDefinitionContent( $id, GadgetDefinitionContent $content)
Create a object based on the metadata in a GadgetDefinitionContent object.
Definition: Gadgets_body.php:74
Gadget\$scripts
$scripts
Definition: Gadgets_body.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: Gadgets_body.php:234
Gadget\getPeers
getPeers()
Get list of extra modules that should be loaded when this gadget is enabled.
Definition: Gadgets_body.php:273
Gadget\$name
$name
Definition: Gadgets_body.php:29
Gadget\$definition
$definition
Definition: Gadgets_body.php:29
GadgetDefinitionContent\getAssocArray
getAssocArray()
Get the JSON content as an associative array with all fields filled out, populating defaults as neces...
Definition: GadgetDefinitionContent.php:88
Gadget\getRawDescription
getRawDescription()
Definition: Gadgets_body.php:135
Gadget\__construct
__construct(array $options)
Definition: Gadgets_body.php:41
Gadget\isHidden
isHidden()
Definition: Gadgets_body.php:190
Gadget\$targets
$targets
Definition: Gadgets_body.php:35
Gadget\$type
$type
Definition: Gadgets_body.php:38
Gadget
Wrapper for one gadget.
Definition: Gadgets_body.php:17
Gadget\$requiredSkins
$requiredSkins
Definition: Gadgets_body.php:34
Gadget\getName
getName()
Definition: Gadgets_body.php:121
Gadget\getRequiredSkins
getRequiredSkins()
Returns array of skins where this gadget works.
Definition: Gadgets_body.php:296
Gadget\newEmptyGadget
static newEmptyGadget( $id)
Get a placeholder object to use if a gadget doesn't exist.
Definition: Gadgets_body.php:104
Gadget\getType
getType()
Returns the load type of this Gadget's ResourceLoader module.
Definition: Gadgets_body.php:304
Gadget\isAllowed
isAllowed( $user)
Checks whether given user has permissions to use this gadget.
Definition: Gadgets_body.php:170
GadgetDefinitionContent
Definition: GadgetDefinitionContent.php:23
Gadget\getLegacyScripts
getLegacyScripts()
Returns list of scripts that don't support ResourceLoader.
Definition: Gadgets_body.php:249
Gadget\isValidGadgetID
static isValidGadgetID( $id)
Whether the provided gadget id is valid.
Definition: Gadgets_body.php:114
Gadget\$category
$category
Definition: Gadgets_body.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:1965
Gadget\isEnabled
isEnabled( $user)
Checks whether this gadget is enabled for given user.
Definition: Gadgets_body.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
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 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
Gadget\getDefinition
getDefinition()
Definition: Gadgets_body.php:213
Gadget\getCategory
getCategory()
Definition: Gadgets_body.php:142
Gadget\$onByDefault
$onByDefault
Definition: Gadgets_body.php:36
Gadget\GADGET_CLASS_VERSION
const GADGET_CLASS_VERSION
Increment this when changing class structure.
Definition: Gadgets_body.php:21
array
the array() calling protocol came about after MediaWiki 1.4rc1.
Gadget\getDependencies
getDependencies()
Returns names of resources this gadget depends on.
Definition: Gadgets_body.php:260