r97152 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97151‎ | r97152 | r97153 >
Date:13:51, 15 September 2011
Author:catrope
Status:ok
Tags:
Comment:
RL2: Rename ext.gadgets.gadgetmananger.api to ext.gadgets.api
Modified paths:
  • /branches/RL2/extensions/Gadgets/Gadgets.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/modules/ext.gadgets.api.js (added) (history)
  • /branches/RL2/extensions/Gadgets/modules/ext.gadgets.gadgetmanager.api.js (deleted) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/Gadgets.php
@@ -144,8 +144,8 @@
145145 'position' => 'top',
146146 ),
147147 // Method to interact with API
148 - 'ext.gadgets.gadgetmanager.api' => $gadResourceTemplate + array(
149 - 'scripts' => 'ext.gadgets.gadgetmanager.api.js',
 148+ 'ext.gadgets.api' => $gadResourceTemplate + array(
 149+ 'scripts' => 'ext.gadgets.api.js',
150150 'dependencies' => 'mediawiki.util',
151151 ),
152152 // jQuery plugin
@@ -160,7 +160,7 @@
161161 'scripts' => 'ext.gadgets.gadgetmanager.ui.js',
162162 'styles' => 'ext.gadgets.gadgetmanager.ui.css',
163163 'dependencies' => array(
164 - 'ext.gadgets.gadgetmanager.api',
 164+ 'ext.gadgets.api',
165165 'jquery.localize',
166166 'jquery.ui.autocomplete',
167167 'jquery.ui.dialog',
Index: branches/RL2/extensions/Gadgets/modules/ext.gadgets.gadgetmanager.api.js
@@ -1,225 +0,0 @@
2 -/**
3 - * Implement the editing API for the gadget manager.
4 - *
5 - * @author Timo Tijhof
6 - * @copyright © 2011 Timo Tijhof
7 - * @license GNU General Public Licence 2.0 or later
8 - */
9 -( function( $ ) {
10 -
11 - var
12 - /**
13 - * @var {Object} Keyed by gadget id, contains the metadata as an object.
14 - */
15 - gadgetCache = {},
16 - /**
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. */
19 - gadgetCategoryCache = null;
20 -
21 - /* Local functions */
22 -
23 - /**
24 - * For most returns from api.* functions, a clone is made when data from
25 - * cache is used. This is to avoid situations where later modifications
26 - * (e.g. by the AJAX editor) to the object affect the cache (because
27 - * the object would otherwise be passed by reference).
28 - */
29 - function objClone( obj ) {
30 - /**
31 - * A normal `$.extend( {}, obj );` is not suffecient,
32 - * it has to be recursive, because the values of this
33 - * object are also refererenes to objects.
34 - * Consider:
35 - * <code>
36 - * var a = { words: [ 'foo', 'bar','baz' ] };
37 - * var b = $.extend( {}, a );
38 - * b.words.push( 'quux' );
39 - * a.words[3]; // quux !
40 - * </code>
41 - */
42 - return $.extend( true /* recursive */, {}, obj );
43 - }
44 - function arrClone( arr ) {
45 - return arr.slice();
46 - }
47 -
48 - /* Public functions */
49 -
50 - mw.gadgetManager = {
51 -
52 - conf: mw.config.get( 'gadgetManagerConf' ),
53 -
54 - api: {
55 -
56 - /**
57 - * Get gadget blob from the API (or from cache if available).
58 - *
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
62 - * error, request error), this is called with error code as first argument.
63 - * @return {jqXHR|Null}: Null if served from cache, otherwise the jqXHR.
64 - */
65 - getGadgetData: function( id, success, error ) {
66 - // Check cache
67 - if ( id in gadgetCache && gadgetCache[id] !== null ) {
68 - success( objClone( gadgetCache[id] ) );
69 - return null;
70 - }
71 - // 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 - },
82 - type: 'GET',
83 - dataType: 'json',
84 - success: function( data ) {
85 - if ( data && data.query && data.query.gadgets && data.query.gadgets[0] ) {
86 - data = data.query.gadgets[0];
87 - // Update cache
88 - gadgetCache[id] = data;
89 - success( objClone( data ) );
90 - } else {
91 - // Invalidate cache
92 - gadgetCache[id] = null;
93 - if ( data && data.error ) {
94 - error( data.error.code );
95 - } else {
96 - error( 'unknown' );
97 - }
98 - }
99 - },
100 - error: function() {
101 - // Invalidate cache
102 - gadgetCache[id] = null;
103 - error( 'unknown' );
104 - }
105 - });
106 - },
107 -
108 - /**
109 - * @param callback {Function} To be called with an array as first argument.
110 - * @return {jqXHR|Null}: Null if served from cache, otherwise the jqXHR.
111 - */
112 - getGadgetCategories: function( callback ) {
113 - // Check cache
114 - if ( gadgetCategoryCache !== null ) {
115 - callback( arrClone( gadgetCategoryCache ) );
116 - return null;
117 - }
118 - // Get from API if not cached
119 - return $.ajax({
120 - url: mw.util.wikiScript( 'api' ),
121 - data: {
122 - format: 'json',
123 - action: 'query',
124 - list: 'gadgetcategories',
125 - gcprop: 'name|title|members',
126 - gclanguage: mw.config.get( 'wgUserLanguage' )
127 - },
128 - type: 'GET',
129 - dataType: 'json',
130 - success: function( data ) {
131 - if ( data && data.query && data.query.gadgetcategories
132 - && data.query.gadgetcategories[0] )
133 - {
134 - data = data.query.gadgetcategories;
135 - // Update cache
136 - gadgetCategoryCache = data;
137 - callback( arrClone( data ), 'success' );
138 - } else {
139 - // Invalidate cache
140 - gadgetCategoryCache = null;
141 - callback( [] );
142 - }
143 - },
144 - error: function() {
145 - // Invalidate cache
146 - gadgetCategoryCache = null;
147 - callback( [] );
148 - }
149 - });
150 - },
151 -
152 - /**
153 - * Creates or edits an existing gadget definition.
154 - *
155 - * @param gadget {Object}
156 - * - id {String} Id of the gadget to modify
157 - * - metadata {Object} Gadget meta data
158 - * @param o {Object} Additional options:
159 - * - starttimestamp {String} ISO_8601 timestamp of when user started editing
160 - * - success {Function} Called with one argument (API response object of the
161 - * 'edit' action)
162 - * - error {Function} Called with one argument (status from API if availabe,
163 - * otherwise, if the request failed, 'unknown' is given)
164 - * @return {jqXHR}
165 - */
166 - doModifyGadget: function( gadget, o ) {
167 - var t = new mw.Title(
168 - gadget.id + '.js',
169 - mw.config.get( 'wgNamespaceIds' ).gadget_definition
170 - );
171 - return $.ajax({
172 - url: mw.util.wikiScript( 'api' ),
173 - type: 'POST',
174 - data: {
175 - format: 'json',
176 - action: 'edit',
177 - title: t.getPrefixedDb(),
178 - text: $.toJSON( gadget.metadata ),
179 - summary: mw.msg( 'gadgetmanager-comment-modify', gadget.id ),
180 - token: mw.user.tokens.get( 'editToken' ),
181 - basetimestamp: gadget.definitiontimestamp,
182 - starttimestamp: o.starttimestamp
183 - },
184 - dataType: 'json',
185 - success: function( data ) {
186 - // Invalidate cache
187 - gadgetCache[gadget.id] = null;
188 -
189 - if ( data && data.edit && data.edit ) {
190 - if ( data.edit.result === 'Success' ) {
191 - o.success( data.edit );
192 - } else {
193 - o.error( data.edit.result );
194 - }
195 - } else if ( data && data.error ) {
196 - o.error( data.error.code );
197 - } else {
198 - o.error( 'unknown' );
199 - }
200 - },
201 - error: function(){
202 - // Invalidate cache
203 - gadgetCache[gadget.id] = null;
204 - o.error( 'unknown' );
205 - }
206 - });
207 - },
208 -
209 - /**
210 - * Deletes a gadget definition.
211 - *
212 - * @param id {String} Id of the gadget to delete.
213 - * @param callback {Function} Called with one argument (ok', 'error' or 'conflict').
214 - * @return {jqXHR}
215 - */
216 - doDeleteGadget: function( id, success, error ) {
217 - // @todo ApiDelete
218 - // Invalidate cache
219 - gadgetCache[id] = null;
220 - error( '@todo' );
221 - return null;
222 - }
223 - }
224 - };
225 -
226 -})( jQuery );
Index: branches/RL2/extensions/Gadgets/modules/ext.gadgets.api.js
@@ -0,0 +1,225 @@
 2+/**
 3+ * Implement the editing API for the gadget manager.
 4+ *
 5+ * @author Timo Tijhof
 6+ * @copyright © 2011 Timo Tijhof
 7+ * @license GNU General Public Licence 2.0 or later
 8+ */
 9+( function( $ ) {
 10+
 11+ var
 12+ /**
 13+ * @var {Object} Keyed by gadget id, contains the metadata as an object.
 14+ */
 15+ gadgetCache = {},
 16+ /**
 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. */
 19+ gadgetCategoryCache = null;
 20+
 21+ /* Local functions */
 22+
 23+ /**
 24+ * For most returns from api.* functions, a clone is made when data from
 25+ * cache is used. This is to avoid situations where later modifications
 26+ * (e.g. by the AJAX editor) to the object affect the cache (because
 27+ * the object would otherwise be passed by reference).
 28+ */
 29+ function objClone( obj ) {
 30+ /**
 31+ * A normal `$.extend( {}, obj );` is not suffecient,
 32+ * it has to be recursive, because the values of this
 33+ * object are also refererenes to objects.
 34+ * Consider:
 35+ * <code>
 36+ * var a = { words: [ 'foo', 'bar','baz' ] };
 37+ * var b = $.extend( {}, a );
 38+ * b.words.push( 'quux' );
 39+ * a.words[3]; // quux !
 40+ * </code>
 41+ */
 42+ return $.extend( true /* recursive */, {}, obj );
 43+ }
 44+ function arrClone( arr ) {
 45+ return arr.slice();
 46+ }
 47+
 48+ /* Public functions */
 49+
 50+ mw.gadgetManager = {
 51+
 52+ conf: mw.config.get( 'gadgetManagerConf' ),
 53+
 54+ api: {
 55+
 56+ /**
 57+ * Get gadget blob from the API (or from cache if available).
 58+ *
 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
 62+ * error, request error), this is called with error code as first argument.
 63+ * @return {jqXHR|Null}: Null if served from cache, otherwise the jqXHR.
 64+ */
 65+ getGadgetData: function( id, success, error ) {
 66+ // Check cache
 67+ if ( id in gadgetCache && gadgetCache[id] !== null ) {
 68+ success( objClone( gadgetCache[id] ) );
 69+ return null;
 70+ }
 71+ // 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+ },
 82+ type: 'GET',
 83+ dataType: 'json',
 84+ success: function( data ) {
 85+ if ( data && data.query && data.query.gadgets && data.query.gadgets[0] ) {
 86+ data = data.query.gadgets[0];
 87+ // Update cache
 88+ gadgetCache[id] = data;
 89+ success( objClone( data ) );
 90+ } else {
 91+ // Invalidate cache
 92+ gadgetCache[id] = null;
 93+ if ( data && data.error ) {
 94+ error( data.error.code );
 95+ } else {
 96+ error( 'unknown' );
 97+ }
 98+ }
 99+ },
 100+ error: function() {
 101+ // Invalidate cache
 102+ gadgetCache[id] = null;
 103+ error( 'unknown' );
 104+ }
 105+ });
 106+ },
 107+
 108+ /**
 109+ * @param callback {Function} To be called with an array as first argument.
 110+ * @return {jqXHR|Null}: Null if served from cache, otherwise the jqXHR.
 111+ */
 112+ getGadgetCategories: function( callback ) {
 113+ // Check cache
 114+ if ( gadgetCategoryCache !== null ) {
 115+ callback( arrClone( gadgetCategoryCache ) );
 116+ return null;
 117+ }
 118+ // Get from API if not cached
 119+ return $.ajax({
 120+ url: mw.util.wikiScript( 'api' ),
 121+ data: {
 122+ format: 'json',
 123+ action: 'query',
 124+ list: 'gadgetcategories',
 125+ gcprop: 'name|title|members',
 126+ gclanguage: mw.config.get( 'wgUserLanguage' )
 127+ },
 128+ type: 'GET',
 129+ dataType: 'json',
 130+ success: function( data ) {
 131+ if ( data && data.query && data.query.gadgetcategories
 132+ && data.query.gadgetcategories[0] )
 133+ {
 134+ data = data.query.gadgetcategories;
 135+ // Update cache
 136+ gadgetCategoryCache = data;
 137+ callback( arrClone( data ), 'success' );
 138+ } else {
 139+ // Invalidate cache
 140+ gadgetCategoryCache = null;
 141+ callback( [] );
 142+ }
 143+ },
 144+ error: function() {
 145+ // Invalidate cache
 146+ gadgetCategoryCache = null;
 147+ callback( [] );
 148+ }
 149+ });
 150+ },
 151+
 152+ /**
 153+ * Creates or edits an existing gadget definition.
 154+ *
 155+ * @param gadget {Object}
 156+ * - id {String} Id of the gadget to modify
 157+ * - metadata {Object} Gadget meta data
 158+ * @param o {Object} Additional options:
 159+ * - starttimestamp {String} ISO_8601 timestamp of when user started editing
 160+ * - success {Function} Called with one argument (API response object of the
 161+ * 'edit' action)
 162+ * - error {Function} Called with one argument (status from API if availabe,
 163+ * otherwise, if the request failed, 'unknown' is given)
 164+ * @return {jqXHR}
 165+ */
 166+ doModifyGadget: function( gadget, o ) {
 167+ var t = new mw.Title(
 168+ gadget.id + '.js',
 169+ mw.config.get( 'wgNamespaceIds' ).gadget_definition
 170+ );
 171+ return $.ajax({
 172+ url: mw.util.wikiScript( 'api' ),
 173+ type: 'POST',
 174+ data: {
 175+ format: 'json',
 176+ action: 'edit',
 177+ title: t.getPrefixedDb(),
 178+ text: $.toJSON( gadget.metadata ),
 179+ summary: mw.msg( 'gadgetmanager-comment-modify', gadget.id ),
 180+ token: mw.user.tokens.get( 'editToken' ),
 181+ basetimestamp: gadget.definitiontimestamp,
 182+ starttimestamp: o.starttimestamp
 183+ },
 184+ dataType: 'json',
 185+ success: function( data ) {
 186+ // Invalidate cache
 187+ gadgetCache[gadget.id] = null;
 188+
 189+ if ( data && data.edit && data.edit ) {
 190+ if ( data.edit.result === 'Success' ) {
 191+ o.success( data.edit );
 192+ } else {
 193+ o.error( data.edit.result );
 194+ }
 195+ } else if ( data && data.error ) {
 196+ o.error( data.error.code );
 197+ } else {
 198+ o.error( 'unknown' );
 199+ }
 200+ },
 201+ error: function(){
 202+ // Invalidate cache
 203+ gadgetCache[gadget.id] = null;
 204+ o.error( 'unknown' );
 205+ }
 206+ });
 207+ },
 208+
 209+ /**
 210+ * Deletes a gadget definition.
 211+ *
 212+ * @param id {String} Id of the gadget to delete.
 213+ * @param callback {Function} Called with one argument (ok', 'error' or 'conflict').
 214+ * @return {jqXHR}
 215+ */
 216+ doDeleteGadget: function( id, success, error ) {
 217+ // @todo ApiDelete
 218+ // Invalidate cache
 219+ gadgetCache[id] = null;
 220+ error( '@todo' );
 221+ return null;
 222+ }
 223+ }
 224+ };
 225+
 226+})( jQuery );

Status & tagging log