1 /* mw.Api objects represent the API of a particular MediaWiki server. */
3 ( function( mw
, $j
, undefined ) {
6 * Represents the API of a particular MediaWiki server.
9 * url - complete URL to API endpoint. Usually equivalent to wgServer + wgScriptPath + '/api.php'
12 * can override the parameter defaults and ajax default options.
15 * TODO share api objects with exact same config.
17 * ajax options can also be overriden on every get() or post()
19 * @param options {Mixed} can take many options, but must include at minimum the API url.
21 mw
.Api = function( options
) {
23 if ( options
=== undefined ) {
27 // make sure we at least have a URL endpoint for the API
28 if ( options
.url
=== undefined ) {
29 options
.url
= mw
.config
.get( 'wgServer' ) + mw
.config
.get( 'wgScriptPath' ) + '/api' + mw
.config
.get( 'wgScriptExtension' );
32 this.url
= options
.url
;
34 /* We allow people to omit these default parameters from API requests */
35 // there is very customizable error handling here, on a per-call basis
36 // wondering, would it be simpler to make it easy to clone the api object, change error handling, and use that instead?
44 // force toString if we got a mw.Uri object
45 url
: new String( this.url
),
47 /* default function for success and no API error */
50 // caller can supply handlers for http transport error or api errors
51 err: function( code
, result
) {
52 mw
.log( "mw.Api error: " + code
, 'debug' );
55 timeout
: 30000, /* 30 seconds */
63 if ( options
.parameters
) {
64 $j
.extend( this.defaults
.parameters
, options
.parameters
);
68 $j
.extend( this.defaults
.ajax
, options
.ajax
);
75 * For api queries, in simple cases the caller just passes a success callback.
76 * In complex cases they pass an object with a success property as callback and probably other options.
77 * Normalize the argument so that it's always the latter case.
79 * @param {Object|Function} ajax properties, or just a success function
82 normalizeAjaxOptions: function( arg
) {
83 if ( typeof arg
=== 'function' ) {
88 throw Error( "ajax options must include ok callback" );
94 * Perform API get request
96 * @param {Object} request parameters
97 * @param {Object|Function} ajax properties, or just a success function
99 get: function( parameters
, ajaxOptions
) {
100 ajaxOptions
= this.normalizeAjaxOptions( ajaxOptions
);
101 ajaxOptions
.type
= 'GET';
102 this.ajax( parameters
, ajaxOptions
);
106 * Perform API post request
107 * TODO post actions for nonlocal will need proxy
109 * @param {Object} request parameters
110 * @param {Object|Function} ajax properties, or just a success function
112 post: function( parameters
, ajaxOptions
) {
113 ajaxOptions
= this.normalizeAjaxOptions( ajaxOptions
);
114 ajaxOptions
.type
= 'POST';
115 this.ajax( parameters
, ajaxOptions
);
119 * Perform the API call.
121 * @param {Object} request parameters
122 * @param {Object} ajax properties
124 ajax: function( parameters
, ajaxOptions
) {
125 parameters
= $j
.extend( {}, this.defaults
.parameters
, parameters
);
126 ajaxOptions
= $j
.extend( {}, this.defaults
.ajax
, ajaxOptions
);
128 // Some deployed MediaWiki >= 1.17 forbid periods in URLs, due to an IE XSS bug
129 // So let's escape them here. See bug #28235
130 // This works because jQuery accepts data as a query string or as an Object
131 ajaxOptions
.data
= $j
.param( parameters
).replace( /\./g, '%2E' );
133 ajaxOptions
.error = function( xhr
, textStatus
, exception
) {
134 ajaxOptions
.err( 'http', { xhr
: xhr
, textStatus
: textStatus
, exception
: exception
} );
138 /* success just means 200 OK; also check for output and API errors */
139 ajaxOptions
.success = function( result
) {
140 if ( result
=== undefined || result
=== null || result
=== '' ) {
141 ajaxOptions
.err( "ok-but-empty", "OK response but empty result (check HTTP headers?)" );
142 } else if ( result
.error
) {
143 var code
= result
.error
.code
=== undefined ? 'unknown' : result
.error
.code
;
144 ajaxOptions
.err( code
, result
);
146 ajaxOptions
.ok( result
);
150 $j
.ajax( ajaxOptions
);
157 * This is a list of errors we might receive from the API.
158 * For now, this just documents our expectation that there should be similar messages
162 /* occurs when POST aborted - jQuery 1.4 can't distinguish abort or lost connection from 200 OK + empty result */
168 /* really a warning, but we treat it like an error */
172 /* upload succeeded, but no image info.
173 this is probably impossible, but might as well check for it */
176 /* remote errors, defined in API */
185 'copyuploaddisabled',
193 'verification-error',
200 'fileexists-shared-forbidden'
204 * This is a list of warnings we might receive from the API.
205 * For now, this just documents our expectation that there should be similar messages
214 }) ( window
.mediaWiki
, jQuery
);