<commit_before><?php
/**
 * CouchDBSource Datasource file
 *
 * Provides a CouchDB Datasource for CakePHP
 *
 * @author maxwellium - https://github.com/maxwellium
 * @package CouchDB
 * @subpackage Model.Datassource
 * @filesource
 */
App::uses('HttpSocket', 'Network/Http');

/**
 * This is the CouchDB Datasource class
 *
 * @package CouchDB
 * @subpackage Model.Datasource
 */
class CouchDBSource extends DataSource {

/**
 * The Datasource's default configuration
 *
 * @var array
 * @access protected
 */
	protected $_baseconfig = array(
		'request'   => array(
			'uri' => array(
				'host' => 'localhost',
				'port' => 5984
			),
			'header'  => array(
				'Content-Type' => 'application/json'
			)
		),
		'authType'  => 'basic',
		'models'    => 'type',
		'database'  => null
	);

/**
 * Wether or not queries should be logged
 *
 * @var boolean
 * @access public
 */
	public $logQueries = true;

/**
 * Holds the various query logs for a request
 *
 * @var array
 * @access protected
 */
	protected $_queriesLog = array();

/**
 * Holds the query count
 *
 * @var integer
 * @access protected
 */
	protected $_queriesCnt = 0;

/**
 * Holds the queries time
 *
 * @var integer
 * @access protected
 */
	protected $_queriesTime = null;

/**
 * The maximum amount of queries that may be logged
 *
 * @var integer
 * @access protected
 */
	protected $_queriesLogMax = 200;

/**
 * CouchDBSource Constructor
 *
 * @param array $config The configuration for the Datasource
 * @param boolean $autoConnect If true, automatically connects the Datasource
 *
 * @return void
 * @access public
 * @link http://api.cakephp.org/class/data-source#method-DataSource__construct
 */
	public function __construct($config = array(), $autoConnect = true){
		parent::__construct($config);
		if ($autoConnect) {
			$this->connect();
		}
	}

/**
 * (non-PHPdoc)
 * @see DataSource::setConfig()
 */
	public function setConfig($config = array()) {
		$translateConfig = array(
			'host'      => 'request.uri.host',
			'port'      => 'request.uri.port'
		);
		$translatedConfig = array();

		foreach ($config as $key => $value) {
			if (isset($translateConfig[$key])) {
				$translatedConfig = Set::insert($translatedConfig, $translateConfig[$key], $value);
				unset($config[$key]);
			}
		}
		$this->config = array_replace_recursive($this->_baseconfig, $this->config, $config, $translatedConfig);
	}

/**
 * Connects the Datasource
 *
 * @return boolean
 * @access public
 * @throws MissingConnectionException
 */
	public function connect() {
		if ($this->connected !== true) {
			try {
				$this->Socket = new HttpSocket($this->config);

				switch ($this->config['authType']) {
					case 'cookie':
						$this->connected = $this->_cookieAuth();
						break;
					case 'oauth':
						$this->connected = false;
						break;
					case 'basic':
					default:<commit_msg>added support for authless couches<commit_after><?php
/**
 * CouchDBSource Datasource file
 *
 * Provides a CouchDB Datasource for CakePHP
 *
 * @author maxwellium - https://github.com/maxwellium
 * @package CouchDB
 * @subpackage Model.Datassource
 * @filesource
 */
App::uses('HttpSocket', 'Network/Http');

/**
 * This is the CouchDB Datasource class
 *
 * @package CouchDB
 * @subpackage Model.Datasource
 */
class CouchDBSource extends DataSource {

/**
 * The Datasource's default configuration
 *
 * @var array
 * @access protected
 */
	protected $_baseconfig = array(
		'request'   => array(
			'uri' => array(
				'host' => 'localhost',
				'port' => 5984
			),
			'header'  => array(
				'Content-Type' => 'application/json'
			)
		),
		'authType'  => 'basic',
		'models'    => 'type',
		'database'  => null
	);

/**
 * Wether or not queries should be logged
 *
 * @var boolean
 * @access public
 */
	public $logQueries = true;

/**
 * Holds the various query logs for a request
 *
 * @var array
 * @access protected
 */
	protected $_queriesLog = array();

/**
 * Holds the query count
 *
 * @var integer
 * @access protected
 */
	protected $_queriesCnt = 0;

/**
 * Holds the queries time
 *
 * @var integer
 * @access protected
 */
	protected $_queriesTime = null;

/**
 * The maximum amount of queries that may be logged
 *
 * @var integer
 * @access protected
 */
	protected $_queriesLogMax = 200;

/**
 * CouchDBSource Constructor
 *
 * @param array $config The configuration for the Datasource
 * @param boolean $autoConnect If true, automatically connects the Datasource
 *
 * @return void
 * @access public
 * @link http://api.cakephp.org/class/data-source#method-DataSource__construct
 */
	public function __construct($config = array(), $autoConnect = true){
		parent::__construct($config);
		if ($autoConnect) {
			$this->connect();
		}
	}

/**
 * (non-PHPdoc)
 * @see DataSource::setConfig()
 */
	public function setConfig($config = array()) {
		$translateConfig = array(
			'host'      => 'request.uri.host',
			'port'      => 'request.uri.port'
		);
		$translatedConfig = array();

		foreach ($config as $key => $value) {
			if (isset($translateConfig[$key])) {
				$translatedConfig = Set::insert($translatedConfig, $translateConfig[$key], $value);
				unset($config[$key]);
			}
		}
		$this->config = array_replace_recursive($this->_baseconfig, $this->config, $config, $translatedConfig);
	}

/**
 * Connects the Datasource
 *
 * @return boolean
 * @access public
 * @throws MissingConnectionException
 */
	public function connect() {
		if ($this->connected !== true) {
			try {
				$this->Socket = new HttpSocket($this->config);

				switch ($this->config['authType']) {
					case 'cookie':
						$this->connected = $this->_cookieAuth();
						break;
					case 'oauth':
						$this->connected = false;
						break;
					case 'none':
						$this->connected = true;
						break;
					case 'basic':
					default:
{'fraction_non_alphanumeric': 0.15362018316917228, 'fraction_numerical': 0.002764817694833247, 'mean_word_length': 3.3948367501898256, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 2, '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': '1666727', 'n_tokens_mistral': 2052, 'n_tokens_neox': 1827, 'n_words': 470}