<commit_before>'use strict';

import { pagination, api, aggregate } from './util';
import wikiPage from './page';

/**
 * @namespace
 * @constant
 * @property {string} apiUrl - URL of Wikipedia API
 * @property {string} headers - Headers to pass through to the API request
 * @property {string} origin - When accessing the API using a cross-domain AJAX
 * request (CORS), set this to the originating domain. This must be included in
 * any pre-flight request, and therefore must be part of the request URI (not
 * the POST body). This must match one of the origins in the Origin header
 * exactly, so it has to be set to something like https://en.wikipedia.org or
 * https://meta.wikimedia.org. If this parameter does not match the Origin
 * header, a 403 response will be returned. If this parameter matches the Origin
 * header and the origin is whitelisted, an Access-Control-Allow-Origin header
 * will be set.
 */
const defaultOptions = {
	apiUrl: 'http://en.wikipedia.org/w/api.php',
	origin: '*'
};

/**
 * wiki
 * @example
 * wiki({ apiUrl: 'http://fr.wikipedia.org/w/api.php' }).search(...);
 * @namespace Wiki
 * @param  {Object} options
 * @return {Object} - wiki (for chaining methods)
 */
export default function wiki(options = {}) {
	if (this instanceof wiki) {
		// eslint-disable-next-line
		console.log(
			'Please do not use wikijs ^1.0.0 as a class. Please see the new README.'
		);
	}

	const apiOptions = Object.assign({}, defaultOptions, options);

	function handleRedirect(res) {
		if (res.query.redirects && res.query.redirects.length === 1) {
			return api(apiOptions, {
				prop: 'info|pageprops',
				inprop: 'url',
				ppprop: 'disambiguation',
				titles: res.query.redirects[0].to
			});
		}
		return res;
	}

	/**
	 * Search articles
	 * @example
	 * wiki.search('star wars').then(data => console.log(data.results.length));
	 * @example
	 * wiki.search('star wars').then(data => {
	 * 	data.next().then(...);
	 * });
	 * @method Wiki#search
	 * @param  {string} query - keyword query
	 * @param  {Number} [limit] - limits the number of results
	 * @return {Promise} - pagination promise with results and next page function
	 */
	function search(query, limit = 50, all = false) {
		return pagination(
			apiOptions,
			{
				list: 'search',
				srsearch: query,
				srlimit: limit
			},
			res => res.query.search.map(article => {
				return all ? article : article.title;
			})
		).catch(err => {
			if (err.message === '"text" search is disabled.') {
				// Try backup search method
				return opensearch(query, limit);
			}
			throw err;
		});
	}

	/**
	 * Search articles using "fuzzy" prefixsearch
	 * @example
	 * wiki.prefixSearch('star wars').then(data => console.log(data.results.length));
	 * @example
	 * wiki.prefixSearch('star wars').then(data => {
	 * 	data.next().then(...);
	 * });
	 * @method Wiki#prefixSearch
	 * @param  {string} query - keyword query
	 * @param  {Number} [limit] - limits the number of results<commit_msg>Added docs for new search param<commit_after>'use strict';

import { pagination, api, aggregate } from './util';
import wikiPage from './page';

/**
 * @namespace
 * @constant
 * @property {string} apiUrl - URL of Wikipedia API
 * @property {string} headers - Headers to pass through to the API request
 * @property {string} origin - When accessing the API using a cross-domain AJAX
 * request (CORS), set this to the originating domain. This must be included in
 * any pre-flight request, and therefore must be part of the request URI (not
 * the POST body). This must match one of the origins in the Origin header
 * exactly, so it has to be set to something like https://en.wikipedia.org or
 * https://meta.wikimedia.org. If this parameter does not match the Origin
 * header, a 403 response will be returned. If this parameter matches the Origin
 * header and the origin is whitelisted, an Access-Control-Allow-Origin header
 * will be set.
 */
const defaultOptions = {
	apiUrl: 'http://en.wikipedia.org/w/api.php',
	origin: '*'
};

/**
 * wiki
 * @example
 * wiki({ apiUrl: 'http://fr.wikipedia.org/w/api.php' }).search(...);
 * @namespace Wiki
 * @param  {Object} options
 * @return {Object} - wiki (for chaining methods)
 */
export default function wiki(options = {}) {
	if (this instanceof wiki) {
		// eslint-disable-next-line
		console.log(
			'Please do not use wikijs ^1.0.0 as a class. Please see the new README.'
		);
	}

	const apiOptions = Object.assign({}, defaultOptions, options);

	function handleRedirect(res) {
		if (res.query.redirects && res.query.redirects.length === 1) {
			return api(apiOptions, {
				prop: 'info|pageprops',
				inprop: 'url',
				ppprop: 'disambiguation',
				titles: res.query.redirects[0].to
			});
		}
		return res;
	}

	/**
	 * Search articles
	 * @example
	 * wiki.search('star wars').then(data => console.log(data.results.length));
	 * @example
	 * wiki.search('star wars').then(data => {
	 * 	data.next().then(...);
	 * });
	 * @method Wiki#search
	 * @param  {string} query - keyword query
	 * @param  {Number} [limit] - limits the number of results
	 * @param  {Boolean} [all] - returns entire article objects instead of just titles
	 * @return {Promise} - pagination promise with results and next page function
	 */
	function search(query, limit = 50, all = false) {
		return pagination(
			apiOptions,
			{
				list: 'search',
				srsearch: query,
				srlimit: limit
			},
			res =>
				res.query.search.map(article => {
					return all ? article : article.title;
				})
		).catch(err => {
			if (err.message === '"text" search is disabled.') {
				// Try backup search method
				return opensearch(query, limit);
			}
			throw err;
		});
	}

	/**
	 * Search articles using "fuzzy" prefixsearch
	 * @example
	 * wiki.prefixSearch('star wars').then(data => console.log(data.results.length));
	 * @example
	 * wiki.prefixSearch('star wars').then(data => {
	 * 	data.next().then(...);
	 * });
	 * @method Wiki#prefixSearch
	 * @param  {string} query - keyword query
	 * @param  {Number} [limit] - limits the number of results
{'fraction_non_alphanumeric': 0.1429279576999339, 'fraction_numerical': 0.003304692663582287, 'mean_word_length': 3.2898653437278527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1113019', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1819, 'n_words': 611}