Merge "Revert "(bug 260) Handle <pre> overflow automatically with a scroll bar""
[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
5 ( function( $, mw, undefined ) {
6
7 // Cache token so we don't have to keep fetching new ones for every single request.
8 var cachedToken = null;
9
10 $.extend( mw.Api.prototype, {
11
12 /**
13 * Post to API with edit token. If we have no token, get one and try to post.
14 * If we have a cached token try using that, and if it fails, blank out the
15 * cached token and start over.
16 *
17 * @param params {Object} API parameters
18 * @param ok {Function} callback for success
19 * @param err {Function} [optional] error callback
20 * @return {jqXHR}
21 */
22 postWithEditToken: function( params, ok, err ) {
23 var api = this, useTokenToPost, getTokenIfBad;
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 cachedToken = null; // force a new token
40 api.postWithEditToken( params, ok, err );
41 } else {
42 err( code, result );
43 }
44 };
45 return api.post( params, { ok : ok, err : getTokenIfBad });
46 }
47 },
48
49 /**
50 * Api helper to grab an edit token
51 *
52 * token callback has signature ( String token )
53 * error callback has signature ( String code, Object results, XmlHttpRequest xhr, Exception exception )
54 * Note that xhr and exception are only available for 'http_*' errors
55 * code may be any http_* error code (see mw.Api), or 'token_missing'
56 *
57 * @param tokenCallback {Function} received token callback
58 * @param err {Function} error callback
59 * @return {jqXHR}
60 */
61 getEditToken: function( tokenCallback, err ) {
62 var parameters = {
63 action: 'tokens',
64 type: 'edit'
65 },
66 ok = function( data ) {
67 var token;
68 // If token type is not available for this user,
69 // key 'edittoken' is missing or can contain Boolean false
70 if ( data.tokens && data.tokens.edittoken ) {
71 token = data.tokens.edittoken;
72 cachedToken = token;
73 tokenCallback( token );
74 } else {
75 err( 'token-missing', data );
76 }
77 },
78 ajaxOptions = {
79 ok: ok,
80 err: err,
81 // Due to the API assuming we're logged out if we pass the callback-parameter,
82 // we have to disable jQuery's callback system, and instead parse JSON string,
83 // by setting 'jsonp' to false.
84 jsonp: false
85 };
86
87 return this.get( parameters, ajaxOptions );
88 },
89
90 /**
91 * Create a new section of the page.
92 * @param title {mw.Title|String} target page
93 * @param header {String}
94 * @param message {String} wikitext message
95 * @param ok {Function} success handler
96 * @param err {Function} error handler
97 * @return {jqXHR}
98 */
99 newSection: function( title, header, message, ok, err ) {
100 var params = {
101 action: 'edit',
102 section: 'new',
103 format: 'json',
104 title: title.toString(),
105 summary: header,
106 text: message
107 };
108 return this.postWithEditToken( params, ok, err );
109 }
110
111 } );
112
113 } )( jQuery, mediaWiki );