r86226 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86225‎ | r86226 | r86227 >
Date:18:39, 16 April 2011
Author:maxsem
Status:ok (Comments)
Tags:
Comment:
Gadgets API: allow filtering by categories, gadget names, allowedness and enabledness (d'oh!)
Modified paths:
  • /trunk/extensions/Gadgets/ApiQueryGadgets.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Gadgets/ApiQueryGadgets.php
@@ -20,7 +20,11 @@
2121 */
2222
2323 class ApiQueryGadgets extends ApiQueryBase {
24 - private $props;
 24+ private $props,
 25+ $category,
 26+ $neededNames,
 27+ $listAllowed,
 28+ $listEnabled;
2529
2630 public function __construct( $query, $moduleName ) {
2731 parent::__construct( $query, $moduleName, 'ga' );
@@ -29,7 +33,18 @@
3034 public function execute() {
3135 $params = $this->extractRequestParams();
3236 $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'];
3345
 46+ $this->getMain()->setCacheMode( $this->listAllowed || $this->listEnabled
 47+ ? 'anon-public-user-private' : 'public' );
 48+
3449 $this->applyList( $this->getList() );
3550 }
3651
@@ -38,6 +53,9 @@
3954
4055 $result = array();
4156 foreach ( $gadgets as $category => $list ) {
 57+ if ( $this->categories && !isset( $this->categories[$category] ) ) {
 58+ continue;
 59+ }
4260 foreach ( $list as $g ) {
4361 if ( $this->isNeeded( $g ) ) {
4462 $result[] = $g;
@@ -100,7 +118,11 @@
101119 *
102120 */
103121 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 ) );
105127 }
106128
107129 public function getAllowedParams() {
@@ -122,6 +144,16 @@
123145 'definition',
124146 ),
125147 ),
 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,
126158 );
127159 }
128160
@@ -134,7 +166,7 @@
135167 'prop' => array(
136168 'What gadget information to get:',
137169 ' 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)',
139171 ' desc-raw - Gadget description in raw wikitext',
140172 ' category - Internal name of a category gadget belongs to (empty if top-level gadget)',
141173 ' resourceloader - Whether gadget supports ResourceLoader',
@@ -145,13 +177,27 @@
146178 ' default - Whether gadget is enabled by default',
147179 ' definition - Line from MediaWiki:Gadgets-definition used to define the gadget',
148180 ),
 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',
149185 );
150186 }
151187
152188 protected function getExamples() {
 189+ $params = $this->getAllowedParams();
 190+ $allProps = implode( '|', $params['prop'][ApiBase::PARAM_TYPE] );
153191 return array(
154192 'Get a list of gadgets along with their descriptions:',
155193 ' 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',
156202 );
157203 }
158204
@@ -159,4 +205,4 @@
160206 return __CLASS__ . ': $Id$';
161207 }
162208
163 -}
\ No newline at end of file
 209+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r93052Per CR on r86226 renamed a couple of parametersmaxsem16:01, 25 July 2011

Comments

#Comment by Aaron Schulz (talk | contribs)   01:25, 21 June 2011

Please rename allowed/enabled to allowedonly/enabledonly or something. Tis confusing :)

Status & tagging log