r94009 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94008‎ | r94009 | r94010 >
Date:08:27, 6 August 2011
Author:catrope
Status:deferred
Tags:
Comment:
RL2: Update the list=gadgets API module to work with the new backend. This drops support for the definition property, adds the JSON property, and deprecates most of the other properties (but keeps them for backwards compatibility)
Modified paths:
  • /branches/RL2/extensions/Gadgets/api/ApiQueryGadgets.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/api/ApiQueryGadgets.php
@@ -41,6 +41,7 @@
4242 : false;
4343 $this->listAllowed = isset( $params['allowedonly'] ) && $params['allowedonly'];
4444 $this->listEnabled = isset( $params['enabledonly'] ) && $params['enabledonly'];
 45+ $this->listShared = isset( $params['sharedonly'] ) && $params['sharedonly'];
4546
4647 $this->getMain()->setCacheMode( $this->listAllowed || $this->listEnabled
4748 ? 'anon-public-user-private' : 'public' );
@@ -49,19 +50,24 @@
5051 }
5152
5253 private function getList() {
53 - $gadgets = Gadget::loadStructuredList();
54 -
 54+ $repo = new LocalGadgetRepo( array() );
5555 $result = array();
56 - foreach ( $gadgets as $category => $list ) {
57 - if ( $this->categories && !isset( $this->categories[$category] ) ) {
58 - continue;
 56+
 57+ if ( $this->neededNames ) {
 58+ // Get all requested gadgets by name
 59+ $names = $this->neededNames;
 60+ } else {
 61+ // Get them all
 62+ $names = $repo->getGadgetNames();
 63+ }
 64+
 65+ foreach ( $names as $name ) {
 66+ $gadget = $repo->getGadget( $name );
 67+ if ( $gadget && $this->isNeeded( $gadget ) ) {
 68+ $result[$name] = $gadget;
5969 }
60 - foreach ( $list as $g ) {
61 - if ( $this->isNeeded( $g ) ) {
62 - $result[] = $g;
63 - }
64 - }
6570 }
 71+
6672 return $result;
6773 }
6874
@@ -69,21 +75,26 @@
7076 $data = array();
7177 $result = $this->getResult();
7278
73 - foreach ( $gadgets as $g ) {
 79+ foreach ( $gadgets as $name => $g ) {
7480 $row = array();
7581 if ( isset( $this->props['name'] ) ) {
76 - $row['name'] = $g->getName();
 82+ $row['name'] = $name;
7783 }
 84+ if ( isset( $this->props['json'] ) ) {
 85+ $row['json'] = $g->getJSON();
 86+ }
7887 if ( isset( $this->props['desc'] ) ) {
79 - $row['desc'] = $g->getDescription();
 88+ $row['desc'] = wfMessage( $g->getDescriptionMsg() )->parse();
8089 }
8190 if ( isset( $this->props['desc-raw'] ) ) {
82 - $row['desc-raw'] = $g->getRawDescription();
 91+ $row['desc-raw'] = $row['desc'] = wfMessage( $g->getDescriptionMsg() )->plain();
8392 }
8493 if ( isset( $this->props['category'] ) ) {
85 - $row['category'] = $g->getCategory();
 94+ $row['category'] = $g->getSection(); // TODO: clean up category vs. section mess in favor category
8695 }
87 - if ( isset( $this->props['resourceloader'] ) && $g->supportsResourceLoader() ) {
 96+ if ( isset( $this->props['resourceloader'] ) /*&& $g->supportsResourceLoader()*/ ) {
 97+ // Everything supports resourceloader now :D
 98+ // FIXME: We need to figure something out for legacy gadgets, or at least MaxSem thinks so
8899 $row['resourceloader'] = '';
89100 }
90101 if ( isset( $this->props['scripts'] ) ) {
@@ -102,12 +113,9 @@
103114 $row['rights'] = $g->getRequiredRights();
104115 $result->setIndexedTagName( $row['rights'], 'right' );
105116 }
106 - if ( isset( $this->props['default'] ) && $g->isOnByDefault() ) {
 117+ if ( isset( $this->props['default'] ) && $g->isEnabledByDefault() ) {
107118 $row['default'] = '';
108119 }
109 - if ( isset( $this->props['definition'] ) ) {
110 - $row['definition'] = $g->getDefinition();
111 - }
112120 $data[] = $row;
113121 }
114122 $result->setIndexedTagName( $data, 'gadget' );
@@ -122,16 +130,19 @@
123131
124132 return ( $this->neededNames === false || isset( $this->neededNames[$gadget->getName()] ) )
125133 && ( !$this->listAllowed || $gadget->isAllowed( $wgUser ) )
126 - && ( !$this->listEnabled || $gadget->isEnabled( $wgUser ) );
 134+ && ( !$this->listEnabled || $gadget->isEnabled( $wgUser ) )
 135+ && ( !$this->listShared || $gadget->isShared() )
 136+ && ( !$this->categories || isset( $this->categories[$g->getSection()] ) );
127137 }
128138
129139 public function getAllowedParams() {
130140 return array(
131141 'prop' => array(
132 - ApiBase::PARAM_DFLT => 'name',
 142+ ApiBase::PARAM_DFLT => 'name|json',
133143 ApiBase::PARAM_ISMULTI => true,
134144 ApiBase::PARAM_TYPE => array(
135145 'name',
 146+ 'json',
136147 'desc',
137148 'desc-raw',
138149 'category',
@@ -141,7 +152,6 @@
142153 'dependencies',
143154 'rights',
144155 'default',
145 - 'definition',
146156 ),
147157 ),
148158 'categories' => array(
@@ -154,6 +164,7 @@
155165 ),
156166 'allowedonly' => false,
157167 'enabledonly' => false,
 168+ 'sharedonly' => false,
158169 );
159170 }
160171
@@ -166,6 +177,7 @@
167178 'prop' => array(
168179 'What gadget information to get:',
169180 ' name - Internal gadget name',
 181+ ' json - JSON representation of the gadget metadata. All other prop attributes below are deprecated but provided for backwards compatibility',
170182 ' desc - Gadget description transformed into HTML (can be slow, use only if really needed)',
171183 ' desc-raw - Gadget description in raw wikitext',
172184 ' category - Internal name of a category gadget belongs to (empty if top-level gadget)',
@@ -175,12 +187,12 @@
176188 ' dependencies - List of ResourceLoader modules gadget depends on',
177189 ' rights - List of rights required to use gadget, if any',
178190 ' default - Whether gadget is enabled by default',
179 - ' definition - Line from MediaWiki:Gadgets-definition used to define the gadget',
180191 ),
181192 'categories' => 'Gadgets from what categories to retrieve',
182193 'names' => 'Name(s) of gadgets to retrieve',
183194 'allowedonly' => 'List only gadgets allowed to current user',
184195 'enabledonly' => 'List only gadgets enabled by current user',
 196+ 'sharedonly' => 'Only list shared gadgets',
185197 );
186198 }
187199

Status & tagging log