All files / mobile.startup extendSearchParams.js

94.44% Statements 17/18
70% Branches 7/10
100% Functions 1/1
94.44% Lines 17/18

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 741x 1x                                                   13x             13x       13x 1x                       12x 12x     12x   12x 12x   12x 9x 9x       12x       12x   1x  
var util = require( './util' ),
	actionParams = require( './actionParams.js' );
 
/**
 * Extends the API query parameters to include those parameters required to also fetch Wikibase
 * descriptions and appropriately sized thumbnail images as well as those required to make a query.
 *
 * This function wraps `util.extend` with some Wikibase-specific configuration
 * variable management
 * but, like `util.extend`, is variadic and so can be used as a replacement for it in search
 * gateways, e.g.
 *
 * ```
 * var params = extendSearchParams(
 *   'search',
 *   baseParams,
 *   specializedParams,
 *   moreSpecializedParams
 * );
 * ```
 *
 * @param {string} feature The name of the feature
 * @throws {Error} If `feature` isn't one that shows Wikidata descriptions. See the
 *  `wgMFDisplayWikibaseDescriptions` configuration variable for detail
 * @return {Object}
 */
function extendSearchParams( feature ) {
	var displayWikibaseDescriptions = mw.config.get( 'wgMFDisplayWikibaseDescriptions' ) || {
			// These must be defined, as these are all the features that this can be used on.
			// If not defined, all these features will see their API calls broken
			search: true,
			watchlist: true,
			tagline: false
		},
		scriptPath = mw.config.get( 'wgMFScriptPath' ),
		args,
		result;
 
	if ( !Object.prototype.hasOwnProperty.call( displayWikibaseDescriptions, feature ) ) {
		throw new Error( '"' + feature + '" isn\'t a feature that shows Wikibase descriptions.' );
	}
 
	// Construct the arguments for a call to `util.extend`
	// such that if it were hand-written, then it
	// would look like the following:
	//
	// ```
	// var result = util.extend( {
	//   prop: []
	// }, params, /* ..., */ mw.config.get( 'wgMFSearchAPIParams' ) );
	// ```
	args = Array.prototype.slice.call( arguments, 1 );
	args.unshift( {
		prop: []
	} );
	args.push( mw.config.get( 'wgMFSearchAPIParams' ) );
 
	result = util.extend.apply( {}, args );
	result.prop = result.prop.concat( mw.config.get( 'wgMFQueryPropModules' ) );
 
	if ( displayWikibaseDescriptions[feature] ) {
		Eif ( result.prop.indexOf( 'description' ) === -1 ) {
			result.prop.push( 'description' );
		}
	}
 
	Iif ( scriptPath ) {
		// A foreign api is being accessed! Enable anonymous CORS queries!
		result.origin = '*';
	}
	return actionParams( result );
}
module.exports = extendSearchParams;