MediaWiki  1.34.0
ApiQueryGadgets.php
Go to the documentation of this file.
1 <?php
26  private $props;
27 
31  private $categories;
32 
36  private $neededIds;
37 
41  private $listAllowed;
42 
46  private $listEnabled;
47 
48  public function __construct( ApiQuery $queryModule, $moduleName ) {
49  parent::__construct( $queryModule, $moduleName, 'ga' );
50  }
51 
52  public function execute() {
53  $params = $this->extractRequestParams();
54  $this->props = array_flip( $params['prop'] );
55  $this->categories = isset( $params['categories'] )
56  ? array_flip( $params['categories'] )
57  : false;
58  $this->neededIds = isset( $params['ids'] )
59  ? array_flip( $params['ids'] )
60  : false;
61  $this->listAllowed = isset( $params['allowedonly'] ) && $params['allowedonly'];
62  $this->listEnabled = isset( $params['enabledonly'] ) && $params['enabledonly'];
63 
64  $this->getMain()->setCacheMode( $this->listAllowed || $this->listEnabled
65  ? 'anon-public-user-private' : 'public' );
66 
67  $this->applyList( $this->getList() );
68  }
69 
73  private function getList() {
74  $gadgets = GadgetRepo::singleton()->getStructuredList();
75 
76  if ( !$gadgets ) {
77  return [];
78  }
79 
80  $result = [];
81  foreach ( $gadgets as $category => $list ) {
82  if ( $this->categories && !isset( $this->categories[$category] ) ) {
83  continue;
84  }
85 
86  foreach ( $list as $g ) {
87  if ( $this->isNeeded( $g ) ) {
88  $result[] = $g;
89  }
90  }
91  }
92  return $result;
93  }
94 
98  private function applyList( $gadgets ) {
99  $data = [];
100  $result = $this->getResult();
101 
105  foreach ( $gadgets as $g ) {
106  $row = [];
107  if ( isset( $this->props['id'] ) ) {
108  $row['id'] = $g->getName();
109  }
110 
111  if ( isset( $this->props['metadata'] ) ) {
112  $row['metadata'] = $this->fakeMetadata( $g );
113  $this->setIndexedTagNameForMetadata( $row['metadata'] );
114  }
115 
116  if ( isset( $this->props['desc'] ) ) {
117  $row['desc'] = $g->getDescription();
118  }
119 
120  $data[] = $row;
121  }
122 
123  $result->setIndexedTagName( $data, 'gadget' );
124  $result->addValue( 'query', $this->getModuleName(), $data );
125  }
126 
132  private function isNeeded( Gadget $gadget ) {
133  $user = $this->getUser();
134 
135  return ( $this->neededIds === false || isset( $this->neededIds[$gadget->getName()] ) )
136  && ( !$this->listAllowed || $gadget->isAllowed( $user ) )
137  && ( !$this->listEnabled || $gadget->isEnabled( $user ) );
138  }
139 
144  private function fakeMetadata( Gadget $g ) {
145  return [
146  'settings' => [
147  'rights' => $g->getRequiredRights(),
148  'skins' => $g->getRequiredSkins(),
149  'default' => $g->isOnByDefault(),
150  'hidden' => $g->isHidden(),
151  'shared' => false,
152  'category' => $g->getCategory(),
153  'legacyscripts' => (bool)$g->getLegacyScripts(),
154  ],
155  'module' => [
156  'scripts' => $g->getScripts(),
157  'styles' => $g->getStyles(),
158  'dependencies' => $g->getDependencies(),
159  'peers' => $g->getPeers(),
160  'messages' => $g->getMessages(),
161  ]
162  ];
163  }
164 
165  private function setIndexedTagNameForMetadata( &$metadata ) {
166  static $tagNames = [
167  'rights' => 'right',
168  'skins' => 'skin',
169  'scripts' => 'script',
170  'styles' => 'style',
171  'dependencies' => 'dependency',
172  'peers' => 'peer',
173  'messages' => 'message',
174  ];
175 
176  $result = $this->getResult();
177  foreach ( $metadata as $data ) {
178  foreach ( $data as $key => $value ) {
179  if ( is_array( $value ) ) {
180  $tag = $tagNames[$key] ?? $key;
181  $result->setIndexedTagName( $value, $tag );
182  }
183  }
184  }
185  }
186 
187  public function getAllowedParams() {
188  return [
189  'prop' => [
190  ApiBase::PARAM_DFLT => 'id|metadata',
191  ApiBase::PARAM_ISMULTI => true,
193  'id',
194  'metadata',
195  'desc',
196  ],
197  ],
198  'categories' => [
199  ApiBase::PARAM_ISMULTI => true,
200  ApiBase::PARAM_TYPE => 'string',
201  ],
202  'ids' => [
203  ApiBase::PARAM_TYPE => 'string',
204  ApiBase::PARAM_ISMULTI => true,
205  ],
206  'allowedonly' => false,
207  'enabledonly' => false,
208  ];
209  }
210 
215  protected function getExamplesMessages() {
216  $params = $this->getAllowedParams();
217  $allProps = implode( '|', $params['prop'][ApiBase::PARAM_TYPE] );
218  return [
219  'action=query&list=gadgets&gaprop=id|desc'
220  => 'apihelp-query+gadgets-example-1',
221  "action=query&list=gadgets&gaprop=$allProps"
222  => 'apihelp-query+gadgets-example-2',
223  'action=query&list=gadgets&gacategories=foo'
224  => 'apihelp-query+gadgets-example-3',
225  'action=query&list=gadgets&gaids=foo|bar&gaprop=id|desc|metadata'
226  => 'apihelp-query+gadgets-example-4',
227  'action=query&list=gadgets&gaenabledonly'
228  => 'apihelp-query+gadgets-example-5',
229  ];
230  }
231 }
Gadget\isAllowed
isAllowed(User $user)
Checks whether given user has permissions to use this gadget.
Definition: Gadget.php:170
ApiQueryGadgets\$props
array $props
Definition: ApiQueryGadgets.php:26
ApiQueryGadgets\applyList
applyList( $gadgets)
Definition: ApiQueryGadgets.php:98
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
Gadget\isOnByDefault
isOnByDefault()
Definition: Gadget.php:178
Gadget\getStyles
getStyles()
Definition: Gadget.php:234
ApiQueryGadgets\getList
getList()
Definition: ApiQueryGadgets.php:73
Gadget\getRequiredRights
getRequiredRights()
Returns array of permissions required by this gadget.
Definition: Gadget.php:295
Gadget\getMessages
getMessages()
Definition: Gadget.php:287
ApiQueryGadgets\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryGadgets.php:52
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
Gadget\getScripts
getScripts()
Definition: Gadget.php:227
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
Gadget\getPeers
getPeers()
Get list of extra modules that should be loaded when this gadget is enabled.
Definition: Gadget.php:280
ApiQueryGadgets\isNeeded
isNeeded(Gadget $gadget)
Definition: ApiQueryGadgets.php:132
ApiQueryGadgets\getExamplesMessages
getExamplesMessages()
Definition: ApiQueryGadgets.php:215
ApiQueryGadgets
Created on 15 April 2011 API for Gadgets extension.
Definition: ApiQueryGadgets.php:22
ApiQueryGadgets\fakeMetadata
fakeMetadata(Gadget $g)
Definition: ApiQueryGadgets.php:144
ApiQueryGadgets\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryGadgets.php:187
Gadget\isHidden
isHidden()
Definition: Gadget.php:185
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:34
ApiQueryGadgets\$listEnabled
bool $listEnabled
Definition: ApiQueryGadgets.php:46
Gadget
Wrapper for one gadget.
Definition: Gadget.php:17
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
ApiQueryGadgets\$neededIds
array bool $neededIds
Definition: ApiQueryGadgets.php:36
GadgetRepo\singleton
static singleton()
Get the configured default GadgetRepo.
Definition: GadgetRepo.php:88
Gadget\getName
getName()
Definition: Gadget.php:121
Gadget\getRequiredSkins
getRequiredSkins()
Returns array of skins where this gadget works.
Definition: Gadget.php:303
Gadget\getLegacyScripts
getLegacyScripts()
Returns list of scripts that don't support ResourceLoader.
Definition: Gadget.php:256
ApiQueryGadgets\setIndexedTagNameForMetadata
setIndexedTagNameForMetadata(&$metadata)
Definition: ApiQueryGadgets.php:165
ApiQueryGadgets\__construct
__construct(ApiQuery $queryModule, $moduleName)
Definition: ApiQueryGadgets.php:48
ApiQueryGadgets\$categories
array bool $categories
Definition: ApiQueryGadgets.php:31
Gadget\isEnabled
isEnabled( $user)
Checks whether this gadget is enabled for given user.
Definition: Gadget.php:160
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:536
Gadget\getCategory
getCategory()
Definition: Gadget.php:142
ApiQueryGadgets\$listAllowed
bool $listAllowed
Definition: ApiQueryGadgets.php:41
Gadget\getDependencies
getDependencies()
Returns names of resources this gadget depends on.
Definition: Gadget.php:267