Index: trunk/extensions/Gadgets/ApiQueryGadgets.php |
— | — | @@ -20,7 +20,11 @@ |
21 | 21 | */ |
22 | 22 | |
23 | 23 | class ApiQueryGadgets extends ApiQueryBase { |
24 | | - private $props; |
| 24 | + private $props, |
| 25 | + $category, |
| 26 | + $neededNames, |
| 27 | + $listAllowed, |
| 28 | + $listEnabled; |
25 | 29 | |
26 | 30 | public function __construct( $query, $moduleName ) { |
27 | 31 | parent::__construct( $query, $moduleName, 'ga' ); |
— | — | @@ -29,7 +33,18 @@ |
30 | 34 | public function execute() { |
31 | 35 | $params = $this->extractRequestParams(); |
32 | 36 | $this->props = array_flip( $params['prop'] ); |
| 37 | + $this->categories = isset( $params['categories'] ) |
| 38 | + ? array_flip( $params['categories'] ) |
| 39 | + : false; |
| 40 | + $this->neededNames = isset( $params['names'] ) |
| 41 | + ? array_flip( $params['names'] ) |
| 42 | + : false; |
| 43 | + $this->listAllowed = isset( $params['allowed'] ) && $params['allowed']; |
| 44 | + $this->listEnabled = isset( $params['enabled'] ) && $params['enabled']; |
33 | 45 | |
| 46 | + $this->getMain()->setCacheMode( $this->listAllowed || $this->listEnabled |
| 47 | + ? 'anon-public-user-private' : 'public' ); |
| 48 | + |
34 | 49 | $this->applyList( $this->getList() ); |
35 | 50 | } |
36 | 51 | |
— | — | @@ -38,6 +53,9 @@ |
39 | 54 | |
40 | 55 | $result = array(); |
41 | 56 | foreach ( $gadgets as $category => $list ) { |
| 57 | + if ( $this->categories && !isset( $this->categories[$category] ) ) { |
| 58 | + continue; |
| 59 | + } |
42 | 60 | foreach ( $list as $g ) { |
43 | 61 | if ( $this->isNeeded( $g ) ) { |
44 | 62 | $result[] = $g; |
— | — | @@ -100,7 +118,11 @@ |
101 | 119 | * |
102 | 120 | */ |
103 | 121 | private function isNeeded( Gadget $gadget ) { |
104 | | - return true; |
| 122 | + global $wgUser; |
| 123 | + |
| 124 | + return ( $this->neededNames === false || isset( $this->neededNames[$gadget->getName()] ) ) |
| 125 | + && ( !$this->listAllowed || $gadget->isAllowed( $wgUser ) ) |
| 126 | + && ( !$this->listEnabled || $gadget->isEnabled( $wgUser ) ); |
105 | 127 | } |
106 | 128 | |
107 | 129 | public function getAllowedParams() { |
— | — | @@ -122,6 +144,16 @@ |
123 | 145 | 'definition', |
124 | 146 | ), |
125 | 147 | ), |
| 148 | + 'categories' => array( |
| 149 | + ApiBase::PARAM_ISMULTI => true, |
| 150 | + ApiBase::PARAM_TYPE => 'string', |
| 151 | + ), |
| 152 | + 'names' => array( |
| 153 | + ApiBase::PARAM_TYPE => 'string', |
| 154 | + ApiBase::PARAM_ISMULTI => true, |
| 155 | + ), |
| 156 | + 'allowed' => false, |
| 157 | + 'enabled' => false, |
126 | 158 | ); |
127 | 159 | } |
128 | 160 | |
— | — | @@ -134,7 +166,7 @@ |
135 | 167 | 'prop' => array( |
136 | 168 | 'What gadget information to get:', |
137 | 169 | ' name - Internal gadget name', |
138 | | - ' desc - Gadget description transformed into HTML', |
| 170 | + ' desc - Gadget description transformed into HTML (can be slow, use only if really needed)', |
139 | 171 | ' desc-raw - Gadget description in raw wikitext', |
140 | 172 | ' category - Internal name of a category gadget belongs to (empty if top-level gadget)', |
141 | 173 | ' resourceloader - Whether gadget supports ResourceLoader', |
— | — | @@ -145,13 +177,27 @@ |
146 | 178 | ' default - Whether gadget is enabled by default', |
147 | 179 | ' definition - Line from MediaWiki:Gadgets-definition used to define the gadget', |
148 | 180 | ), |
| 181 | + 'categories' => 'Gadgets from what categories to retrieve', |
| 182 | + 'names' => 'Name(s) of gadgets to retrieve', |
| 183 | + 'allowed' => 'List only gadgets allowed to current user', |
| 184 | + 'enabled' => 'List only gadgets enabled by current user', |
149 | 185 | ); |
150 | 186 | } |
151 | 187 | |
152 | 188 | protected function getExamples() { |
| 189 | + $params = $this->getAllowedParams(); |
| 190 | + $allProps = implode( '|', $params['prop'][ApiBase::PARAM_TYPE] ); |
153 | 191 | return array( |
154 | 192 | 'Get a list of gadgets along with their descriptions:', |
155 | 193 | ' api.php?action=query&list=gadgets&gaprop=name|desc', |
| 194 | + 'Get a list of gadgets with all possble properties:', |
| 195 | + " api.php?action=query&list=gadgets&gaprop=$allProps", |
| 196 | + 'Get a list of gadgets belonging to caregory "foo":', |
| 197 | + ' api.php?action=query&list=gadgets&gacategories=foo', |
| 198 | + 'Get information about gadgets named "foo" and "bar":', |
| 199 | + ' api.php?action=query&list=gadgets&ganames=foo|bar&gaprop=name|desc|category', |
| 200 | + 'Get a list of gadgets enabled by current user:', |
| 201 | + ' api.php?action=query&list=gadgets&gaenabled', |
156 | 202 | ); |
157 | 203 | } |
158 | 204 | |
— | — | @@ -159,4 +205,4 @@ |
160 | 206 | return __CLASS__ . ': $Id$'; |
161 | 207 | } |
162 | 208 | |
163 | | -} |
\ No newline at end of file |
| 209 | +} |