2 * @class mw.Api.plugin.edit
6 $.extend( mw
.Api
.prototype, {
9 * Post to API with csrf token. If we have no token, get one and try to post.
10 * If we have a cached token try using that, and if it fails, blank out the
11 * cached token and start over.
13 * @param {Object} params API parameters
14 * @param {Object} [ajaxOptions]
15 * @return {jQuery.Promise} See #post
17 postWithEditToken: function ( params
, ajaxOptions
) {
18 return this.postWithToken( 'csrf', params
, ajaxOptions
);
22 * API helper to grab a csrf token.
24 * @return {jQuery.Promise} Received token.
26 getEditToken: function () {
27 return this.getToken( 'csrf' );
35 * new mw.Api().create( 'Sandbox',
36 * { summary: 'Load sand particles.' },
41 * @param {mw.Title|string} title Page title
42 * @param {Object} params Edit API parameters
43 * @param {string} params.summary Edit summary
44 * @param {string} content
45 * @return {jQuery.Promise} API response
47 create: function ( title
, params
, content
) {
48 return this.postWithEditToken( $.extend( {
50 title
: String( title
),
54 // Protect against errors and conflicts
55 assert
: mw
.user
.isAnon() ? undefined : 'user',
57 }, params
) ).then( function ( data
) {
63 * Edit an existing page.
65 * To create a new page, use #create() instead.
67 * Simple transformation:
70 * .edit( 'Sandbox', function ( revision ) {
71 * return revision.content.replace( 'foo', 'bar' );
73 * .then( function () {
74 * console.log( 'Saved! ');
77 * Set save parameters by returning an object instead of a string:
81 * function ( revision ) {
83 * text: revision.content.replace( 'foo', 'bar' ),
84 * summary: 'Replace "foo" with "bar".',
90 * .then( function () {
91 * console.log( 'Saved! ');
94 * Transform asynchronously by returning a promise.
97 * .edit( 'Sandbox', function ( revision ) {
99 * .corrections( revision.content )
100 * .then( function ( report ) {
102 * text: report.output,
103 * summary: report.changelog
107 * .then( function () {
108 * console.log( 'Saved! ');
112 * @param {mw.Title|string} title Page title
113 * @param {Function} transform Callback that prepares the edit
114 * @param {Object} transform.revision Current revision
115 * @param {string} transform.revision.content Current revision content
116 * @param {string|Object|jQuery.Promise} transform.return New content, object with edit
117 * API parameters, or promise providing one of those.
118 * @return {jQuery.Promise} Edit API response
120 edit: function ( title
, transform
) {
121 var basetimestamp
, curtimestamp
,
126 rvprop
: [ 'content', 'timestamp' ],
127 titles
: String( title
),
131 .then( function ( data
) {
133 if ( !data
.query
|| !data
.query
.pages
) {
134 return $.Deferred().reject( 'unknown' );
136 page
= data
.query
.pages
[ 0 ];
137 if ( !page
|| page
.missing
) {
138 return $.Deferred().reject( 'nocreate-missing' );
140 revision
= page
.revisions
[ 0 ];
141 basetimestamp
= revision
.timestamp
;
142 curtimestamp
= data
.curtimestamp
;
144 timestamp
: revision
.timestamp
,
145 content
: revision
.content
148 .then( function ( params
) {
149 var editParams
= typeof params
=== 'object' ? params
: { text
: String( params
) };
150 return api
.postWithEditToken( $.extend( {
155 // Protect against errors and conflicts
156 assert
: mw
.user
.isAnon() ? undefined : 'user',
157 basetimestamp
: basetimestamp
,
158 starttimestamp
: curtimestamp
,
162 .then( function ( data
) {
168 * Post a new section to the page.
170 * @see #postWithEditToken
171 * @param {mw.Title|string} title Target page
172 * @param {string} header
173 * @param {string} message wikitext message
174 * @param {Object} [additionalParams] Additional API parameters, e.g. `{ redirect: true }`
175 * @return {jQuery.Promise}
177 newSection: function ( title
, header
, message
, additionalParams
) {
178 return this.postWithEditToken( $.extend( {
181 title
: String( title
),
184 }, additionalParams
) );
190 * @mixins mw.Api.plugin.edit
193 }( mediaWiki
, jQuery
) );