MediaWiki REL1_34
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',
193 'id',
194 'metadata',
195 'desc',
196 ],
197 ],
198 'categories' => [
200 ApiBase::PARAM_TYPE => 'string',
201 ],
202 'ids' => [
203 ApiBase::PARAM_TYPE => 'string',
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}
getMain()
Get the main module.
Definition ApiBase.php:536
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:55
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:58
This is a base class for all Query modules.
Created on 15 April 2011 API for Gadgets extension.
fakeMetadata(Gadget $g)
setIndexedTagNameForMetadata(&$metadata)
array bool $neededIds
__construct(ApiQuery $queryModule, $moduleName)
array bool $categories
isNeeded(Gadget $gadget)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
This is the main query class.
Definition ApiQuery.php:37
static singleton()
Get the configured default GadgetRepo.
Wrapper for one gadget.
Definition Gadget.php:17
getName()
Definition Gadget.php:121
getStyles()
Definition Gadget.php:234
isOnByDefault()
Definition Gadget.php:178
getCategory()
Definition Gadget.php:142
getScripts()
Definition Gadget.php:227
getPeers()
Get list of extra modules that should be loaded when this gadget is enabled.
Definition Gadget.php:280
getLegacyScripts()
Returns list of scripts that don't support ResourceLoader.
Definition Gadget.php:256
getRequiredSkins()
Returns array of skins where this gadget works.
Definition Gadget.php:303
isAllowed(User $user)
Checks whether given user has permissions to use this gadget.
Definition Gadget.php:170
getDependencies()
Returns names of resources this gadget depends on.
Definition Gadget.php:267
isHidden()
Definition Gadget.php:185
isEnabled( $user)
Checks whether this gadget is enabled for given user.
Definition Gadget.php:160
getRequiredRights()
Returns array of permissions required by this gadget.
Definition Gadget.php:295
getMessages()
Definition Gadget.php:287