r110759 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110758‎ | r110759 | r110760 >
Date:16:58, 6 February 2012
Author:reedy
Status:resolved
Tags:
Comment:
Move mediawiki.api to own folder
Modified paths:
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/mediawiki.api (added) (history)
  • /trunk/phase3/resources/mediawiki.api/mediawiki.api.category.js (added) (history)
  • /trunk/phase3/resources/mediawiki.api/mediawiki.api.edit.js (added) (history)
  • /trunk/phase3/resources/mediawiki.api/mediawiki.api.js (added) (history)
  • /trunk/phase3/resources/mediawiki.api/mediawiki.api.parse.js (added) (history)
  • /trunk/phase3/resources/mediawiki.api/mediawiki.api.titleblacklist.js (added) (history)
  • /trunk/phase3/resources/mediawiki.api/mediawiki.api.watch.js (added) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.api.category.js (deleted) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.api.edit.js (deleted) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.api.js (deleted) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.api.parse.js (deleted) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.api.titleblacklist.js (deleted) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.api.watch.js (deleted) (history)

Diff [purge]

Index: trunk/phase3/resources/Resources.php
@@ -506,36 +506,36 @@
507507 'debugRaw' => false,
508508 ),
509509 'mediawiki.api' => array(
510 - 'scripts' => 'resources/mediawiki/mediawiki.api.js',
 510+ 'scripts' => 'resources/mediawiki/mediawiki.api/mediawiki.api.js',
511511 'dependencies' => 'mediawiki.util',
512512 ),
513513 'mediawiki.api.category' => array(
514 - 'scripts' => 'resources/mediawiki/mediawiki.api.category.js',
 514+ 'scripts' => 'resources/mediawiki/mediawiki.api/mediawiki.api.category.js',
515515 'dependencies' => array(
516516 'mediawiki.api',
517517 'mediawiki.Title'
518518 ),
519519 ),
520520 'mediawiki.api.edit' => array(
521 - 'scripts' => 'resources/mediawiki/mediawiki.api.edit.js',
 521+ 'scripts' => 'resources/mediawiki/mediawiki.api/mediawiki.api.edit.js',
522522 'dependencies' => array(
523523 'mediawiki.api',
524524 'mediawiki.Title'
525525 ),
526526 ),
527527 'mediawiki.api.parse' => array(
528 - 'scripts' => 'resources/mediawiki/mediawiki.api.parse.js',
 528+ 'scripts' => 'resources/mediawiki/mediawiki.api/mediawiki.api.parse.js',
529529 'dependencies' => 'mediawiki.api',
530530 ),
531531 'mediawiki.api.titleblacklist' => array(
532 - 'scripts' => 'resources/mediawiki/mediawiki.api.titleblacklist.js',
 532+ 'scripts' => 'resources/mediawiki/mediawiki.api/mediawiki.api.titleblacklist.js',
533533 'dependencies' => array(
534534 'mediawiki.api',
535535 'mediawiki.Title'
536536 ),
537537 ),
538538 'mediawiki.api.watch' => array(
539 - 'scripts' => 'resources/mediawiki/mediawiki.api.watch.js',
 539+ 'scripts' => 'resources/mediawiki/mediawiki.api/mediawiki.api.watch.js',
540540 'dependencies' => 'mediawiki.api',
541541 ),
542542 'mediawiki.debug' => array(
Index: trunk/phase3/resources/mediawiki/mediawiki.api.edit.js
@@ -1,119 +0,0 @@
2 -/**
3 - * Additional mw.Api methods to assist with API calls related to editing wiki pages.
4 - */
5 -
6 -( function( $, mw, undefined ) {
7 -
8 - // Cache token so we don't have to keep fetching new ones for every single request.
9 - var cachedToken = null;
10 -
11 - $.extend( mw.Api.prototype, {
12 -
13 - /**
14 - * Post to API with edit token. If we have no token, get one and try to post.
15 - * If we have a cached token try using that, and if it fails, blank out the
16 - * cached token and start over.
17 - *
18 - * @param params {Object} API parameters
19 - * @param ok {Function} callback for success
20 - * @param err {Function} [optional] error callback
21 - * @return {jqXHR}
22 - */
23 - postWithEditToken: function( params, ok, err ) {
24 - var api = this, useTokenToPost, getTokenIfBad;
25 - if ( cachedToken === null ) {
26 - // We don't have a valid cached token, so get a fresh one and try posting.
27 - // We do not trap any 'badtoken' or 'notoken' errors, because we don't want
28 - // an infinite loop. If this fresh token is bad, something else is very wrong.
29 - useTokenToPost = function( token ) {
30 - params.token = token;
31 - api.post( params, ok, err );
32 - };
33 - return api.getEditToken( useTokenToPost, err );
34 - } else {
35 - // We do have a token, but it might be expired. So if it is 'bad' then
36 - // start over with a new token.
37 - params.token = cachedToken;
38 - getTokenIfBad = function( code, result ) {
39 - if ( code === 'badtoken' ) {
40 - cachedToken = null; // force a new token
41 - api.postWithEditToken( params, ok, err );
42 - } else {
43 - err( code, result );
44 - }
45 - };
46 - return api.post( params, { ok : ok, err : getTokenIfBad });
47 - }
48 - },
49 -
50 - /**
51 - * Api helper to grab an edit token
52 - *
53 - * token callback has signature ( String token )
54 - * error callback has signature ( String code, Object results, XmlHttpRequest xhr, Exception exception )
55 - * Note that xhr and exception are only available for 'http_*' errors
56 - * code may be any http_* error code (see mw.Api), or 'token_missing'
57 - *
58 - * @param tokenCallback {Function} received token callback
59 - * @param err {Function} error callback
60 - * @return {jqXHR}
61 - */
62 - getEditToken: function( tokenCallback, err ) {
63 - var parameters = {
64 - prop: 'info',
65 - intoken: 'edit',
66 - // we need some kind of dummy page to get a token from. This will return a response
67 - // complaining that the page is missing, but we should also get an edit token
68 - titles: 'DummyPageForEditToken'
69 - },
70 - ok = function( data ) {
71 - var token;
72 - $.each( data.query.pages, function( i, page ) {
73 - if ( page.edittoken ) {
74 - token = page.edittoken;
75 - return false;
76 - }
77 - } );
78 - if ( token !== undefined ) {
79 - cachedToken = token;
80 - tokenCallback( token );
81 - } else {
82 - err( 'token-missing', data );
83 - }
84 - },
85 - ajaxOptions = {
86 - ok: ok,
87 - err: err,
88 - // Due to the API assuming we're logged out if we pass the callback-parameter,
89 - // we have to disable jQuery's callback system, and instead parse JSON string,
90 - // by setting 'jsonp' to false.
91 - jsonp: false
92 - };
93 -
94 - return this.get( parameters, ajaxOptions );
95 - },
96 -
97 - /**
98 - * Create a new section of the page.
99 - * @param title {mw.Title|String} target page
100 - * @param header {String}
101 - * @param message {String} wikitext message
102 - * @param ok {Function} success handler
103 - * @param err {Function} error handler
104 - * @return {jqXHR}
105 - */
106 - newSection: function( title, header, message, ok, err ) {
107 - var params = {
108 - action: 'edit',
109 - section: 'new',
110 - format: 'json',
111 - title: title.toString(),
112 - summary: header,
113 - text: message
114 - };
115 - return this.postWithEditToken( params, ok, err );
116 - }
117 -
118 - } );
119 -
120 -} )( jQuery, mediaWiki );
Index: trunk/phase3/resources/mediawiki/mediawiki.api.js
@@ -1,224 +0,0 @@
2 -/* mw.Api objects represent the API of a particular MediaWiki server. */
3 -
4 -( function( $, mw, undefined ) {
5 -
6 - /**
7 - * @var defaultOptions {Object}
8 - * We allow people to omit these default parameters from API requests
9 - * there is very customizable error handling here, on a per-call basis
10 - * wondering, would it be simpler to make it easy to clone the api object,
11 - * change error handling, and use that instead?
12 - */
13 - var defaultOptions = {
14 -
15 - // Query parameters for API requests
16 - parameters: {
17 - action: 'query',
18 - format: 'json'
19 - },
20 -
21 - // Ajax options for jQuery.ajax()
22 - ajax: {
23 - url: mw.util.wikiScript( 'api' ),
24 -
25 - ok: function() {},
26 -
27 - // caller can supply handlers for http transport error or api errors
28 - err: function( code, result ) {
29 - mw.log( 'mw.Api error: ' + code, 'debug' );
30 - },
31 -
32 - timeout: 30000, // 30 seconds
33 -
34 - dataType: 'json'
35 - }
36 - };
37 -
38 - /**
39 - * Constructor to create an object to interact with the API of a particular MediaWiki server.
40 - *
41 - * @todo Share API objects with exact same config.
42 - * @example
43 - * <code>
44 - * var api = new mw.Api();
45 - * api.get( {
46 - * action: 'query',
47 - * meta: 'userinfo'
48 - * }, {
49 - * ok: function () { console.log( arguments ); }
50 - * } );
51 - * </code>
52 - *
53 - * @constructor
54 - * @param options {Object} See defaultOptions documentation above. Ajax options can also be
55 - * overridden for each individual request to jQuery.ajax() later on.
56 - */
57 - mw.Api = function( options ) {
58 -
59 - if ( options === undefined ) {
60 - options = {};
61 - }
62 -
63 - // Force toString if we got a mw.Uri object
64 - if ( options.ajax && options.ajax.url !== undefined ) {
65 - options.ajax.url = String( options.ajax.url );
66 - }
67 -
68 - options.parameters = $.extend( {}, defaultOptions.parameters, options.parameters );
69 - options.ajax = $.extend( {}, defaultOptions.ajax, options.ajax );
70 -
71 - this.defaults = options;
72 - };
73 -
74 - mw.Api.prototype = {
75 -
76 - /**
77 - * For api queries, in simple cases the caller just passes a success callback.
78 - * In complex cases they pass an object with a success property as callback and
79 - * probably other options.
80 - * Normalize the argument so that it's always the latter case.
81 - *
82 - * @param {Object|Function} An object contaning one or more of options.ajax,
83 - * or just a success function (options.ajax.ok).
84 - * @return {Object} Normalized ajax options.
85 - */
86 - normalizeAjaxOptions: function( arg ) {
87 - var opt = arg;
88 - if ( typeof arg === 'function' ) {
89 - opt = { 'ok': arg };
90 - }
91 - if ( !opt.ok ) {
92 - throw new Error( 'ajax options must include ok callback' );
93 - }
94 - return opt;
95 - },
96 -
97 - /**
98 - * Perform API get request
99 - *
100 - * @param {Object} request parameters
101 - * @param {Object|Function} ajax options, or just a success function
102 - * @return {jqXHR}
103 - */
104 - get: function( parameters, ajaxOptions ) {
105 - ajaxOptions = this.normalizeAjaxOptions( ajaxOptions );
106 - ajaxOptions.type = 'GET';
107 - return this.ajax( parameters, ajaxOptions );
108 - },
109 -
110 - /**
111 - * Perform API post request
112 - * @todo Post actions for nonlocal will need proxy
113 - *
114 - * @param {Object} request parameters
115 - * @param {Object|Function} ajax options, or just a success function
116 - * @return {jqXHR}
117 - */
118 - post: function( parameters, ajaxOptions ) {
119 - ajaxOptions = this.normalizeAjaxOptions( ajaxOptions );
120 - ajaxOptions.type = 'POST';
121 - return this.ajax( parameters, ajaxOptions );
122 - },
123 -
124 - /**
125 - * Perform the API call.
126 - *
127 - * @param {Object} request parameters
128 - * @param {Object} ajax options
129 - * @return {jqXHR}
130 - */
131 - ajax: function( parameters, ajaxOptions ) {
132 - parameters = $.extend( {}, this.defaults.parameters, parameters );
133 - ajaxOptions = $.extend( {}, this.defaults.ajax, ajaxOptions );
134 -
135 - // Some deployed MediaWiki >= 1.17 forbid periods in URLs, due to an IE XSS bug
136 - // So let's escape them here. See bug #28235
137 - // This works because jQuery accepts data as a query string or as an Object
138 - ajaxOptions.data = $.param( parameters ).replace( /\./g, '%2E' );
139 -
140 - ajaxOptions.error = function( xhr, textStatus, exception ) {
141 - ajaxOptions.err( 'http', {
142 - xhr: xhr,
143 - textStatus: textStatus,
144 - exception: exception
145 - } );
146 - };
147 -
148 - // Success just means 200 OK; also check for output and API errors
149 - ajaxOptions.success = function( result ) {
150 - if ( result === undefined || result === null || result === '' ) {
151 - ajaxOptions.err( 'ok-but-empty',
152 - 'OK response but empty result (check HTTP headers?)' );
153 - } else if ( result.error ) {
154 - var code = result.error.code === undefined ? 'unknown' : result.error.code;
155 - ajaxOptions.err( code, result );
156 - } else {
157 - ajaxOptions.ok( result );
158 - }
159 - };
160 -
161 - return $.ajax( ajaxOptions );
162 - }
163 -
164 - };
165 -
166 - /**
167 - * @var {Array} List of errors we might receive from the API.
168 - * For now, this just documents our expectation that there should be similar messages
169 - * available.
170 - */
171 - mw.Api.errors = [
172 - // occurs when POST aborted
173 - // jQuery 1.4 can't distinguish abort or lost connection from 200 OK + empty result
174 - 'ok-but-empty',
175 -
176 - // timeout
177 - 'timeout',
178 -
179 - // really a warning, but we treat it like an error
180 - 'duplicate',
181 - 'duplicate-archive',
182 -
183 - // upload succeeded, but no image info.
184 - // this is probably impossible, but might as well check for it
185 - 'noimageinfo',
186 - // remote errors, defined in API
187 - 'uploaddisabled',
188 - 'nomodule',
189 - 'mustbeposted',
190 - 'badaccess-groups',
191 - 'stashfailed',
192 - 'missingresult',
193 - 'missingparam',
194 - 'invalid-file-key',
195 - 'copyuploaddisabled',
196 - 'mustbeloggedin',
197 - 'empty-file',
198 - 'file-too-large',
199 - 'filetype-missing',
200 - 'filetype-banned',
201 - 'filename-tooshort',
202 - 'illegal-filename',
203 - 'verification-error',
204 - 'hookaborted',
205 - 'unknown-error',
206 - 'internal-error',
207 - 'overwrite',
208 - 'badtoken',
209 - 'fetchfileerror',
210 - 'fileexists-shared-forbidden',
211 - 'invalidtitle',
212 - 'notloggedin'
213 - ];
214 -
215 - /**
216 - * @var {Array} List of warnings we might receive from the API.
217 - * For now, this just documents our expectation that there should be similar messages
218 - * available.
219 - */
220 - mw.Api.warnings = [
221 - 'duplicate',
222 - 'exists'
223 - ];
224 -
225 -})( jQuery, mediaWiki );
Index: trunk/phase3/resources/mediawiki/mediawiki.api.watch.js
@@ -1,58 +0,0 @@
2 -/**
3 - * Additional mw.Api methods to assist with (un)watching wiki pages.
4 - * @since 1.19
5 - */
6 -( function( $, mw ) {
7 -
8 - $.extend( mw.Api.prototype, {
9 - /**
10 - * Convinience method for 'action=watch'.
11 - *
12 - * @param page {String|mw.Title} Full page name or instance of mw.Title
13 - * @param success {Function} callback to which the watch object will be passed
14 - * watch object contains 'title' (full page name), 'watched' (boolean) and
15 - * 'message' (parsed HTML of the 'addedwatchtext' message).
16 - * @param err {Function} callback if error (optional)
17 - * @return {jqXHR}
18 - */
19 - watch: function( page, success, err ) {
20 - var params, ok;
21 - params = {
22 - action: 'watch',
23 - title: String( page ),
24 - token: mw.user.tokens.get( 'watchToken' ),
25 - uselang: mw.config.get( 'wgUserLanguage' )
26 - };
27 - ok = function( data ) {
28 - success( data.watch );
29 - };
30 - return this.post( params, { ok: ok, err: err } );
31 - },
32 - /**
33 - * Convinience method for 'action=watch&unwatch='.
34 - *
35 - * @param page {String|mw.Title} Full page name or instance of mw.Title
36 - * @param success {Function} callback to which the watch object will be passed
37 - * watch object contains 'title' (full page name), 'unwatched' (boolean) and
38 - * 'message' (parsed HTML of the 'removedwatchtext' message).
39 - * @param err {Function} callback if error (optional)
40 - * @return {jqXHR}
41 - */
42 - unwatch: function( page, success, err ) {
43 - var params, ok;
44 - params = {
45 - action: 'watch',
46 - unwatch: 1,
47 - title: String( page ),
48 - token: mw.user.tokens.get( 'watchToken' ),
49 - uselang: mw.config.get( 'wgUserLanguage' )
50 - };
51 - ok = function( data ) {
52 - success( data.watch );
53 - };
54 - return this.post( params, { ok: ok, err: err } );
55 - }
56 -
57 - } );
58 -
59 -} )( jQuery, mediaWiki );
Index: trunk/phase3/resources/mediawiki/mediawiki.api.parse.js
@@ -1,31 +0,0 @@
2 -/**
3 - * Additional mw.Api methods to assist with API calls related to parsing wikitext.
4 - */
5 -
6 -( function( $, mw ) {
7 -
8 - $.extend( mw.Api.prototype, {
9 - /**
10 - * Convinience method for 'action=parse'. Parses wikitext into HTML.
11 - *
12 - * @param wikiText {String}
13 - * @param success {Function} callback to which to pass success HTML
14 - * @param err {Function} callback if error (optional)
15 - * @return {jqXHR}
16 - */
17 - parse: function( wikiText, success, err ) {
18 - var params = {
19 - text: wikiText,
20 - action: 'parse'
21 - },
22 - ok = function( data ) {
23 - if ( data.parse && data.parse.text && data.parse.text['*'] ) {
24 - success( data.parse.text['*'] );
25 - }
26 - };
27 - return this.get( params, { ok: ok, err: err } );
28 - }
29 -
30 - } );
31 -
32 -} )( jQuery, mediaWiki );
Index: trunk/phase3/resources/mediawiki/mediawiki.api.titleblacklist.js
@@ -1,51 +0,0 @@
2 -/**
3 - * Additional mw.Api methods to assist with API calls to the API module of the TitleBlacklist extension.
4 - */
5 -
6 -( function( $, mw, undefined ) {
7 -
8 - $.extend( mw.Api.prototype, {
9 - /**
10 - * Convinience method for 'action=titleblacklist'.
11 - * Note: This action is not provided by MediaWiki core, but as part of the TitleBlacklist extension.
12 - *
13 - * @param title {mw.Title}
14 - * @param success {Function} Called on successfull request. First argument is false if title wasn't blacklisted,
15 - * object with 'reason', 'line' and 'message' properties if title was blacklisted.
16 - * @param err {Function} optional callback to run if api error
17 - * @return {jqXHR}
18 - */
19 - isBlacklisted: function( title, success, err ) {
20 - var params = {
21 - action: 'titleblacklist',
22 - tbaction: 'create',
23 - tbtitle: title.toString()
24 - },
25 - ok = function( data ) {
26 - var result;
27 -
28 - // this fails open (if nothing valid is returned by the api, allows the title)
29 - // also fails open when the API is not present, which will be most of the time
30 - // as this API module is part of the TitleBlacklist extension.
31 - if ( data.titleblacklist && data.titleblacklist.result && data.titleblacklist.result === 'blacklisted') {
32 - if ( data.titleblacklist.reason ) {
33 - result = {
34 - reason: data.titleblacklist.reason,
35 - line: data.titleblacklist.line,
36 - message: data.titleblacklist.message
37 - };
38 - } else {
39 - mw.log('mw.Api.titleblacklist::isBlacklisted> no reason data for blacklisted title', 'debug');
40 - result = { reason: 'Blacklisted, but no reason supplied', line: 'Unknown', message: null };
41 - }
42 - success( result );
43 - } else {
44 - success ( false );
45 - }
46 - };
47 -
48 - return this.get( params, { ok: ok, err: err } );
49 - }
50 -
51 - } );
52 -} )( jQuery, mediaWiki );
Index: trunk/phase3/resources/mediawiki/mediawiki.api.category.js
@@ -1,105 +0,0 @@
2 -/**
3 - * Additional mw.Api methods to assist with API calls related to categories.
4 - */
5 -
6 -( function( $, mw, undefined ) {
7 -
8 - $.extend( mw.Api.prototype, {
9 - /**
10 - * Determine if a category exists.
11 - * @param title {mw.Title}
12 - * @param success {Function} callback to pass boolean of category's existence
13 - * @param err {Function} optional callback to run if api error
14 - * @return ajax call object
15 - */
16 - isCategory: function( title, success, err ) {
17 - var params = {
18 - prop: 'categoryinfo',
19 - titles: title.toString()
20 - },
21 - ok = function( data ) {
22 - var exists = false;
23 - if ( data.query && data.query.pages ) {
24 - $.each( data.query.pages, function( id, page ) {
25 - if ( page.categoryinfo ) {
26 - exists = true;
27 - }
28 - } );
29 - }
30 - success( exists );
31 - };
32 -
33 - return this.get( params, { ok: ok, err: err } );
34 - },
35 -
36 - /**
37 - * Get a list of categories that match a certain prefix.
38 - * e.g. given "Foo", return "Food", "Foolish people", "Foosball tables" ...
39 - * @param prefix {String} prefix to match
40 - * @param success {Function} callback to pass matched categories to
41 - * @param err {Function} optional callback to run if api error
42 - * @return {jqXHR}
43 - */
44 - getCategoriesByPrefix: function( prefix, success, err ) {
45 -
46 - // fetch with allpages to only get categories that have a corresponding description page.
47 - var params = {
48 - 'list': 'allpages',
49 - 'apprefix': prefix,
50 - 'apnamespace': mw.config.get('wgNamespaceIds').category
51 - };
52 -
53 - var ok = function( data ) {
54 - var texts = [];
55 - if ( data.query && data.query.allpages ) {
56 - $.each( data.query.allpages, function( i, category ) {
57 - texts.push( new mw.Title( category.title ).getNameText() );
58 - } );
59 - }
60 - success( texts );
61 - };
62 -
63 - return this.get( params, { ok: ok, err: err } );
64 - },
65 -
66 -
67 - /**
68 - * Get the categories that a particular page on the wiki belongs to
69 - * @param title {mw.Title}
70 - * @param success {Function} callback to pass categories to (or false, if title not found)
71 - * @param err {Function} optional callback to run if api error
72 - * @param async {Boolean} optional asynchronousness (default = true = async)
73 - * @return {jqXHR}
74 - */
75 - getCategories: function( title, success, err, async ) {
76 - var params, ok;
77 - params = {
78 - prop: 'categories',
79 - titles: title.toString()
80 - };
81 - if ( async === undefined ) {
82 - async = true;
83 - }
84 - ok = function( data ) {
85 - var ret = false;
86 - if ( data.query && data.query.pages ) {
87 - $.each( data.query.pages, function( id, page ) {
88 - if ( page.categories ) {
89 - if ( typeof ret !== 'object' ) {
90 - ret = [];
91 - }
92 - $.each( page.categories, function( i, cat ) {
93 - ret.push( new mw.Title( cat.title ) );
94 - } );
95 - }
96 - } );
97 - }
98 - success( ret );
99 - };
100 -
101 - return this.get( params, { ok: ok, err: err, async: async } );
102 - }
103 -
104 - } );
105 -
106 -} )( jQuery, mediaWiki );
Index: trunk/phase3/resources/mediawiki.api/mediawiki.api.edit.js
@@ -0,0 +1,119 @@
 2+/**
 3+ * Additional mw.Api methods to assist with API calls related to editing wiki pages.
 4+ */
 5+
 6+( function( $, mw, undefined ) {
 7+
 8+ // Cache token so we don't have to keep fetching new ones for every single request.
 9+ var cachedToken = null;
 10+
 11+ $.extend( mw.Api.prototype, {
 12+
 13+ /**
 14+ * Post to API with edit token. If we have no token, get one and try to post.
 15+ * If we have a cached token try using that, and if it fails, blank out the
 16+ * cached token and start over.
 17+ *
 18+ * @param params {Object} API parameters
 19+ * @param ok {Function} callback for success
 20+ * @param err {Function} [optional] error callback
 21+ * @return {jqXHR}
 22+ */
 23+ postWithEditToken: function( params, ok, err ) {
 24+ var api = this, useTokenToPost, getTokenIfBad;
 25+ if ( cachedToken === null ) {
 26+ // We don't have a valid cached token, so get a fresh one and try posting.
 27+ // We do not trap any 'badtoken' or 'notoken' errors, because we don't want
 28+ // an infinite loop. If this fresh token is bad, something else is very wrong.
 29+ useTokenToPost = function( token ) {
 30+ params.token = token;
 31+ api.post( params, ok, err );
 32+ };
 33+ return api.getEditToken( useTokenToPost, err );
 34+ } else {
 35+ // We do have a token, but it might be expired. So if it is 'bad' then
 36+ // start over with a new token.
 37+ params.token = cachedToken;
 38+ getTokenIfBad = function( code, result ) {
 39+ if ( code === 'badtoken' ) {
 40+ cachedToken = null; // force a new token
 41+ api.postWithEditToken( params, ok, err );
 42+ } else {
 43+ err( code, result );
 44+ }
 45+ };
 46+ return api.post( params, { ok : ok, err : getTokenIfBad });
 47+ }
 48+ },
 49+
 50+ /**
 51+ * Api helper to grab an edit token
 52+ *
 53+ * token callback has signature ( String token )
 54+ * error callback has signature ( String code, Object results, XmlHttpRequest xhr, Exception exception )
 55+ * Note that xhr and exception are only available for 'http_*' errors
 56+ * code may be any http_* error code (see mw.Api), or 'token_missing'
 57+ *
 58+ * @param tokenCallback {Function} received token callback
 59+ * @param err {Function} error callback
 60+ * @return {jqXHR}
 61+ */
 62+ getEditToken: function( tokenCallback, err ) {
 63+ var parameters = {
 64+ prop: 'info',
 65+ intoken: 'edit',
 66+ // we need some kind of dummy page to get a token from. This will return a response
 67+ // complaining that the page is missing, but we should also get an edit token
 68+ titles: 'DummyPageForEditToken'
 69+ },
 70+ ok = function( data ) {
 71+ var token;
 72+ $.each( data.query.pages, function( i, page ) {
 73+ if ( page.edittoken ) {
 74+ token = page.edittoken;
 75+ return false;
 76+ }
 77+ } );
 78+ if ( token !== undefined ) {
 79+ cachedToken = token;
 80+ tokenCallback( token );
 81+ } else {
 82+ err( 'token-missing', data );
 83+ }
 84+ },
 85+ ajaxOptions = {
 86+ ok: ok,
 87+ err: err,
 88+ // Due to the API assuming we're logged out if we pass the callback-parameter,
 89+ // we have to disable jQuery's callback system, and instead parse JSON string,
 90+ // by setting 'jsonp' to false.
 91+ jsonp: false
 92+ };
 93+
 94+ return this.get( parameters, ajaxOptions );
 95+ },
 96+
 97+ /**
 98+ * Create a new section of the page.
 99+ * @param title {mw.Title|String} target page
 100+ * @param header {String}
 101+ * @param message {String} wikitext message
 102+ * @param ok {Function} success handler
 103+ * @param err {Function} error handler
 104+ * @return {jqXHR}
 105+ */
 106+ newSection: function( title, header, message, ok, err ) {
 107+ var params = {
 108+ action: 'edit',
 109+ section: 'new',
 110+ format: 'json',
 111+ title: title.toString(),
 112+ summary: header,
 113+ text: message
 114+ };
 115+ return this.postWithEditToken( params, ok, err );
 116+ }
 117+
 118+ } );
 119+
 120+} )( jQuery, mediaWiki );
Property changes on: trunk/phase3/resources/mediawiki.api/mediawiki.api.edit.js
___________________________________________________________________
Added: svn:eol-style
1121 + native
Index: trunk/phase3/resources/mediawiki.api/mediawiki.api.watch.js
@@ -0,0 +1,58 @@
 2+/**
 3+ * Additional mw.Api methods to assist with (un)watching wiki pages.
 4+ * @since 1.19
 5+ */
 6+( function( $, mw ) {
 7+
 8+ $.extend( mw.Api.prototype, {
 9+ /**
 10+ * Convinience method for 'action=watch'.
 11+ *
 12+ * @param page {String|mw.Title} Full page name or instance of mw.Title
 13+ * @param success {Function} callback to which the watch object will be passed
 14+ * watch object contains 'title' (full page name), 'watched' (boolean) and
 15+ * 'message' (parsed HTML of the 'addedwatchtext' message).
 16+ * @param err {Function} callback if error (optional)
 17+ * @return {jqXHR}
 18+ */
 19+ watch: function( page, success, err ) {
 20+ var params, ok;
 21+ params = {
 22+ action: 'watch',
 23+ title: String( page ),
 24+ token: mw.user.tokens.get( 'watchToken' ),
 25+ uselang: mw.config.get( 'wgUserLanguage' )
 26+ };
 27+ ok = function( data ) {
 28+ success( data.watch );
 29+ };
 30+ return this.post( params, { ok: ok, err: err } );
 31+ },
 32+ /**
 33+ * Convinience method for 'action=watch&unwatch='.
 34+ *
 35+ * @param page {String|mw.Title} Full page name or instance of mw.Title
 36+ * @param success {Function} callback to which the watch object will be passed
 37+ * watch object contains 'title' (full page name), 'unwatched' (boolean) and
 38+ * 'message' (parsed HTML of the 'removedwatchtext' message).
 39+ * @param err {Function} callback if error (optional)
 40+ * @return {jqXHR}
 41+ */
 42+ unwatch: function( page, success, err ) {
 43+ var params, ok;
 44+ params = {
 45+ action: 'watch',
 46+ unwatch: 1,
 47+ title: String( page ),
 48+ token: mw.user.tokens.get( 'watchToken' ),
 49+ uselang: mw.config.get( 'wgUserLanguage' )
 50+ };
 51+ ok = function( data ) {
 52+ success( data.watch );
 53+ };
 54+ return this.post( params, { ok: ok, err: err } );
 55+ }
 56+
 57+ } );
 58+
 59+} )( jQuery, mediaWiki );
Property changes on: trunk/phase3/resources/mediawiki.api/mediawiki.api.watch.js
___________________________________________________________________
Added: svn:eol-style
160 + native
Index: trunk/phase3/resources/mediawiki.api/mediawiki.api.parse.js
@@ -0,0 +1,31 @@
 2+/**
 3+ * Additional mw.Api methods to assist with API calls related to parsing wikitext.
 4+ */
 5+
 6+( function( $, mw ) {
 7+
 8+ $.extend( mw.Api.prototype, {
 9+ /**
 10+ * Convinience method for 'action=parse'. Parses wikitext into HTML.
 11+ *
 12+ * @param wikiText {String}
 13+ * @param success {Function} callback to which to pass success HTML
 14+ * @param err {Function} callback if error (optional)
 15+ * @return {jqXHR}
 16+ */
 17+ parse: function( wikiText, success, err ) {
 18+ var params = {
 19+ text: wikiText,
 20+ action: 'parse'
 21+ },
 22+ ok = function( data ) {
 23+ if ( data.parse && data.parse.text && data.parse.text['*'] ) {
 24+ success( data.parse.text['*'] );
 25+ }
 26+ };
 27+ return this.get( params, { ok: ok, err: err } );
 28+ }
 29+
 30+ } );
 31+
 32+} )( jQuery, mediaWiki );
Property changes on: trunk/phase3/resources/mediawiki.api/mediawiki.api.parse.js
___________________________________________________________________
Added: svn:eol-style
133 + native
Index: trunk/phase3/resources/mediawiki.api/mediawiki.api.titleblacklist.js
@@ -0,0 +1,51 @@
 2+/**
 3+ * Additional mw.Api methods to assist with API calls to the API module of the TitleBlacklist extension.
 4+ */
 5+
 6+( function( $, mw, undefined ) {
 7+
 8+ $.extend( mw.Api.prototype, {
 9+ /**
 10+ * Convinience method for 'action=titleblacklist'.
 11+ * Note: This action is not provided by MediaWiki core, but as part of the TitleBlacklist extension.
 12+ *
 13+ * @param title {mw.Title}
 14+ * @param success {Function} Called on successfull request. First argument is false if title wasn't blacklisted,
 15+ * object with 'reason', 'line' and 'message' properties if title was blacklisted.
 16+ * @param err {Function} optional callback to run if api error
 17+ * @return {jqXHR}
 18+ */
 19+ isBlacklisted: function( title, success, err ) {
 20+ var params = {
 21+ action: 'titleblacklist',
 22+ tbaction: 'create',
 23+ tbtitle: title.toString()
 24+ },
 25+ ok = function( data ) {
 26+ var result;
 27+
 28+ // this fails open (if nothing valid is returned by the api, allows the title)
 29+ // also fails open when the API is not present, which will be most of the time
 30+ // as this API module is part of the TitleBlacklist extension.
 31+ if ( data.titleblacklist && data.titleblacklist.result && data.titleblacklist.result === 'blacklisted') {
 32+ if ( data.titleblacklist.reason ) {
 33+ result = {
 34+ reason: data.titleblacklist.reason,
 35+ line: data.titleblacklist.line,
 36+ message: data.titleblacklist.message
 37+ };
 38+ } else {
 39+ mw.log('mw.Api.titleblacklist::isBlacklisted> no reason data for blacklisted title', 'debug');
 40+ result = { reason: 'Blacklisted, but no reason supplied', line: 'Unknown', message: null };
 41+ }
 42+ success( result );
 43+ } else {
 44+ success ( false );
 45+ }
 46+ };
 47+
 48+ return this.get( params, { ok: ok, err: err } );
 49+ }
 50+
 51+ } );
 52+} )( jQuery, mediaWiki );
Property changes on: trunk/phase3/resources/mediawiki.api/mediawiki.api.titleblacklist.js
___________________________________________________________________
Added: svn:eol-style
153 + native
Index: trunk/phase3/resources/mediawiki.api/mediawiki.api.category.js
@@ -0,0 +1,105 @@
 2+/**
 3+ * Additional mw.Api methods to assist with API calls related to categories.
 4+ */
 5+
 6+( function( $, mw, undefined ) {
 7+
 8+ $.extend( mw.Api.prototype, {
 9+ /**
 10+ * Determine if a category exists.
 11+ * @param title {mw.Title}
 12+ * @param success {Function} callback to pass boolean of category's existence
 13+ * @param err {Function} optional callback to run if api error
 14+ * @return ajax call object
 15+ */
 16+ isCategory: function( title, success, err ) {
 17+ var params = {
 18+ prop: 'categoryinfo',
 19+ titles: title.toString()
 20+ },
 21+ ok = function( data ) {
 22+ var exists = false;
 23+ if ( data.query && data.query.pages ) {
 24+ $.each( data.query.pages, function( id, page ) {
 25+ if ( page.categoryinfo ) {
 26+ exists = true;
 27+ }
 28+ } );
 29+ }
 30+ success( exists );
 31+ };
 32+
 33+ return this.get( params, { ok: ok, err: err } );
 34+ },
 35+
 36+ /**
 37+ * Get a list of categories that match a certain prefix.
 38+ * e.g. given "Foo", return "Food", "Foolish people", "Foosball tables" ...
 39+ * @param prefix {String} prefix to match
 40+ * @param success {Function} callback to pass matched categories to
 41+ * @param err {Function} optional callback to run if api error
 42+ * @return {jqXHR}
 43+ */
 44+ getCategoriesByPrefix: function( prefix, success, err ) {
 45+
 46+ // fetch with allpages to only get categories that have a corresponding description page.
 47+ var params = {
 48+ 'list': 'allpages',
 49+ 'apprefix': prefix,
 50+ 'apnamespace': mw.config.get('wgNamespaceIds').category
 51+ };
 52+
 53+ var ok = function( data ) {
 54+ var texts = [];
 55+ if ( data.query && data.query.allpages ) {
 56+ $.each( data.query.allpages, function( i, category ) {
 57+ texts.push( new mw.Title( category.title ).getNameText() );
 58+ } );
 59+ }
 60+ success( texts );
 61+ };
 62+
 63+ return this.get( params, { ok: ok, err: err } );
 64+ },
 65+
 66+
 67+ /**
 68+ * Get the categories that a particular page on the wiki belongs to
 69+ * @param title {mw.Title}
 70+ * @param success {Function} callback to pass categories to (or false, if title not found)
 71+ * @param err {Function} optional callback to run if api error
 72+ * @param async {Boolean} optional asynchronousness (default = true = async)
 73+ * @return {jqXHR}
 74+ */
 75+ getCategories: function( title, success, err, async ) {
 76+ var params, ok;
 77+ params = {
 78+ prop: 'categories',
 79+ titles: title.toString()
 80+ };
 81+ if ( async === undefined ) {
 82+ async = true;
 83+ }
 84+ ok = function( data ) {
 85+ var ret = false;
 86+ if ( data.query && data.query.pages ) {
 87+ $.each( data.query.pages, function( id, page ) {
 88+ if ( page.categories ) {
 89+ if ( typeof ret !== 'object' ) {
 90+ ret = [];
 91+ }
 92+ $.each( page.categories, function( i, cat ) {
 93+ ret.push( new mw.Title( cat.title ) );
 94+ } );
 95+ }
 96+ } );
 97+ }
 98+ success( ret );
 99+ };
 100+
 101+ return this.get( params, { ok: ok, err: err, async: async } );
 102+ }
 103+
 104+ } );
 105+
 106+} )( jQuery, mediaWiki );
Property changes on: trunk/phase3/resources/mediawiki.api/mediawiki.api.category.js
___________________________________________________________________
Added: svn:eol-style
1107 + native
Index: trunk/phase3/resources/mediawiki.api/mediawiki.api.js
@@ -0,0 +1,224 @@
 2+/* mw.Api objects represent the API of a particular MediaWiki server. */
 3+
 4+( function( $, mw, undefined ) {
 5+
 6+ /**
 7+ * @var defaultOptions {Object}
 8+ * We allow people to omit these default parameters from API requests
 9+ * there is very customizable error handling here, on a per-call basis
 10+ * wondering, would it be simpler to make it easy to clone the api object,
 11+ * change error handling, and use that instead?
 12+ */
 13+ var defaultOptions = {
 14+
 15+ // Query parameters for API requests
 16+ parameters: {
 17+ action: 'query',
 18+ format: 'json'
 19+ },
 20+
 21+ // Ajax options for jQuery.ajax()
 22+ ajax: {
 23+ url: mw.util.wikiScript( 'api' ),
 24+
 25+ ok: function() {},
 26+
 27+ // caller can supply handlers for http transport error or api errors
 28+ err: function( code, result ) {
 29+ mw.log( 'mw.Api error: ' + code, 'debug' );
 30+ },
 31+
 32+ timeout: 30000, // 30 seconds
 33+
 34+ dataType: 'json'
 35+ }
 36+ };
 37+
 38+ /**
 39+ * Constructor to create an object to interact with the API of a particular MediaWiki server.
 40+ *
 41+ * @todo Share API objects with exact same config.
 42+ * @example
 43+ * <code>
 44+ * var api = new mw.Api();
 45+ * api.get( {
 46+ * action: 'query',
 47+ * meta: 'userinfo'
 48+ * }, {
 49+ * ok: function () { console.log( arguments ); }
 50+ * } );
 51+ * </code>
 52+ *
 53+ * @constructor
 54+ * @param options {Object} See defaultOptions documentation above. Ajax options can also be
 55+ * overridden for each individual request to jQuery.ajax() later on.
 56+ */
 57+ mw.Api = function( options ) {
 58+
 59+ if ( options === undefined ) {
 60+ options = {};
 61+ }
 62+
 63+ // Force toString if we got a mw.Uri object
 64+ if ( options.ajax && options.ajax.url !== undefined ) {
 65+ options.ajax.url = String( options.ajax.url );
 66+ }
 67+
 68+ options.parameters = $.extend( {}, defaultOptions.parameters, options.parameters );
 69+ options.ajax = $.extend( {}, defaultOptions.ajax, options.ajax );
 70+
 71+ this.defaults = options;
 72+ };
 73+
 74+ mw.Api.prototype = {
 75+
 76+ /**
 77+ * For api queries, in simple cases the caller just passes a success callback.
 78+ * In complex cases they pass an object with a success property as callback and
 79+ * probably other options.
 80+ * Normalize the argument so that it's always the latter case.
 81+ *
 82+ * @param {Object|Function} An object contaning one or more of options.ajax,
 83+ * or just a success function (options.ajax.ok).
 84+ * @return {Object} Normalized ajax options.
 85+ */
 86+ normalizeAjaxOptions: function( arg ) {
 87+ var opt = arg;
 88+ if ( typeof arg === 'function' ) {
 89+ opt = { 'ok': arg };
 90+ }
 91+ if ( !opt.ok ) {
 92+ throw new Error( 'ajax options must include ok callback' );
 93+ }
 94+ return opt;
 95+ },
 96+
 97+ /**
 98+ * Perform API get request
 99+ *
 100+ * @param {Object} request parameters
 101+ * @param {Object|Function} ajax options, or just a success function
 102+ * @return {jqXHR}
 103+ */
 104+ get: function( parameters, ajaxOptions ) {
 105+ ajaxOptions = this.normalizeAjaxOptions( ajaxOptions );
 106+ ajaxOptions.type = 'GET';
 107+ return this.ajax( parameters, ajaxOptions );
 108+ },
 109+
 110+ /**
 111+ * Perform API post request
 112+ * @todo Post actions for nonlocal will need proxy
 113+ *
 114+ * @param {Object} request parameters
 115+ * @param {Object|Function} ajax options, or just a success function
 116+ * @return {jqXHR}
 117+ */
 118+ post: function( parameters, ajaxOptions ) {
 119+ ajaxOptions = this.normalizeAjaxOptions( ajaxOptions );
 120+ ajaxOptions.type = 'POST';
 121+ return this.ajax( parameters, ajaxOptions );
 122+ },
 123+
 124+ /**
 125+ * Perform the API call.
 126+ *
 127+ * @param {Object} request parameters
 128+ * @param {Object} ajax options
 129+ * @return {jqXHR}
 130+ */
 131+ ajax: function( parameters, ajaxOptions ) {
 132+ parameters = $.extend( {}, this.defaults.parameters, parameters );
 133+ ajaxOptions = $.extend( {}, this.defaults.ajax, ajaxOptions );
 134+
 135+ // Some deployed MediaWiki >= 1.17 forbid periods in URLs, due to an IE XSS bug
 136+ // So let's escape them here. See bug #28235
 137+ // This works because jQuery accepts data as a query string or as an Object
 138+ ajaxOptions.data = $.param( parameters ).replace( /\./g, '%2E' );
 139+
 140+ ajaxOptions.error = function( xhr, textStatus, exception ) {
 141+ ajaxOptions.err( 'http', {
 142+ xhr: xhr,
 143+ textStatus: textStatus,
 144+ exception: exception
 145+ } );
 146+ };
 147+
 148+ // Success just means 200 OK; also check for output and API errors
 149+ ajaxOptions.success = function( result ) {
 150+ if ( result === undefined || result === null || result === '' ) {
 151+ ajaxOptions.err( 'ok-but-empty',
 152+ 'OK response but empty result (check HTTP headers?)' );
 153+ } else if ( result.error ) {
 154+ var code = result.error.code === undefined ? 'unknown' : result.error.code;
 155+ ajaxOptions.err( code, result );
 156+ } else {
 157+ ajaxOptions.ok( result );
 158+ }
 159+ };
 160+
 161+ return $.ajax( ajaxOptions );
 162+ }
 163+
 164+ };
 165+
 166+ /**
 167+ * @var {Array} List of errors we might receive from the API.
 168+ * For now, this just documents our expectation that there should be similar messages
 169+ * available.
 170+ */
 171+ mw.Api.errors = [
 172+ // occurs when POST aborted
 173+ // jQuery 1.4 can't distinguish abort or lost connection from 200 OK + empty result
 174+ 'ok-but-empty',
 175+
 176+ // timeout
 177+ 'timeout',
 178+
 179+ // really a warning, but we treat it like an error
 180+ 'duplicate',
 181+ 'duplicate-archive',
 182+
 183+ // upload succeeded, but no image info.
 184+ // this is probably impossible, but might as well check for it
 185+ 'noimageinfo',
 186+ // remote errors, defined in API
 187+ 'uploaddisabled',
 188+ 'nomodule',
 189+ 'mustbeposted',
 190+ 'badaccess-groups',
 191+ 'stashfailed',
 192+ 'missingresult',
 193+ 'missingparam',
 194+ 'invalid-file-key',
 195+ 'copyuploaddisabled',
 196+ 'mustbeloggedin',
 197+ 'empty-file',
 198+ 'file-too-large',
 199+ 'filetype-missing',
 200+ 'filetype-banned',
 201+ 'filename-tooshort',
 202+ 'illegal-filename',
 203+ 'verification-error',
 204+ 'hookaborted',
 205+ 'unknown-error',
 206+ 'internal-error',
 207+ 'overwrite',
 208+ 'badtoken',
 209+ 'fetchfileerror',
 210+ 'fileexists-shared-forbidden',
 211+ 'invalidtitle',
 212+ 'notloggedin'
 213+ ];
 214+
 215+ /**
 216+ * @var {Array} List of warnings we might receive from the API.
 217+ * For now, this just documents our expectation that there should be similar messages
 218+ * available.
 219+ */
 220+ mw.Api.warnings = [
 221+ 'duplicate',
 222+ 'exists'
 223+ ];
 224+
 225+})( jQuery, mediaWiki );
Property changes on: trunk/phase3/resources/mediawiki.api/mediawiki.api.js
___________________________________________________________________
Added: svn:eol-style
1226 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r110760Ping r110759, remove erroneous extra subdirreedy17:06, 6 February 2012

Status & tagging log