Merge "[FileBackend] Use the new CloudFiles metadata functions."
[lhc/web/wiklou.git] / resources / mediawiki.api / mediawiki.api.edit.js
1 /**
2 * Additional mw.Api methods to assist with API calls related to editing wiki pages.
3 */
4 ( function ( mw, $ ) {
5
6 // Cache token so we don't have to keep fetching new ones for every single request.
7 var cachedToken = null;
8
9 $.extend( mw.Api.prototype, {
10
11 /**
12 * Post to API with edit token. If we have no token, get one and try to post.
13 * If we have a cached token try using that, and if it fails, blank out the
14 * cached token and start over.
15 *
16 * @param params {Object} API parameters
17 * @param ok {Function} callback for success
18 * @param err {Function} [optional] error callback
19 * @return {jqXHR}
20 */
21 postWithEditToken: function ( params, ok, err ) {
22 var useTokenToPost, getTokenIfBad,
23 api = this;
24 if ( cachedToken === null ) {
25 // We don't have a valid cached token, so get a fresh one and try posting.
26 // We do not trap any 'badtoken' or 'notoken' errors, because we don't want
27 // an infinite loop. If this fresh token is bad, something else is very wrong.
28 useTokenToPost = function ( token ) {
29 params.token = token;
30 api.post( params, ok, err );
31 };
32 return api.getEditToken( useTokenToPost, err );
33 } else {
34 // We do have a token, but it might be expired. So if it is 'bad' then
35 // start over with a new token.
36 params.token = cachedToken;
37 getTokenIfBad = function ( code, result ) {
38 if ( code === 'badtoken' ) {
39 // force a new token, clear any old one
40 cachedToken = null;
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 action: 'tokens',
65 type: 'edit'
66 },
67 ok = function ( data ) {
68 var token;
69 // If token type is not available for this user,
70 // key 'edittoken' is missing or can contain Boolean false
71 if ( data.tokens && data.tokens.edittoken ) {
72 token = data.tokens.edittoken;
73 cachedToken = token;
74 tokenCallback( token );
75 } else {
76 err( 'token-missing', data );
77 }
78 },
79 ajaxOptions = {
80 ok: ok,
81 err: err,
82 // Due to the API assuming we're logged out if we pass the callback-parameter,
83 // we have to disable jQuery's callback system, and instead parse JSON string,
84 // by setting 'jsonp' to false.
85 jsonp: false
86 };
87
88 return this.get( parameters, ajaxOptions );
89 },
90
91 /**
92 * Create a new section of the page.
93 * @param title {mw.Title|String} target page
94 * @param header {String}
95 * @param message {String} wikitext message
96 * @param ok {Function} success handler
97 * @param err {Function} error handler
98 * @return {jqXHR}
99 */
100 newSection: function ( title, header, message, ok, err ) {
101 var params = {
102 action: 'edit',
103 section: 'new',
104 format: 'json',
105 title: title.toString(),
106 summary: header,
107 text: message
108 };
109 return this.postWithEditToken( params, ok, err );
110 }
111
112 } );
113
114 }( mediaWiki, jQuery ) );