<commit_before><?php

// (c) 2016 <NAME> <<EMAIL>>

// This script should be included and not called directly.
// However, it is no security issue if it is called directly, because it only
// contains functions (thus, no code is executed)

require_once 'json_file.php';
require_once 'result_helper.php';
require_once 'type_definitions.php';


/**
 * Compile a DataSet object containing info about all databases and uploads
 * of a given project, as well as the project's name.
 *
 * @param $authToken AuthToken
 * @return Result An object with 'data' set to a DataSet object.
 */
function project_info ($authToken) {
	$result = new Result();
	$result->data = new DataSet();

	$dbDir = $authToken->projectDir . '/databases';
	$uploadDir = $authToken->projectDir . '/uploads';

	// Project name
	$result->data->name = $authToken->projectName;

	// Find databases belonging to the project
	$dbStat = readDirOfDatabases($dbDir);
	if ($dbStat->success === true) {
		$result->data->databases = $dbStat->data;
	} else {
		return $dbStat;
	}

	// Find uploads belonging to the project
	$uploadsStat = readDirOfUploads($uploadDir);
	if ($uploadsStat->success === true) {
		$result->data->uploads = $uploadsStat->data;
	} else {
		return $uploadsStat;
	}

	$result->success = true;

	// fake data
	$result->data->uploads[0]->sessions =
		$result->data->databases[2]->sessions;
	// end fake data

	return $result;
}

function readDirOfUploads ($directory) {
	$dirHandle = dir($directory);

	if ($dirHandle === false || is_null($dirHandle)) {
		return negativeResult(
			'LIST_DIR_FAILED',
			'Failed to read directory of uploads.'
		);
	}

	$result = array();

	// Each directory corresponds to one upload. The dir's name is
	// expected to be a UUIDv4 (which however is not verified).
	while (false !== ($entry = $dirHandle->read())) {
		if ($entry === '.' || $entry === '..') {
			continue;
		}

		$upload = new Upload();

		$upload->uuid = $entry;

		// Read modification time
		$stat = stat($directory . '/' . $entry);
		if ($stat === false) {
			return negativeResult(
				'STAT_UPLOAD_DIR_FAILED',
				'Failed to read an upload directory.'
			);
		}

		$upload->date = date("M d, Y H:i T", $stat['mtime']);

		// Read name and sessions of the database contained in the upload
		$upload->name = '';
		$upload->sessions = array();

		$result[] = $upload;
	}

	return positiveResult($result);
}


/**
 * Read the project databases dir and look for emuDBs inside it. Every emuDB is
 * read and an array of Database objects is returned.
 *
 * @param $directory string The directory to traverse.
 * @returns Result An object with 'data' set to an array of Database objects.
 */
function readDirOfDatabases ($directory) {
	$dirHandle = dir($directory);

	if ($dirHandle === false || is_null($dirHandle)) {
		return negativeResult(
			'LIST_DIR_FAILED',
			'Failed to read directory of databases.'
		);
	}

	$result = array();

	while (false !== ($entry = $dirHandle->read())) {
		// Directories whose name ends in _emuDB are a database.
		// Everything else is ignored.
		if (substr($entry, -6) === "_emuDB") {
			$dbStat = readDatabase($directory . '/' . $entry);

			if ($dbStat->success === true) {
				$result[] = $dbStat->data;
			} else {
				return $dbStat;
			}
		}
	}

	return positiveResult($result);
}

/**
 * Read a database dir and look for sessions, bundle lists, and a DBconfig
 * file inside it. A Database object is returned.
 *
 * @param $directory string The directory to traverse.
 * @returns Result An object with 'data' set to a Database object
 */
