Merge "Exclude redirects from Special:Fewestrevisions"
[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 options = options || {};
32 mw.messagePoster.WikitextMessagePoster.parent.prototype.post.call( this, subject, body, options );
33
34 // Add signature if needed
35 if ( body.indexOf( '~~~' ) === -1 ) {
36 body += '\n\n~~~~';
37 }
38
39 additionalParams = { redirect: true };
40 if ( options.tags !== undefined ) {
41 additionalParams.tags = options.tags;
42 }
43 return this.api.newSection(
44 this.title,
45 subject,
46 body,
47 additionalParams
48 ).then( function ( resp, jqXHR ) {
49 if ( resp.edit.result === 'Success' ) {
50 return $.Deferred().resolve( resp, jqXHR );
51 } else {
52 // mw.Api checks for response error. Are there actually cases where the
53 // request fails, but it's not caught there?
54 return $.Deferred().reject( 'api-unexpected' );
55 }
56 }, function ( code, details ) {
57 return $.Deferred().reject( 'api-fail', code, details );
58 } ).promise();
59 };
60
61 mw.messagePoster.factory.register( 'wikitext', WikitextMessagePoster );
62 mw.messagePoster.WikitextMessagePoster = WikitextMessagePoster;
63 }() );