<commit_before><?php
/**
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */

namespace li3_bot\models;

use DirectoryIterator;
use Exception;

class Log extends \lithium\core\StaticObject {

	public static $path = null;

	protected static $_pattern = null;

	public static function __init() {
		static::$path = $path = LITHIUM_APP_PATH . '/resources/bot/logs';
		static::$_pattern = '/^(?P<time>\d+:\d+(:\d+)?) : (?P<user>[^\s]+) : (?P<message>.*)/';

		if (!is_dir($path)) {
			throw new Exception("Logs directory at `{$path}` doesn't exist");
		}
		if (!is_readable($path)) {
			throw new Exception("Logs directory at `{$path}` is not readable");
		}
		if (!is_writable($path)) {
			throw new Exception("Logs directory at `{$path}` is not writable");
		}
	}

	public static function save($data = null) {
		$dir = static::path($data['channel']);
		$path = static::path($data['channel'], date('Y-m-d'));
		if (!is_dir($dir)) {
			mkdir($dir);
		}
		$fp = !file_exists($path) ? fopen($path, 'x+') : fopen($path, 'a+');

		if (!is_resource($fp)) {
			return false;
		}

		$log = date('H:i:s') . " : {$data['user']} : {$data['message']}\n";
		fwrite($fp, $log);
		fclose($fp);
		return $data;
	}

	public static function read($channel, $date) {
		$path = static::path($channel, $date);

		if (!static::exists($channel, $date)) {
			return array();
		}
		$log = array();
		$fp = @fopen($path, 'r');

		if (!is_resource($fp)) {
			return $log;
		}

		while ($line = fgets($fp)) {
			if (preg_match(static::$_pattern, $line, $matches)) {
				$log[] = $matches;<commit_msg>Refactoring appending to log.<commit_after><?php
/**
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */

namespace li3_bot\models;

use DirectoryIterator;
use Exception;

class Log extends \lithium\core\StaticObject {

	public static $path = null;

	protected static $_pattern = null;

	public static function __init() {
		static::$path = $path = LITHIUM_APP_PATH . '/resources/bot/logs';
		static::$_pattern = '/^(?P<time>\d+:\d+(:\d+)?) : (?P<user>[^\s]+) : (?P<message>.*)/';

		if (!is_dir($path)) {
			throw new Exception("Logs directory at `{$path}` doesn't exist");
		}
		if (!is_readable($path)) {
			throw new Exception("Logs directory at `{$path}` is not readable");
		}
		if (!is_writable($path)) {
			throw new Exception("Logs directory at `{$path}` is not writable");
		}
	}

	public static function save($data = null) {
		$dir = static::path($data['channel']);
		$path = static::path($data['channel'], date('Y-m-d'));

		if (!is_dir($dir)) {
			mkdir($dir);
		}

		$line = date('H:i:s') . " : {$data['user']} : {$data['message']}\n";
		file_put_contents($path, $line, FILE_APPEND);

		return $data;
	}

	public static function read($channel, $date) {
		$path = static::path($channel, $date);

		if (!static::exists($channel, $date)) {
			return array();
		}
		$log = array();
		$fp = @fopen($path, 'r');

		if (!is_resource($fp)) {
			return $log;
		}

		while ($line = fgets($fp)) {
			if (preg_match(static::$_pattern, $line, $matches)) {
				$log[] = $matches;
{'fraction_non_alphanumeric': 0.21071535468422628, 'fraction_numerical': 0.00299311583358276, 'mean_word_length': 3.9732142857142856, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '4389551', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1181, 'n_words': 280}