function readDatabase ($directory) {
	$db = new Database();<commit_msg>read name and session list of uploaded databases<commit_after><?php

// (c) 2016 <NAME> <<EMAIL>>

// This script should be included and not called directly.
// However, it is no security issue if it is called directly, because it only
// contains functions (thus, no code is executed)

require_once 'json_file.php';
require_once 'result_helper.php';
require_once 'type_definitions.php';


/**
 * Compile a DataSet object containing info about all databases and uploads
 * of a given project, as well as the project's name.
 *
 * @param $authToken AuthToken
 * @return Result An object with 'data' set to a DataSet object.
 */
function project_info ($authToken) {
	$result = new Result();
	$result->data = new DataSet();

	$dbDir = $authToken->projectDir . '/databases';
	$uploadDir = $authToken->projectDir . '/uploads';

	// Project name
	$result->data->name = $authToken->projectName;

	// Find databases belonging to the project
	$dbStat = readDirOfDatabases($dbDir);
	if ($dbStat->success === true) {
		$result->data->databases = $dbStat->data;
	} else {
		return $dbStat;
	}

	// Find uploads belonging to the project
	$uploadsStat = readDirOfUploads($uploadDir);
	if ($uploadsStat->success === true) {
		$result->data->uploads = $uploadsStat->data;
	} else {
		return $uploadsStat;
	}

	$result->success = true;

	return $result;
}

function readDirOfUploads ($directory) {
	$dirHandle = dir($directory);

	if ($dirHandle === false || is_null($dirHandle)) {
		return negativeResult(
			'LIST_DIR_FAILED',
			'Failed to read directory of uploads.'
		);
	}

	$result = array();

	// Each directory corresponds to one upload. The dir's name is
	// expected to be a UUIDv4 (which however is not verified).
	while (false !== ($entry = $dirHandle->read())) {
		if ($entry === '.' || $entry === '..') {
			continue;
		}

		$upload = new Upload();

		$upload->uuid = $entry;

		// Read modification time
		$stat = stat($directory . '/' . $entry);
		if ($stat === false) {
			return negativeResult(
				'STAT_UPLOAD_DIR_FAILED',
				'Failed to read an upload directory.'
			);
		}

		$upload->date = date("M d, Y H:i T", $stat['mtime']);

		// Find the first entry in the data/ subdir. This is regarded the
		// database because data/ is expected to contain only one file anyway
		// (i.e. the database).

		$uploadDataDir = $directory . '/' . $entry . '/data';
		$uploadDataDirHandle = dir($uploadDataDir);
		if ($uploadDataDirHandle === false || is_null($uploadDataDirHandle)) {
			return negativeResult(
				'LIST_DIR_FAILED',
				'Failed to read data directory of an upload.'
			);
		}

		$databaseDir = $uploadDataDirHandle->read();
		if (substr($databaseDir, -6) !== '_emuDB') {
			return negativeResult(
				'INVALID_UPLOAD_DIR',
				'Found an upload directory that does not contain an emu'
				. ' speech database'
			);
		}

		$upload->name = substr($databaseDir, 0, -6);


		// Read the sessions contained in the upload
		$db = readDatabase($uploadDataDir . '/' . $databaseDir);
		if ($db->success !== true) {
			return $db;
		}

		$upload->sessions = $db->data->sessions;

		$result[] = $upload;
	}

	return positiveResult($result);
}


/**
 * Read the project databases dir and look for emuDBs inside it. Every emuDB is
 * read and an array of Database objects is returned.
 *
 * @param $directory string The directory to traverse.
 * @returns Result An object with 'data' set to an array of Database objects.
 */
function readDirOfDatabases ($directory) {
	$dirHandle = dir($directory);

	if ($dirHandle === false || is_null($dirHandle)) {
		return negativeResult(
			'LIST_DIR_FAILED',
			'Failed to read directory of databases.'
		);
	}

	$result = array();

	while (false !== ($entry = $dirHandle->read())) {
		// Directories whose name ends in _emuDB are a database.
		// Everything else is ignored.
		if (substr($entry, -6) === "_emuDB") {
			$dbStat = readDatabase($directory . '/' . $entry);

			if ($dbStat->success === true) {
				$result[] = $dbStat->data;
			} else {
				return $dbStat;
			}
		}
	}

	return positiveResult($result);
}

/**
 * Read a database dir and look for sessions, bundle lists, and a DBconfig
 * file inside it. A Database object is returned.
 *
 * @param $directory string The directory to read.
 * @returns Result An object with 'data' set to a Database object
 */
function readDatabase ($directory) {
	$db = new Database();
{'fraction_non_alphanumeric': 0.14498187726534184, 'fraction_numerical': 0.002124734408198975, 'mean_word_length': 3.5699600228440893, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 65, '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': '2209665', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2458, 'n_words': 826}