All files / mobile.special.watchlist.scripts WatchListGateway.js

96.77% Statements 30/31
87.5% Branches 14/16
100% Functions 5/5
96.77% Lines 30/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99  1x 1x 1x               5x   5x   5x 1x       1x   4x     4x     5x     1x             5x 5x               5x     5x     5x 4x   1x     5x                         5x 1x     4x       4x 31x         4x 1x 1x       4x         1x  
var
	pageJSONParser = require( '../mobile.startup/page/pageJSONParser' ),
	util = require( '../mobile.startup/util' ),
	extendSearchParams = require( '../mobile.startup/extendSearchParams' );
 
/**
 * @class WatchListGateway
 * @param {mw.Api} api
 * @param {string} lastTitle of page listed in Watchlist to be used as a continuation parameter
 */
function WatchListGateway( api, lastTitle ) {
	this.api = api;
	// Try to keep it in sync with SpecialMobileWatchlist::LIMIT (php)
	this.limit = 50;
 
	if ( lastTitle ) {
		this.continueParams = {
			continue: 'gwrcontinue||',
			gwrcontinue: '0|' + lastTitle.replace( / /g, '_' )
		};
		this.shouldSkipFirstTitle = true;
	} else {
		this.continueParams = {
			continue: ''
		};
		this.shouldSkipFirstTitle = false;
	}
 
	this.canContinue = true;
}
 
WatchListGateway.prototype = {
	/**
	 * Load the list of items on the watchlist
	 *
	 * @return {jQuery.Deferred}
	 */
	loadWatchlist: function () {
		var self = this,
			params = extendSearchParams( 'watchlist', {
				prop: [ 'info', 'revisions' ],
				rvprop: 'timestamp|user',
				generator: 'watchlistraw',
				gwrnamespace: '0',
				gwrlimit: this.limit
			}, this.continueParams );
 
		Iif ( this.canContinue === false ) {
			return util.Deferred().resolve( [] );
		}
		return this.api.get( params, {
			url: this.apiUrl
		} ).then( function ( data ) {
			if ( data.continue !== undefined ) {
				self.continueParams = data.continue;
			} else {
				self.canContinue = false;
			}
 
			return self.parseData( data );
		} );
	},
 
	/**
	 * Parse api response data into pagelist item format
	 *
	 * @param {Object[]} data
	 * @return {Page[]}
	 */
	parseData: function ( data ) {
		var pages;
 
		if ( !data.query || !data.query.pages ) {
			return [];
		}
 
		pages = data.query.pages;
 
		// Sort results alphabetically (the api map doesn't have any order). The
		// watchlist is ordered alphabetically right now.
		pages.sort( function ( p1, p2 ) {
			return p1.title === p2.title ? 0 : ( p1.title < p2.title ? -1 : 1 );
		} );
 
		// If we requested from the last item of the previous page, we shall
		// remove the first result (to avoid it being repeated)
		if ( this.shouldSkipFirstTitle ) {
			pages = pages.slice( 1 );
			this.shouldSkipFirstTitle = false;
		}
 
		// Transform the items to a sensible format
		return pages.map( pageJSONParser.parse );
	}
 
};
 
module.exports = WatchListGateway;