summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki.api/mediawiki.api.watch.js
blob: a2ff1292bba299a86449e2ce3ee4f3fac00f6737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * @class mw.Api.plugin.watch
 * @since 1.19
 */
( function ( mw, $ ) {

	/**
	 * @private
	 * @static
	 * @context mw.Api
	 *
	 * @param {string|mw.Title|string[]|mw.Title[]} pages Full page name or instance of mw.Title, or an
	 *  array thereof. If an array is passed, the return value passed to the promise will also be an
	 *  array of appropriate objects.
	 * @return {jQuery.Promise}
	 * @return {Function} return.done
	 * @return {Object|Object[]} return.done.watch Object or list of objects (depends on the `pages`
	 *  parameter)
	 * @return {string} return.done.watch.title Full pagename
	 * @return {boolean} return.done.watch.watched Whether the page is now watched or unwatched
	 * @return {string} return.done.watch.message Parsed HTML of the confirmational interface message
	 */
	function doWatchInternal( pages, addParams ) {
		// XXX: Parameter addParams is undocumented because we inherit this
		// documentation in the public method...
		var apiPromise = this.postWithToken( 'watch',
			$.extend(
				{
					action: 'watch',
					titles: $.isArray( pages ) ? pages.join( '|' ) : String( pages ),
					uselang: mw.config.get( 'wgUserLanguage' )
				},
				addParams
			)
		);

		return apiPromise
			.then( function ( data ) {
				// If a single page was given (not an array) respond with a single item as well.
				return $.isArray( pages ) ? data.watch : data.watch[ 0 ];
			} )
			.promise( { abort: apiPromise.abort } );
	}

	$.extend( mw.Api.prototype, {
		/**
		 * Convenience method for `action=watch`.
		 *
		 * @inheritdoc #doWatchInternal
		 */
		watch: function ( pages ) {
			return doWatchInternal.call( this, pages );
		},

		/**
		 * Convenience method for `action=watch&unwatch=1`.
		 *
		 * @inheritdoc #doWatchInternal
		 */
		unwatch: function ( pages ) {
			return doWatchInternal.call( this, pages, { unwatch: 1 } );
		}
	} );

	/**
	 * @class mw.Api
	 * @mixins mw.Api.plugin.watch
	 */

}( mediaWiki, jQuery ) );