<commit_before><?php

/**

Copyright (c) 2010, <NAME> and contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the owner nor the names of its contributors
      may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**/

/**
 * ApiProducer
 * @author <NAME> <<EMAIL>>
 * @version $Id$
 * @package ApiProducerRecords
 */

include('base.php');

class ApiProducerRecords extends ApiProducerBase {

	protected $allow_input_arrays = false;

	public function __construct() {
		parent::__construct();

		$this->api_parameters = array_merge(
			$this->api_parameters, array(
			'csvHeader' => true,
			'numResults' => 0,
			'outputFields' => array(),
			'sortDir' => 'desc',
			'sortField' => NULL,
			'startIndex' => 0,
		));

		$this->content_disposition = array_merge(
			$this->content_disposition, array(
			'csv' => 'csv',
		));

		$this->content_types = array_merge(
			$this->content_types, array(
			'csv' => 'text/csv',
		));

		$this->output_formats = array_merge(
			$this->output_formats, array(
			'csv',
			'list',
		));

		$this->requires_flat_output = array_merge(
			$this->requires_flat_output, array(
			'csv',
		));
	}

	/**
	 * Sanitize csvHeader value
	 * @param string $value
	 * @return bool
	 */
	protected function sanitizeParameter_csvHeader($value) {
		return $this->trueFalse($value, $this->api_parameters['csvHeader']);
	}

	/**
	 * Sanitize numResults value
	 * @param string $value
	 * @return int
	 */
	protected function sanitizeParameter_numResults($value) {
		if(ctype_digit((string) $value)) {
			return (int) $value;
		}

		return $this->api_parameters['numResults'];
	}

	/**
	 * Sanitize outputFields value
	 * @param mixed $value
	 * @return array
	 */
	protected function sanitizeParameter_outputFields($value) {
		$fields = array();
		$keys = array();

		if(is_array($value)) {
			$keys = $value;
		} else {
			$keys = explode(',', $value);
		}

		foreach($keys as $key) {
			if($key == '') {
				continue;
			}

			if(substr($key, 0, 1) == '!') {
				$fields[$key] = false;
			} else {
				$fields[$key] = true;
			}
		}

		return $fields;
	}

	/**
	 * Sanitize sortDir value
	 * @param string $value
	 * @return string
	 */
	protected function sanitizeParameter_sortDir($value) {
		switch(strtolower((string) $value)) {
			case 'asc':
			case 'ascending':
				return 'asc';
				break;
			case 'desc':
			case 'descending':
				return 'desc';
				break;
		}

		return $this->api_parameters['sortDir'];
	}

	/**
	 * Sanitize sortField value
	 * @param string $value
	 * @return string
	 */
	protected function sanitizeParameter_sortField($value) {
		if(is_null($value) || is_scalar($value)) {
			return $value;
		}

		return $this->api_parameters['sortField'];
	}

	/**
	 * Sanitize startIndex value
	 * @param string $value
	 * @return int
	 */
	protected function sanitizeParameter_startIndex($value) {
		if(ctype_digit((string) $value)) {
			return (int) $value;
		}

		return $this->api_parameters['startIndex'];
	}

	/**
	 * Show output
	 * @param array $records
	 * @param int $total
	 * @param int $status Optional
	 * @param string $message Optional
	 */
	public function showOutput($records = array(), $total = 0, $status = 200, $message = '') {
		$sort = $this->getSortField($records);
		$output = array(
			'message' => $message,
			'records' => $records,
			'recordsReturned' => count($records),
			'sortDir' => $this->getParameter('sortDir'),
			'startIndex' => $this->getParameter('startIndex'),
			'status' => $status,
			'totalRecords' => $total,
		);

		$function = 'showOutput_' . $this->getParameter('outputFormat');
		if(method_exists($this, $function)) {
			$this->$function($output);
			return;
		}

		// Fallback
		print_r($output);
	}<commit_msg>cleanup showOutput<commit_after><?php

/**

Copyright (c) 2010, <NAME> and contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the owner nor the names of its contributors
      may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**/

/**
 * ApiProducer
 * @author <NAME> <<EMAIL>>
 * @version $Id$
 * @package ApiProducerRecords
 */

include('base.php');

class ApiProducerRecords extends ApiProducerBase {

