<commit_before><?php

/**
* Implementation of keystore class using files. For development purposes only.
*/
class Gwilym_KeyStore_File implements Gwilym_KeyStore_Interface
{
	/**
	* List of characters disallowed as key names when this implementation is used.
	*
	* @var string
	*/
	protected static $_badCharacters = array(
		"\\",
		"/",
		":",
		"*",
		"?",
		'"',
		"<",
		">",
		"|",
		"\0"
	);

	public static $dir;

	protected static function patternToRegularExpresion ($pattern)
	{
		// todo: this is really basic and needs improving
		return '#^' . str_replace('*', '.*', $pattern) . '$#';
	}

	protected static function testFilenameAgainstPattern ($filename, $pattern)
	{
		$pattern = self::patternToRegularExpresion($pattern);
		return (bool)preg_match($pattern, $filename);
	}

	public static function keyToFilename ($key)
	{
		$filename = str_replace(self::$_badCharacters, '', $key);
		if ($filename !== $key)
		{
			throw new Gwilym_KeyStore_File_Exception_InvalidKeyName($key);
		}

		if (!file_exists(self::$dir))
		{
			if (!mkdir(self::$dir))
			{
				throw new Gwilym_KeyStore_File_Exception_DirectoryCreateError(self::$dir);
			}
		}

		return self::$dir . '/' . $filename;
	}

	public static function set ($key, $value)
	{
		$file = self::keyToFilename($key);

		if (file_exists($file) && !is_writable($file))
		{
			throw new Gwilym_KeyStore_File_Exception_FileNotWritable($file);
		}

		if (!file_put_contents($file, $value))
		{
			throw new Gwilym_KeyStore_File_Exception_FileWriteError($file);
		}

		return true;
	}

	public static function get ($key)
	{
		$file = self::keyToFilename($key);

		if (!file_exists($file))
		{
			throw new Gwilym_KeyStore_Exception_KeyDoesntExist($key);
		}

		if (!is_readable($file))
		{
			throw new Gwilym_KeyStore_File_Exception_FileNotReadable($file);
		}

		return file_get_contents($file);
	}

	public static function exists ($key)
	{
		$file = self::keyToFilename($key);
		return file_exists($file);
	}

	public static function delete ($key)
	{
		$file = self::keyToFilename($key);

		if (!file_exists($file))
		{
			throw new Gwilym_KeyStore_Exception_KeyDoesntExist($key);
		}

		if (!unlink($file))
		{
			throw new Gwilym_KeyStore_File_Exception_FileDeleteError();
		}

		return true;
	}

	public static function multiSet ($keyValues)
	{
		throw new Exception();
	}

	public static function multiGet ($pattern)
	{
		$dir = dir(self::$dir);
		$results = array();
		while ($file = $dir->read())
		{
			if ($file == '.' || $file == '..')
			{<commit_msg>removing some exceptions in the keystore in favour of returning false<commit_after><?php

/**
* Implementation of keystore class using files. For development purposes only.
*/
class Gwilym_KeyStore_File implements Gwilym_KeyStore_Interface
{
	/**
	* List of characters disallowed as key names when this implementation is used.
	*
	* @var string
	*/
	protected static $_badCharacters = array(
		"\\",
		"/",
		":",
		"*",
		"?",
		'"',
		"<",
		">",
		"|",
		"\0"
	);

	public static $dir;

	protected static function patternToRegularExpresion ($pattern)
	{
		// todo: this is really basic and needs improving
		return '#^' . str_replace('*', '.*', $pattern) . '$#';
	}

	protected static function testFilenameAgainstPattern ($filename, $pattern)
	{
		$pattern = self::patternToRegularExpresion($pattern);
		return (bool)preg_match($pattern, $filename);
	}

	public static function keyToFilename ($key)
	{
		$filename = str_replace(self::$_badCharacters, '', $key);
		if ($filename !== $key)
		{
			throw new Gwilym_KeyStore_File_Exception_InvalidKeyName($key);
		}

		if (!file_exists(self::$dir))
		{
			if (!mkdir(self::$dir))
			{
				throw new Gwilym_KeyStore_File_Exception_DirectoryCreateError(self::$dir);
			}
		}

		return self::$dir . '/' . $filename;
	}

	public static function set ($key, $value)
	{
		$file = self::keyToFilename($key);

		if (file_exists($file) && !is_writable($file))
		{
			throw new Gwilym_KeyStore_File_Exception_FileNotWritable($file);
		}

		if (!file_put_contents($file, $value))
		{
			throw new Gwilym_KeyStore_File_Exception_FileWriteError($file);
		}

		return true;
	}

	public static function get ($key)
	{
		$file = self::keyToFilename($key);

		if (!file_exists($file))
		{
			return false;
		}

		if (!is_readable($file))
		{
			throw new Gwilym_KeyStore_File_Exception_FileNotReadable($file);
		}

		return file_get_contents($file);
	}

	public static function exists ($key)
	{
		$file = self::keyToFilename($key);
		return file_exists($file);
	}

	public static function delete ($key)
	{
		$file = self::keyToFilename($key);

		if (!file_exists($file))
		{
			return false;
		}

		if (!unlink($file))
		{
			throw new Gwilym_KeyStore_File_Exception_FileDeleteError();
		}

		return true;
	}

	public static function multiSet ($keyValues)
	{
		throw new Exception();
	}

	public static function multiGet ($pattern)
	{
		$dir = dir(self::$dir);
		$results = array();
		while ($file = $dir->read())
		{
			if ($file == '.' || $file == '..')
			{
{'fraction_non_alphanumeric': 0.15323224261771748, 'fraction_numerical': 0.0003990422984836393, 'mean_word_length': 4.09969481180061, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, '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': '941933', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1740, 'n_words': 368}