<commit_before><?php namespace HashOver;

// Copyright (C) 2010-2019 <NAME>
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class Database extends Secrets
{
	protected $setup;
	protected $database;

	// Initial comment data
	protected $commentsTable = array (
		'domain' => '',
		'thread' => '',
		'comment' => '',
		'body' => '',
		'status' => '',
		'date' => '',
		'name' => '',
		'password' => '',
		'login_id' => '',
		'email' => '',
		'encryption' => '',
		'email_hash' => '',
		'notifications' => '',
		'website' => '',
		'ipaddr' => '',
		'likes' => 0,
		'dislikes' => 0
	);

	public function __construct (Setup $setup)
	{
		// Store parameters as properties
		$this->setup = $setup;

		try {
		// Check if database type is SQLite
		if ($this->databaseType === 'sqlite') {
			// If so, construct SQLite file name
			$file = sprintf ('%s/%s.sqlite',
				$setup->commentsRoot, $this->databaseName
			);

			// Instantiate an SQLite data object
			$this->database = new \PDO ('sqlite:' . $file);

			// And change file permissions
			@chmod ($file, 0600);
		} else {
			// If not, create SQL server connection statement
			$connection = implode (';', array (
				'host=' . $this->databaseHost,
				'dbname=' . $this->databaseName,
				'charset=' . $this->databaseCharset
			));

			// And create SQL server data object
			$this->database = new \PDO (
				// PDO driver and connection details
				$this->databaseType . ':' . $connection,

				// Database user as configured
				$this->databaseUser,

				// Database password as configured
				$this->databasePassword,

				// We want the number of found (matched) rows,
				// not the number of changed rows
				array (
					\PDO::MYSQL_ATTR_FOUND_ROWS => true
				)
			);
		}
		} catch (\PDOException $error) {
			throw new \Exception ($error->getMessage ());
		}
	}

	// Prepares and executes an SQL statement
	protected function executeStatement ($statement, $data = null)
	{
		try {
		// Prepare statement
		$prepare = $this->database->prepare ($statement);

		// Check if prepare was successful
		if ($prepare !== false) {
			// If so, attempt to execute statement
			$execute = $prepare->execute ($data);

			// And return statement object if execute was successful
			if ($execute !== false) {
				return $prepare;
			}
		}
		} catch (\PDOException $error) {
			throw new \Exception ($error->getMessage ());
		}

		return false;
	}

	// Returns a given thread or thread from setup
	protected function getCommentThread ($thread)
	{
		// Return thread from setup if thread is auto
		if ($thread === 'auto') {
			return $this->setup->threadName;
		}

		// Otherwise, return given thread
		return $thread;
	}

	// Creates table creation statement from array
	protected function creationArray (array $columns)
	{
		// Initial statement
		$statement = array ();<commit_msg>We don't need to rethrow PDO exceptions<commit_after><?php namespace HashOver;

// Copyright (C) 2010-2019 <NAME>ull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class Database extends Secrets
{
	protected $setup;
	protected $database;

	// Initial comment data
	protected $commentsTable = array (
		'domain' => '',
		'thread' => '',
		'comment' => '',
		'body' => '',
		'status' => '',
		'date' => '',
		'name' => '',
		'password' => '',
		'login_id' => '',
		'email' => '',
		'encryption' => '',
		'email_hash' => '',
		'notifications' => '',
		'website' => '',
		'ipaddr' => '',
		'likes' => 0,
		'dislikes' => 0
	);

	public function __construct (Setup $setup)
	{
		// Store parameters as properties
		$this->setup = $setup;

		// Check if database type is SQLite
		if ($this->databaseType === 'sqlite') {
			// If so, construct SQLite file name
			$file = sprintf ('%s/%s.sqlite',
				$setup->commentsRoot, $this->databaseName
			);

			// Instantiate an SQLite data object
			$this->database = new \PDO ('sqlite:' . $file);

			// And change file permissions
			@chmod ($file, 0600);
		} else {
			// If not, create SQL server connection statement
			$connection = implode (';', array (
				'host=' . $this->databaseHost,
				'dbname=' . $this->databaseName,
				'charset=' . $this->databaseCharset
			));

			// And create SQL server data object
			$this->database = new \PDO (
				// PDO driver and connection details
				$this->databaseType . ':' . $connection,

				// Database user as configured
				$this->databaseUser,

				// Database password as configured
				$this->databasePassword,

				// We want the number of found (matched) rows,
				// not the number of changed rows
				array (
					\PDO::MYSQL_ATTR_FOUND_ROWS => true
				)
			);
		}
	}

	// Prepares and executes an SQL statement
	protected function executeStatement ($statement, $data = null)
	{
		// Prepare statement
		$prepare = $this->database->prepare ($statement);

		// Check if prepare was successful
		if ($prepare !== false) {
			// If so, attempt to execute statement
			$execute = $prepare->execute ($data);

			// And return statement object if execute was successful
			if ($execute !== false) {
				return $prepare;
			}
		}

		return false;
	}

	// Returns a given thread or thread from setup
	protected function getCommentThread ($thread)
	{
		// Return thread from setup if thread is auto
		if ($thread === 'auto') {
			return $this->setup->threadName;
		}

		// Otherwise, return given thread
		return $thread;
	}

	// Creates table creation statement from array
	protected function creationArray (array $columns)
	{
		// Initial statement
		$statement = array ();
{'fraction_non_alphanumeric': 0.13813946631284094, 'fraction_numerical': 0.004422821760283061, 'mean_word_length': 2.9976428992339423, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6246566', 'n_tokens_mistral': 2286, 'n_tokens_neox': 1970, 'n_words': 718}