	protected $allow_input_arrays = false;

	public function __construct() {
		parent::__construct();

		$this->api_parameters = array_merge(
			$this->api_parameters, array(
			'csvHeader' => true,
			'numResults' => 0,
			'outputFields' => array(),
			'sortDir' => 'desc',
			'sortField' => NULL,
			'startIndex' => 0,
		));

		$this->content_disposition = array_merge(
			$this->content_disposition, array(
			'csv' => 'csv',
		));

		$this->content_types = array_merge(
			$this->content_types, array(
			'csv' => 'text/csv',
		));

		$this->output_formats = array_merge(
			$this->output_formats, array(
			'csv',
			'list',
		));

		$this->requires_flat_output = array_merge(
			$this->requires_flat_output, array(
			'csv',
		));
	}

	/**
	 * Sanitize csvHeader value
	 * @param string $value
	 * @return bool
	 */
	protected function sanitizeParameter_csvHeader($value) {
		return $this->trueFalse($value, $this->api_parameters['csvHeader']);
	}

	/**
	 * Sanitize numResults value
	 * @param string $value
	 * @return int
	 */
	protected function sanitizeParameter_numResults($value) {
		if(ctype_digit((string) $value)) {
			return (int) $value;
		}

		return $this->api_parameters['numResults'];
	}

	/**
	 * Sanitize outputFields value
	 * @param mixed $value
	 * @return array
	 */
	protected function sanitizeParameter_outputFields($value) {
		$fields = array();
		$keys = array();

		if(is_array($value)) {
			$keys = $value;
		} else {
			$keys = explode(',', $value);
		}

		foreach($keys as $key) {
			if($key == '') {
				continue;
			}

			if(substr($key, 0, 1) == '!') {
				$fields[$key] = false;
			} else {
				$fields[$key] = true;
			}
		}

		return $fields;
	}

	/**
	 * Sanitize sortDir value
	 * @param string $value
	 * @return string
	 */
	protected function sanitizeParameter_sortDir($value) {
		switch(strtolower((string) $value)) {
			case 'asc':
			case 'ascending':
				return 'asc';
				break;
			case 'desc':
			case 'descending':
				return 'desc';
				break;
		}

		return $this->api_parameters['sortDir'];
	}

	/**
	 * Sanitize sortField value
	 * @param string $value
	 * @return string
	 */
	protected function sanitizeParameter_sortField($value) {
		if(is_null($value) || is_scalar($value)) {
			return $value;
		}

		return $this->api_parameters['sortField'];
	}

	/**
	 * Sanitize startIndex value
	 * @param string $value
	 * @return int
	 */
	protected function sanitizeParameter_startIndex($value) {
		if(ctype_digit((string) $value)) {
			return (int) $value;
		}

		return $this->api_parameters['startIndex'];
	}

	/**
	 * Show output
	 * @param array $records
	 * @param int $total
	 * @param int $status Optional
	 * @param string $message Optional
	 */
	public function showOutput($records = array(), $total = 0, $status = 200, $message = '') {
		$count = count($records);

		$output = array(
			'message' => $message,
			'records' => $records,
			'recordsReturned' => $count,
			'sortDir' => $this->getParameter('sortDir'),
			'startIndex' => $this->getParameter('startIndex'),
			'status' => $status,
			'totalRecords' => ($total) ? $total : $count,
		);

		$function = 'showOutput_' . $this->getParameter('outputFormat');
		if(method_exists($this, $function)) {
			$this->$function($output);
			return;
		}

		// Fallback
		print_r($output);
	}
{'fraction_non_alphanumeric': 0.12990244873581525, 'fraction_numerical': 0.002389010551463269, 'mean_word_length': 3.8302884615384616, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 82, 'https://': 0, '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': '2853842', 'n_tokens_mistral': 3485, 'n_tokens_neox': 3004, 'n_words': 931}