Debian Codegraph @

resources/src/mediawiki.api/edit.js

raw symbols SCIP
1 ( function () {
2
3 Object.assign( mw.Api.prototype, /** @lends mw.Api.prototype */ {
4 /**
5 * @callback mw.Api.EditTransform
6 * @param {Object} revision Current revision
7 * @param {string} revision.content Current revision content
8 * @return {string|Object|jQuery.Promise} New content, object with edit
9 * API parameters, or promise providing one of those.
10 */
11
12 /**
13 * Post to API with `csrf` token. See [#postWithToken]{@link mw.Api#postWithToken}
14 *
15 * @param {Object} params API parameters
16 * @param {Object} [ajaxOptions]
17 * @return {jQuery.Promise} See [#post]{@link mw.Api#post}
18 */
19 postWithEditToken: function ( params, ajaxOptions ) {
20 return this.postWithToken( 'csrf', params, ajaxOptions );
21 },
22
23 /**
24 * API helper to grab a csrf token.
25 *
26 * @return {jQuery.Promise} Received token.
27 */
28 getEditToken: function () {
29 return this.getToken( 'csrf' );
30 },
31
32 /**
33 * Create a new page.
34 *
35 * @example
36 * new mw.Api().create( 'Sandbox',
37 * { summary: 'Load sand particles.' },
38 * 'Sand.'
39 * );
40 *
41 * @since 1.28
42 * @param {mw.Title|string} title Page title
43 * @param {Object} params Edit API parameters
44 * @param {string} params.summary Edit summary
45 * @param {string} content
46 * @return {jQuery.Promise} API response
47 */
48 create: function ( title, params, content ) {
49 return this.postWithEditToken( Object.assign( this.assertCurrentUser( {
50 action: 'edit',
51 title: String( title ),
52 text: content,
53 formatversion: '2',
54 // Protect against conflicts
55 createonly: true
56 } ), params ) ).then( ( data ) => data.edit );
57 },
58
59 /**
60 * Edit an existing page.
61 *
62 * To create a new page, use #create() instead.
63 *
64 * Simple transformation:
65 * ```
66 * new mw.Api()
67 * .edit( 'Sandbox', function ( revision ) {
68 * return revision.content.replace( 'foo', 'bar' );
69 * } )
70 * .then( function () {
71 * console.log( 'Saved!' );
72 * } );
73 * ```
74 *
75 * Set save parameters by returning an object instead of a string:
76 * ```
77 * new mw.Api().edit(
78 * 'Sandbox',
79 * function ( revision ) {
80 * return {
81 * text: revision.content.replace( 'foo', 'bar' ),
82 * summary: 'Replace "foo" with "bar".',
83 * assert: 'bot',
84 * minor: true
85 * };
86 * }
87 * )
88 * .then( function () {
89 * console.log( 'Saved!' );
90 * } );
91 * ```
92 *
93 * Transform asynchronously by returning a promise.
94 * ```
95 * new mw.Api()
96 * .edit( 'Sandbox', function ( revision ) {
97 * return Spelling
98 * .corrections( revision.content )
99 * .then( function ( report ) {
100 * return {
101 * text: report.output,
102 * summary: report.changelog
103 * };
104 * } );
105 * } )
106 * .then( function () {
107 * console.log( 'Saved!' );
108 * } );
109 * ```
110 *
111 * @since 1.28
112 * @param {mw.Title|string} title Page title
113 * @param {mw.Api.EditTransform} transform Callback that prepares the edit
114 * @return {jQuery.Promise} Edit API response
115 */
116 edit: function ( title, transform ) {
117 const api = this;
118
119 title = String( title );
120
121 let basetimestamp, curtimestamp;
122 return api.get( {
123 action: 'query',
124 prop: 'revisions',
125 rvprop: [ 'content', 'timestamp' ],
126 titles: [ title ],
127 formatversion: '2',
128 curtimestamp: true
129 } )
130 .then( ( data ) => {
131 if ( !data.query || !data.query.pages ) {
132 return $.Deferred().reject( 'unknown' );
133 }
134 const page = data.query.pages[ 0 ];
135 if ( !page || page.invalid ) {
136 return $.Deferred().reject( 'invalidtitle' );
137 }
138 if ( page.missing ) {
139 return $.Deferred().reject( 'nocreate-missing' );
140 }
141 const revision = page.revisions[ 0 ];
142 basetimestamp = revision.timestamp;
143 curtimestamp = data.curtimestamp;
144 return transform( {
145 timestamp: revision.timestamp,
146 content: revision.content
147 } );
148 } )
149 .then( ( params ) => {
150 const editParams = typeof params === 'object' ? params : { text: String( params ) };
151 return api.postWithEditToken( Object.assign( {
152 action: 'edit',
153 title: title,
154 formatversion: '2',
155
156 // Protect against errors and conflicts
157 assert: mw.config.get( 'wgUserName' ) ? 'user' : undefined,
158 basetimestamp: basetimestamp,
159 starttimestamp: curtimestamp,
160 nocreate: true
161 }, editParams ) );
162 } )
163 .then( ( data ) => data.edit );
164 },
165
166 /**
167 * Post a new section to the page.
168 *
169 * @see mw.Api#postWithEditToken
170 * @param {mw.Title|string} title Target page
171 * @param {string} header
172 * @param {string} message wikitext message
173 * @param {Object} [additionalParams] Additional API parameters, e.g. `{ redirect: true }`
174 * @return {jQuery.Promise}
175 */
176 newSection: function ( title, header, message, additionalParams ) {
177 return this.postWithEditToken( Object.assign( {
178 action: 'edit',
179 section: 'new',
180 title: String( title ),
181 summary: header,
182 text: message
183 }, additionalParams ) );
184 }
185 } );
186
187 }() );