r97166 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97165‎ | r97166 | r97167 >
Date:15:57, 15 September 2011
Author:catrope
Status:ok (Comments)
Tags:
Comment:
RL2: Followup r97154, various fixes for ext.gadgets.api . This unbreaks Special:GadgetManager; the foreign*() functions are still untested
* Indentation was still a bit messed up
* Initialize gadgetCategoryCache as an object, initializing it as null causes errors
* Replace this.conf, which never existed, with mw.gadgets.conf
* Rather than defaulting to mw.util.wikiScript('api') when repoName is not set, make repoName default to 'local'. This prevents various annoying things that might happen to the cache
* Replace reference to nonexistent variable data with queryData. Remnant from renaming data to queryData in the Etherpad
Modified paths:
  • /branches/RL2/extensions/Gadgets/modules/ext.gadgets.api.js (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/modules/ext.gadgets.api.js
@@ -17,7 +17,7 @@
1818 * @var {Object} Keyed by repo, array of category objects
1919 * @example { repoName: [ {name: .., title: .., members: .. }, { .. }, { .. } ] }
2020 */
21 - gadgetCategoryCache = null;
 21+ gadgetCategoryCache = {};
2222
2323 /* Local functions */
2424
@@ -73,14 +73,14 @@
7474 * @param data {Object} Data to put in the cache
7575 */
7676 function cacheGadgetData( repoName, id, data ) {
77 - if ( id === null ) {
78 - gadgetCache[repoName] = data;
79 - } else {
80 - if ( !( repoName in cache ) ) {
81 - gadgetCache[repoName] = {};
 77+ if ( id === null ) {
 78+ gadgetCache[repoName] = data;
 79+ } else {
 80+ if ( !( repoName in gadgetCache ) ) {
 81+ gadgetCache[repoName] = {};
 82+ }
 83+ gadgetCache[repoName][id] = data;
8284 }
83 - gadgetCache[repoName][id] = data;
84 - }
8585 }
8686
8787 /* Public functions */
@@ -103,20 +103,21 @@
104104 // Find out how many repos there are
105105 // Needs to be in a separate loop because we have to have the final number ready
106106 // before we fire the first potentially (since it could be cached) async request
107 - for ( repo in this.conf.repos ) {
 107+ for ( repo in mw.gadgets.conf.repos ) {
108108 numRepos++;
109109 }
110110
111 - for ( repo in this.conf.repos ) {
112 - mw.gadgets.api.getGadgetData( null, function( data ) {
113 - combined[repo] = data;
114 - if ( ++successes === numRepos ) {
115 - success( combined );
116 - }
117 - }, function( errorCode ) {
118 - error( errorCode );
119 - }, repo
120 - );
 111+ for ( repo in mw.gadgets.conf.repos ) {
 112+ mw.gadgets.api.getGadgetData( null,
 113+ function( data ) {
 114+ combined[repo] = data;
 115+ if ( ++successes === numRepos ) {
 116+ success( combined );
 117+ }
 118+ }, function( errorCode ) {
 119+ error( errorCode );
 120+ }, repo
 121+ );
121122 }
122123 },
123124
@@ -133,18 +134,19 @@
134135 // Find out how many repos there are
135136 // Needs to be in a separate loop because we have to have the final number ready
136137 // before we fire the first async request
137 - for ( repo in this.conf.repos ) {
 138+ for ( repo in mw.gadgets.conf.repos ) {
138139 numRepos++;
139140 }
140141
141 - for ( repo in this.conf.repos ) {
142 - mw.gadgets.api.getGadgetCategories( function( data ) {
143 - combined[repo] = data;
144 - if ( ++successes === numRepos ) {
145 - success( combined );
146 - }
 142+ for ( repo in mw.gadgets.conf.repos ) {
 143+ mw.gadgets.api.getGadgetCategories(
 144+ function( data ) {
 145+ combined[repo] = data;
 146+ if ( ++successes === numRepos ) {
 147+ success( combined );
 148+ }
147149 }, function( errorCode ) {
148 - error( errorCode );
 150+ error( errorCode );
149151 }, repo
150152 );
151153 }
@@ -156,9 +158,10 @@
157159 * @param success {Function} To be called with the gadget object or array of gadget objects as first argument.
158160 * @param error {Function} If something went wrong (inexisting gadget, api
159161 * error, request error), this is called with error code as first argument.
160 - * @param repoName {String} Name of the repository, key in this.conf.repos
 162+ * @param repoName {String} Name of the repository, key in mw.gadgets.conf.repos. Defaults to 'local'
161163 */
162164 getGadgetData: function( id, success, error, repoName ) {
 165+ repoName = repoName || 'local';
163166 // Check cache
164167 if ( repoName in gadgetCache && gadgetCache[repoName] !== null ) {
165168 if ( id === null ) {
@@ -178,10 +181,10 @@
179182 galanguage: mw.config.get( 'wgUserLanguage' )
180183 };
181184 if ( id !== null ) {
182 - data.gaids = id;
 185+ queryData.gaids = id;
183186 }
184187 $.ajax({
185 - url: this.conf.repos[repoName].apiScript || mw.util.wikiScript( 'api' ),
 188+ url: mw.gadgets.conf.repos[repoName].apiScript,
186189 data: queryData,
187190 type: 'GET',
188191 dataType: 'json',
@@ -218,10 +221,11 @@
219222 *
220223 * @param success {Function} To be called with an array as first argument.
221224 * @param error {Function} To be called with a string (error code) as first argument.
222 - * @param repoName {String} Name of the repository, key in this.conf.repos
 225+ * @param repoName {String} Name of the repository, key in mw.gadgets.conf.repos . Defaults to 'local'
223226 * @return {jqXHR|Null}: Null if served from cache, otherwise the jqXHR.
224227 */
225228 getGadgetCategories: function( success, error, repoName ) {
 229+ repoName = repoName || 'local';
226230 // Check cache
227231 if ( repoName in gadgetCategoryCache && gadgetCategoryCache[repoName] !== null ) {
228232 success( arrClone( gadgetCategoryCache[repoName] ) );
@@ -229,7 +233,7 @@
230234 }
231235 // Get from API if not cached
232236 return $.ajax({
233 - url: this.conf.repos[repoName].apiScript || mw.util.wikiScript( 'api' ),
 237+ url: mw.gadgets.conf.repos[repoName].apiScript,
234238 data: {
235239 format: 'json',
236240 action: 'query',

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r97154RL2: Rewrite of ext.gadgets.api that Timo and I worked on together at http://......catrope14:17, 15 September 2011

Comments

#Comment by Krinkle (talk | contribs)   02:00, 17 September 2011

Replace this.conf, which never existed, with mw.gadgets.conf

Nice catch. In most cases 'this' woud've referred to mw.gadgets.api (since that's the object being declared). But it was broken on the Etherpad indeed.

Status & tagging log