MediaWiki  1.29.2
Gadgets_body.php
Go to the documentation of this file.
1 <?php
18 class Gadget {
23 
24  const CACHE_TTL = 86400;
25 
26  private $scripts = [],
27  $styles = [],
29  $peers = [],
30  $messages = [],
31  $name,
33  $resourceLoaded = false,
36  $targets = [ 'desktop' ],
37  $onByDefault = false,
38  $hidden = false,
39  $type = '',
40  $category;
41 
42  public function __construct( array $options ) {
43  foreach ( $options as $member => $option ) {
44  switch ( $member ) {
45  case 'scripts':
46  case 'styles':
47  case 'dependencies':
48  case 'peers':
49  case 'messages':
50  case 'name':
51  case 'definition':
52  case 'resourceLoaded':
53  case 'requiredRights':
54  case 'requiredSkins':
55  case 'targets':
56  case 'onByDefault':
57  case 'type':
58  case 'hidden':
59  case 'category':
60  $this->{$member} = $option;
61  break;
62  default:
63  throw new InvalidArgumentException( "Unrecognized '$member' parameter" );
64  }
65  }
66  }
67 
76  $data = $content->getAssocArray();
77  $prefixGadgetNs = function ( $page ) {
78  return 'Gadget:' . $page;
79  };
80  $info = [
81  'name' => $id,
82  'resourceLoaded' => true,
83  'requiredRights' => $data['settings']['rights'],
84  'onByDefault' => $data['settings']['default'],
85  'hidden' => $data['settings']['hidden'],
86  'requiredSkins' => $data['settings']['skins'],
87  'category' => $data['settings']['category'],
88  'scripts' => array_map( $prefixGadgetNs, $data['module']['scripts'] ),
89  'styles' => array_map( $prefixGadgetNs, $data['module']['styles'] ),
90  'dependencies' => $data['module']['dependencies'],
91  'peers' => $data['module']['peers'],
92  'messages' => $data['module']['messages'],
93  'type' => $data['module']['type'],
94  ];
95 
96  return new self( $info );
97 
98  }
99 
106  public static function newEmptyGadget( $id ) {
107  return new self( [ 'name' => $id ] );
108  }
109 
116  public static function isValidGadgetID( $id ) {
117  return strlen( $id ) > 0 && ResourceLoader::isValidModuleName( Gadget::getModuleName( $id ) );
118  }
119 
120 
124  public function getName() {
125  return $this->name;
126  }
127 
131  public function getDescription() {
132  return wfMessage( "gadget-{$this->getName()}" )->parse();
133  }
134 
138  public function getRawDescription() {
139  return wfMessage( "gadget-{$this->getName()}" )->plain();
140  }
141 
145  public function getCategory() {
146  return $this->category;
147  }
148 
153  public static function getModuleName( $id ) {
154  return "ext.gadget.{$id}";
155  }
156 
163  public function isEnabled( $user ) {
164  return (bool)$user->getOption( "gadget-{$this->name}", $this->onByDefault );
165  }
166 
173  public function isAllowed( $user ) {
174  return count( array_intersect( $this->requiredRights, $user->getRights() ) ) == count( $this->requiredRights )
175  && ( $this->requiredSkins === true || !count( $this->requiredSkins ) || in_array( $user->getOption( 'skin' ), $this->requiredSkins ) );
176  }
177 
181  public function isOnByDefault() {
182  return $this->onByDefault;
183  }
184 
188  public function isHidden() {
189  return $this->hidden;
190  }
191 
195  public function supportsResourceLoader() {
196  return $this->resourceLoaded;
197  }
198 
202  public function hasModule() {
203  return count( $this->styles )
204  + ( $this->supportsResourceLoader() ? count( $this->scripts ) : 0 )
205  > 0;
206  }
207 
211  public function getDefinition() {
212  return $this->definition;
213  }
214 
218  public function getScripts() {
219  return $this->scripts;
220  }
221 
225  public function getStyles() {
226  return $this->styles;
227  }
228 
232  public function getScriptsAndStyles() {
233  return array_merge( $this->scripts, $this->styles );
234  }
235 
239  public function getTargets() {
240  return $this->targets;
241  }
242 
247  public function getLegacyScripts() {
248  if ( $this->supportsResourceLoader() ) {
249  return [];
250  }
251  return $this->scripts;
252  }
253 
258  public function getDependencies() {
259  return $this->dependencies;
260  }
261 
271  public function getPeers() {
272  return $this->peers;
273  }
274 
278  public function getMessages() {
279  return $this->messages;
280  }
281 
286  public function getRequiredRights() {
287  return $this->requiredRights;
288  }
289 
294  public function getRequiredSkins() {
295  return $this->requiredSkins;
296  }
297 
302  public function getType() {
303  if ( $this->type === 'styles' || $this->type === 'general' ) {
304  return $this->type;
305  }
306  if ( $this->styles && !$this->scripts && !$this->dependencies ) {
307  // Similar to ResourceLoaderWikiModule default
308  return 'styles';
309  }
310  if ( !$this->styles && $this->supportsResourceLoader() && $this->scripts ) {
311  return 'general';
312  }
313  // Real default is in GadgetResourceLoaderModule so that beforePageDisplay
314  // can distinguish between explicit and fallback.
315  return '';
316  }
317 }
Gadget\$peers
$peers
Definition: Gadgets_body.php:29
Gadget\getDescription
getDescription()
Definition: Gadgets_body.php:131
Gadget\$hidden
$hidden
Definition: Gadgets_body.php:38
Gadget\getModuleName
static getModuleName( $id)
Definition: Gadgets_body.php:153
Gadget\getTargets
getTargets()
Definition: Gadgets_body.php:239
Gadget\supportsResourceLoader
supportsResourceLoader()
Definition: Gadgets_body.php:195
Gadget\CACHE_TTL
const CACHE_TTL
Definition: Gadgets_body.php:24
Gadget\$dependencies
$dependencies
Definition: Gadgets_body.php:28
captcha-old.count
count
Definition: captcha-old.py:225
Gadget\$requiredRights
$requiredRights
Definition: Gadgets_body.php:34
Gadget\isOnByDefault
isOnByDefault()
Definition: Gadgets_body.php:181
Gadget\getStyles
getStyles()
Definition: Gadgets_body.php:225
Gadget\$resourceLoaded
$resourceLoaded
Definition: Gadgets_body.php:33
Gadget\getRequiredRights
getRequiredRights()
Returns array of permissions required by this gadget.
Definition: Gadgets_body.php:286
Gadget\getMessages
getMessages()
Definition: Gadgets_body.php:278
Gadget\$styles
$styles
Definition: Gadgets_body.php:27
$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:246
Gadget\hasModule
hasModule()
Definition: Gadgets_body.php:202
Gadget\$messages
$messages
Definition: Gadgets_body.php:30
Gadget\getScripts
getScripts()
Definition: Gadgets_body.php:218
Gadget\newFromDefinitionContent
static newFromDefinitionContent( $id, GadgetDefinitionContent $content)
Create a object based on the metadata in a GadgetDefinitionContent object.
Definition: Gadgets_body.php:75
Gadget\$scripts
$scripts
Definition: Gadgets_body.php:26
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:232
Gadget\getPeers
getPeers()
Get list of extra modules that should be loaded when this gadget is enabled.
Definition: Gadgets_body.php:271
Gadget\$name
$name
Definition: Gadgets_body.php:30
Gadget\$definition
$definition
Definition: Gadgets_body.php:30
Gadget\getRawDescription
getRawDescription()
Definition: Gadgets_body.php:138
Gadget\__construct
__construct(array $options)
Definition: Gadgets_body.php:42
Gadget\isHidden
isHidden()
Definition: Gadgets_body.php:188
$content
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1049
$page
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2536
Gadget\$targets
$targets
Definition: Gadgets_body.php:36
Gadget\$type
$type
Definition: Gadgets_body.php:39
Gadget
Wrapper for one gadget.
Definition: Gadgets_body.php:18
Gadget\$requiredSkins
$requiredSkins
Definition: Gadgets_body.php:35
Gadget\getName
getName()
Definition: Gadgets_body.php:124
Gadget\getRequiredSkins
getRequiredSkins()
Returns array of skins where this gadget works.
Definition: Gadgets_body.php:294
Gadget\newEmptyGadget
static newEmptyGadget( $id)
Get a placeholder object to use if a gadget doesn't exist.
Definition: Gadgets_body.php:106
Gadget\getType
getType()
Returns the load type of this Gadget's ResourceLoader module.
Definition: Gadgets_body.php:302
Gadget\isAllowed
isAllowed( $user)
Checks whether given user has permissions to use this gadget.
Definition: Gadgets_body.php:173
GadgetDefinitionContent
Definition: GadgetDefinitionContent.php:23
Gadget\getLegacyScripts
getLegacyScripts()
Returns list of scripts that don't support ResourceLoader.
Definition: Gadgets_body.php:247
Gadget\isValidGadgetID
static isValidGadgetID( $id)
Whether the provided gadget id is valid.
Definition: Gadgets_body.php:116
Gadget\$category
$category
Definition: Gadgets_body.php:39
scripts
The package scripts
Definition: README.txt:1
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\isEnabled
isEnabled( $user)
Checks whether this gadget is enabled for given user.
Definition: Gadgets_body.php:163
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:211
Gadget\getCategory
getCategory()
Definition: Gadgets_body.php:145
Gadget\$onByDefault
$onByDefault
Definition: Gadgets_body.php:37
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1049
Gadget\GADGET_CLASS_VERSION
const GADGET_CLASS_VERSION
Increment this when changing class structure.
Definition: Gadgets_body.php:22
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:258