Index: branches/RL2/extensions/Gadgets/modules/ext.gadgets.api.js |
— | — | @@ -1,24 +1,26 @@ |
2 | 2 | /** |
3 | | - * Implement the editing API for the gadget manager. |
| 3 | + * Interface to the API for the gadgets extension. |
4 | 4 | * |
5 | 5 | * @author Timo Tijhof |
6 | | - * @copyright © 2011 Timo Tijhof |
| 6 | + * @author Roan Kattouw |
7 | 7 | * @license GNU General Public Licence 2.0 or later |
8 | 8 | */ |
| 9 | + |
9 | 10 | ( function( $ ) { |
10 | | - |
11 | 11 | var |
12 | 12 | /** |
13 | | - * @var {Object} Keyed by gadget id, contains the metadata as an object. |
| 13 | + * @var {Object} Keyed by repo, object of gadget objects by id |
| 14 | + * @example { repoName: { gadgetID: { title: .., metadata: ..}, otherId: { .. } } } |
14 | 15 | */ |
15 | 16 | gadgetCache = {}, |
16 | 17 | /** |
17 | | - * @var {Object} If cached, object keyed by category id with categormember-count as value. |
18 | | - * Set to null if there is no cache, yet, or when the cache is cleared. */ |
| 18 | + * @var {Object} Keyed by repo, array of category objects |
| 19 | + * @example { repoName: [ {name: .., title: .., members: .. }, { .. }, { .. } ] } |
| 20 | + */ |
19 | 21 | gadgetCategoryCache = null; |
20 | | - |
| 22 | + |
21 | 23 | /* Local functions */ |
22 | | - |
| 24 | + |
23 | 25 | /** |
24 | 26 | * For most returns from api.* functions, a clone is made when data from |
25 | 27 | * cache is used. This is to avoid situations where later modifications |
— | — | @@ -26,7 +28,7 @@ |
27 | 29 | * the object would otherwise be passed by reference). |
28 | 30 | */ |
29 | 31 | function objClone( obj ) { |
30 | | - /** |
| 32 | + /* |
31 | 33 | * A normal `$.extend( {}, obj );` is not suffecient, |
32 | 34 | * it has to be recursive, because the values of this |
33 | 35 | * object are also refererenes to objects. |
— | — | @@ -40,55 +42,163 @@ |
41 | 43 | */ |
42 | 44 | return $.extend( true /* recursive */, {}, obj ); |
43 | 45 | } |
| 46 | + |
44 | 47 | function arrClone( arr ) { |
45 | 48 | return arr.slice(); |
46 | 49 | } |
47 | | - |
| 50 | + |
| 51 | + /** |
| 52 | + * Reformat an array of gadget objects, into an object keyed by the id. |
| 53 | + * Note: Maintains object reference |
| 54 | + * @param arr {Array} |
| 55 | + * @return {Object} |
| 56 | + */ |
| 57 | + function gadgetArrToObj( arr ) { |
| 58 | + for( var obj = {}, i = 0, g = arr[i], len = arr.length; i < len; g = arr[++i] ) { |
| 59 | + obj[g.id] = g; |
| 60 | + } |
| 61 | + return obj; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Write data to gadgetCache, taking into account that id may be null |
| 66 | + * and working around JS's annoying refusal to just let us do |
| 67 | + * var foo = {}; foo[bar][baz] = quux; |
| 68 | + * |
| 69 | + * This sets gadgetCache[repoName][id] = data; if id is not null, |
| 70 | + * or gadgetCache[repoName] = data; if id is null. |
| 71 | + * |
| 72 | + * @param repoName {String} Repository name |
| 73 | + * @param id {String|null} Gadget ID or null |
| 74 | + * @param data {Object} Data to put in the cache |
| 75 | + */ |
| 76 | + function cacheGadgetData( repoName, id, data ) { |
| 77 | + if ( id === null ) { |
| 78 | + gadgetCache[repoName] = data; |
| 79 | + } else { |
| 80 | + if ( !( repoName in cache ) ) { |
| 81 | + gadgetCache[repoName] = {}; |
| 82 | + } |
| 83 | + gadgetCache[repoName][id] = data; |
| 84 | + } |
| 85 | + } |
| 86 | + |
48 | 87 | /* Public functions */ |
49 | | - |
50 | | - mw.gadgetManager = { |
51 | | - |
52 | | - conf: mw.config.get( 'gadgetManagerConf' ), |
53 | | - |
| 88 | + |
| 89 | + mw.gadgets = { |
| 90 | + /** |
| 91 | + * @todo: Add something derived from $wgGadgetRepositories to gadgetsConf |
| 92 | + * ... + repos: { local: { apiScript: .. }, awesomeRepo: { .. }, .. } |
| 93 | + */ |
| 94 | + conf: mw.config.get( 'gadgetsConf' ), |
54 | 95 | api: { |
55 | | - |
56 | 96 | /** |
| 97 | + * Get the gadget blobs for all gadgets from all repositories. |
| 98 | + * |
| 99 | + * @param success {Function} To be called with an object of arrays of gadget objects, keyed by repository name, as first argument. |
| 100 | + * @param error {Function} To be called with a string (error code) as first argument. |
| 101 | + */ |
| 102 | + getForeignGadgetsData: function( success, error ) { |
| 103 | + var combined = {}, successes = 0, numRepos = 0, repo; |
| 104 | + // Find out how many repos there are |
| 105 | + // Needs to be in a separate loop because we have to have the final number ready |
| 106 | + // before we fire the first potentially (since it could be cached) async request |
| 107 | + for ( repo in this.conf.repos ) { |
| 108 | + numRepos++; |
| 109 | + } |
| 110 | + |
| 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 | + ); |
| 121 | + } |
| 122 | + }, |
| 123 | + |
| 124 | + /** |
| 125 | + * Get the gadget categories from all repositories. |
| 126 | + * |
| 127 | + * @param success {Function} To be called with an array |
| 128 | + * @param success {Function} To be called with an object of arrays of category objects, keyed by repository name, as first argument. |
| 129 | + * @param error {Function} To be called with a string (error code) as the first argument. |
| 130 | + */ |
| 131 | + getForeignGadgetCategories: function( success, error ){ |
| 132 | + // TODO: Almost entirely duplicated from the function above. Factoring this out is easy |
| 133 | + var combined = {}, successes = 0, numRepos = 0, repo; |
| 134 | + // Find out how many repos there are |
| 135 | + // Needs to be in a separate loop because we have to have the final number ready |
| 136 | + // before we fire the first async request |
| 137 | + for ( repo in this.conf.repos ) { |
| 138 | + numRepos++; |
| 139 | + } |
| 140 | + |
| 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 | + } |
| 147 | + }, function( errorCode ) { |
| 148 | + error( errorCode ); |
| 149 | + }, repo |
| 150 | + ); |
| 151 | + } |
| 152 | + }, |
| 153 | + /** |
57 | 154 | * Get gadget blob from the API (or from cache if available). |
58 | 155 | * |
59 | | - * @param id {String} Gadget id. |
60 | | - * @param success {Function} To be called with the gadget object as first argument. |
61 | | - * @param error {Fucntion} If something went wrong (inexisting gadget, api |
| 156 | + * @param id {String|null} Gadget id, or null to get all from the repo. |
| 157 | + * @param success {Function} To be called with the gadget object or array of gadget objects as first argument. |
| 158 | + * @param error {Function} If something went wrong (inexisting gadget, api |
62 | 159 | * error, request error), this is called with error code as first argument. |
63 | | - * @return {jqXHR|Null}: Null if served from cache, otherwise the jqXHR. |
| 160 | + * @param repoName {String} Name of the repository, key in this.conf.repos |
64 | 161 | */ |
65 | | - getGadgetData: function( id, success, error ) { |
| 162 | + getGadgetData: function( id, success, error, repoName ) { |
66 | 163 | // Check cache |
67 | | - if ( id in gadgetCache && gadgetCache[id] !== null ) { |
68 | | - success( objClone( gadgetCache[id] ) ); |
69 | | - return null; |
| 164 | + if ( repoName in gadgetCache && gadgetCache[repoName] !== null ) { |
| 165 | + if ( id === null ) { |
| 166 | + success( objClone( gadgetCache[repoName] ) ); |
| 167 | + return; |
| 168 | + } else if ( id in gadgetCache[repoName] && gadgetCache[repoName][id] !== null ) { |
| 169 | + success( objClone( gadgetCache[repoName][id] ) ); |
| 170 | + return; |
| 171 | + } |
70 | 172 | } |
71 | 173 | // Get from API if not cached |
72 | | - return $.ajax({ |
73 | | - url: mw.util.wikiScript( 'api' ), |
74 | | - data: { |
75 | | - format: 'json', |
76 | | - action: 'query', |
77 | | - list: 'gadgets', |
78 | | - gaprop: 'id|title|metadata|definitiontimestamp', |
79 | | - gaids: id, |
80 | | - galanguage: mw.config.get( 'wgUserLanguage' ) |
81 | | - }, |
| 174 | + var queryData = { |
| 175 | + format: 'json', |
| 176 | + action: 'query', |
| 177 | + list: 'gadgets', |
| 178 | + gaprop: 'id|title|metadata|definitiontimestamp', |
| 179 | + galanguage: mw.config.get( 'wgUserLanguage' ) |
| 180 | + }; |
| 181 | + if ( id !== null ) { |
| 182 | + data.gaids = id; |
| 183 | + } |
| 184 | + $.ajax({ |
| 185 | + url: this.conf.repos[repoName].apiScript || mw.util.wikiScript( 'api' ), |
| 186 | + data: queryData, |
82 | 187 | type: 'GET', |
83 | 188 | dataType: 'json', |
84 | 189 | success: function( data ) { |
85 | | - if ( data && data.query && data.query.gadgets && data.query.gadgets[0] ) { |
86 | | - data = data.query.gadgets[0]; |
| 190 | + if ( data && data.query && data.query.gadgets ) { |
| 191 | + data = data.query.gadgets; |
| 192 | + if ( id !== null ) { |
| 193 | + data = data[0] || null; |
| 194 | + } else { |
| 195 | + data = gadgetArrToObj( data ); |
| 196 | + } |
87 | 197 | // Update cache |
88 | | - gadgetCache[id] = data; |
| 198 | + cacheGadgetData( repoName, id, data ); |
89 | 199 | success( objClone( data ) ); |
90 | 200 | } else { |
91 | 201 | // Invalidate cache |
92 | | - gadgetCache[id] = null; |
| 202 | + cacheGadgetData( repoName, id, null ); |
93 | 203 | if ( data && data.error ) { |
94 | 204 | error( data.error.code ); |
95 | 205 | } else { |
— | — | @@ -98,25 +208,28 @@ |
99 | 209 | }, |
100 | 210 | error: function() { |
101 | 211 | // Invalidate cache |
102 | | - gadgetCache[id] = null; |
| 212 | + cacheGadgetData( repoName, id, null ); |
103 | 213 | error( 'unknown' ); |
104 | 214 | } |
105 | 215 | }); |
106 | 216 | }, |
107 | | - |
108 | 217 | /** |
109 | | - * @param callback {Function} To be called with an array as first argument. |
| 218 | + * Get the gadget categories for a certain repository from the API. |
| 219 | + * |
| 220 | + * @param success {Function} To be called with an array as first argument. |
| 221 | + * @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 |
110 | 223 | * @return {jqXHR|Null}: Null if served from cache, otherwise the jqXHR. |
111 | 224 | */ |
112 | | - getGadgetCategories: function( callback ) { |
| 225 | + getGadgetCategories: function( success, error, repoName ) { |
113 | 226 | // Check cache |
114 | | - if ( gadgetCategoryCache !== null ) { |
115 | | - callback( arrClone( gadgetCategoryCache ) ); |
| 227 | + if ( repoName in gadgetCategoryCache && gadgetCategoryCache[repoName] !== null ) { |
| 228 | + success( arrClone( gadgetCategoryCache[repoName] ) ); |
116 | 229 | return null; |
117 | 230 | } |
118 | 231 | // Get from API if not cached |
119 | 232 | return $.ajax({ |
120 | | - url: mw.util.wikiScript( 'api' ), |
| 233 | + url: this.conf.repos[repoName].apiScript || mw.util.wikiScript( 'api' ), |
121 | 234 | data: { |
122 | 235 | format: 'json', |
123 | 236 | action: 'query', |
— | — | @@ -132,22 +245,25 @@ |
133 | 246 | { |
134 | 247 | data = data.query.gadgetcategories; |
135 | 248 | // Update cache |
136 | | - gadgetCategoryCache = data; |
137 | | - callback( arrClone( data ), 'success' ); |
| 249 | + gadgetCategoryCache[repoName] = data; |
| 250 | + success( arrClone( data ) ); |
138 | 251 | } else { |
139 | 252 | // Invalidate cache |
140 | | - gadgetCategoryCache = null; |
141 | | - callback( [] ); |
| 253 | + gadgetCategoryCache[repoName] = null; |
| 254 | + if ( data && data.error ) { |
| 255 | + error( data.error.code ); |
| 256 | + } else { |
| 257 | + error( 'unknown' ); |
| 258 | + } |
142 | 259 | } |
143 | 260 | }, |
144 | 261 | error: function() { |
145 | 262 | // Invalidate cache |
146 | | - gadgetCategoryCache = null; |
147 | | - callback( [] ); |
| 263 | + gadgetCategoryCache[repoName] = null; |
| 264 | + error( 'unknown' ); |
148 | 265 | } |
149 | 266 | }); |
150 | 267 | }, |
151 | | - |
152 | 268 | /** |
153 | 269 | * Creates or edits an existing gadget definition. |
154 | 270 | * |
— | — | @@ -183,8 +299,7 @@ |
184 | 300 | dataType: 'json', |
185 | 301 | success: function( data ) { |
186 | 302 | // Invalidate cache |
187 | | - gadgetCache[gadget.id] = null; |
188 | | - |
| 303 | + cacheGadgetData( 'local', id, null ); |
189 | 304 | if ( data && data.edit && data.edit ) { |
190 | 305 | if ( data.edit.result === 'Success' ) { |
191 | 306 | o.success( data.edit ); |
— | — | @@ -199,12 +314,11 @@ |
200 | 315 | }, |
201 | 316 | error: function(){ |
202 | 317 | // Invalidate cache |
203 | | - gadgetCache[gadget.id] = null; |
| 318 | + cacheGadgetData( 'local', id, null ); |
204 | 319 | o.error( 'unknown' ); |
205 | 320 | } |
206 | 321 | }); |
207 | 322 | }, |
208 | | - |
209 | 323 | /** |
210 | 324 | * Deletes a gadget definition. |
211 | 325 | * |
— | — | @@ -215,11 +329,10 @@ |
216 | 330 | doDeleteGadget: function( id, success, error ) { |
217 | 331 | // @todo ApiDelete |
218 | 332 | // Invalidate cache |
219 | | - gadgetCache[id] = null; |
| 333 | + cacheGadgetData( 'local', id, null ); |
220 | 334 | error( '@todo' ); |
221 | 335 | return null; |
222 | 336 | } |
223 | 337 | } |
224 | 338 | }; |
225 | | - |
226 | 339 | })( jQuery ); |