fd65e8a5d9b2fa148a0e95cb29d93a813a994bae
[lhc/web/wiklou.git] / resources / src / mediawiki.messagePoster.wikitext / WikitextMessagePoster.js
1 ( function () {
2 /**
3 * This is an implementation of MessagePoster for wikitext talk pages.
4 *
5 * @class mw.messagePoster.WikitextMessagePoster
6 * @extends mw.messagePoster.MessagePoster
7 *
8 * @constructor
9 * @param {mw.Title} title Wikitext page in a talk namespace, to post to
10 * @param {mw.Api} api mw.Api object to use
11 */
12 function WikitextMessagePoster( title, api ) {
13 this.api = api;
14 this.title = title;
15 }
16
17 OO.inheritClass(
18 WikitextMessagePoster,
19 mw.messagePoster.MessagePoster
20 );
21
22 /**
23 * @inheritdoc
24 * @param {string} subject Section title.
25 * @param {string} body Message body, as wikitext. Signature code will automatically be added unless the message already contains the string ~~~.
26 * @param {Object} [options] Message options:
27 * @param {string} [options.tags] [Change tags](https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Tags) to add to the message's revision, pipe-separated.
28 */
29 WikitextMessagePoster.prototype.post = function ( subject, body, options ) {
30 var additionalParams;
31 mw.messagePoster.WikitextMessagePoster.parent.prototype.post.call( this, subject, body, options );
32
33 // Add signature if needed
34 if ( body.indexOf( '~~~' ) === -1 ) {
35 body += '\n\n~~~~';
36 }
37
38 additionalParams = { redirect: true };
39 if ( options.tags !== undefined ) {
40 additionalParams.tags = options.tags;
41 }
42 return this.api.newSection(
43 this.title,
44 subject,
45 body,
46 additionalParams
47 ).then( function ( resp, jqXHR ) {
48 if ( resp.edit.result === 'Success' ) {
49 return $.Deferred().resolve( resp, jqXHR );
50 } else {
51 // mw.Api checks for response error. Are there actually cases where the
52 // request fails, but it's not caught there?
53 return $.Deferred().reject( 'api-unexpected' );
54 }
55 }, function ( code, details ) {
56 return $.Deferred().reject( 'api-fail', code, details );
57 } ).promise();
58 };
59
60 mw.messagePoster.factory.register( 'wikitext', WikitextMessagePoster );
61 mw.messagePoster.WikitextMessagePoster = WikitextMessagePoster;
62 }() );