<commit_before><?php
class Model {
	private $controller;

	public $models = array();

	public $modelName;

	public $validate = array();
	public $validationErrors = array();

	public function __construct(&$controller) {
		$this->controller = $controller;
		$this->modelName = ucfirst(Inflector::singularize($controller->params["controller"]));
	}

	public function query($url, $method = "GET", $data = null, $jsonDecodeResponse = true) {
		$handle = curl_init();

		curl_setopt_array($handle, array(
			CURLOPT_CUSTOMREQUEST => $method,
			CURLOPT_HEADER => true,
			CURLOPT_NOBODY => $method === "HEAD",
			CURLOPT_POSTFIELDS => $data,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_URL => $url
		));

		$rawResponse = curl_exec($handle);
		curl_close($handle);

		// Split response text at empty lines to separate headers and data
		$response = explode("\r\n\r\n", $rawResponse);

		// Get most recent header from all received headers
		$rawHeader = explode("\r\n", $response[count($response) - 2]);

		// Get status code and header fields
		$header = array();
		foreach ($rawHeader as $i => $line) {
			if ($i === 0) {
				 $rawStatus = explode(" ", $line);
				 $status = (integer) $rawStatus[1];
				 $header["Status"] = $line;
			} else {
				list($fieldName, $fieldValue) = explode(": ", $line, 2);
				$header[$fieldName] = $fieldValue;
			}
		}

		// Get data
		$data = $response[count($response) - 1];

		if ($jsonDecodeResponse) {
			$data = json_decode($data, true);
		}

		return array($status, $header, $data);
	}

	public function getDocument($documentId, $method = "GET") {
		$url = DATABASE_HOST . "/" . rawurlencode(DATABASE_NAME) . "/" . rawurlencode($documentId);
		list($status, $header, $data) = $this->query($url, $method);

		if ($status === 200) {
			return array($header, $data);
		} elseif ($status === 404) {
			throw new Exception("The document you try to fetch does not exist.");
		} else {
			throw new Exception("The document could not be fetched (Status $status).");
		}
	}

	public function storeDocument($data, $options) {
		if (!isset($data["_id"])) {
			throw new Exception("No _id provided. Can't store document without _id.");
		}

		if (!isset($options["fieldList"]) || empty($options["fieldList"])) {
			throw new Exception("Please whitelist fields. Aborted storing document.");
		}

		// Abort if non-whitelisted fields are present
		foreach ($data as $fieldName => $value) {
			if (!in_array($fieldName, $options["fieldList"])) {
				throw new Exception("Non-whitelisted field '$fieldName' is present. Aborted storing document.");
			}
		}

		// Add type field
		if (!isset($data["type"])) {
			$data["type"] = lcfirst($this->modelName);
		}

		// Validate data
		$this->validationErrors = $this->validate($data);
		$this->controller->validationErrors = $this->validationErrors;

		if (empty($this->validationErrors)) {
			$url = DATABASE_HOST . "/" . rawurlencode(DATABASE_NAME) . "/" . rawurlencode($data["_id"]);

			list($status, $header, $data) = $this->query($url, "PUT", json_encode($data));

			if ($status === 201) {
				return array($header, $data);
			} elseif ($status === 409) {
				throw new Exception("A document with this _id already exists.");
			} else {
				throw new Exception("Document could not be saved (Status $status).");
			}
		} else {
			throw new Exception("Data is not valid. Aborted storing document.");
		}
	}

	public function updateDocument($documentId, $data, $options = array()) {
		$data["_id"] = $documentId;
		$data["_rev"] = $this->getRevision($documentId);

		$options["fieldList"][] = "_id";
		$options["fieldList"][] = "_rev";

		return $this->storeDocument($data, $options);
	}

	public function deleteDocument($documentId) {
		$revision = $this->getRevision($documentId);

		$url = DATABASE_HOST . "/" . rawurlencode(DATABASE_NAME) . "/" . rawurlencode($documentId) . "?rev=" . rawurlencode($revision);
		return $this->query($url, "DELETE");
	}

	/**
	 * Gets the revision by reading a document's e-tag.
	 *
	 * @param string $documentId
	 * @return string Document revision ("_rev")
	 */
	public function getRevision($documentId) {
		list($header) = $this->getDocument($documentId, "HEAD");

		if (isset($header["Etag"])) {
			if (preg_match('/^"([^"]+)"$/', $header["Etag"], $eTag) === 1) {
				return $eTag[1];
			} else {
				throw new Exception("Could not parse e-tag from response header. Getting the latest revision failed.");
			}
		} else {
			throw new Exception("E-tag is not present in response header. Getting the latest revision failed.");
		}<commit_msg>Added documentExists(). Added revision argument to deleteDocument(). Changed order of arguments for storeDocument().<commit_after><?php
class Model {
	private $controller;

	public $models = array();

	public $modelName;

	public $validate = array();
	public $validationErrors = array();

	public function __construct(&$controller) {
		$this->controller = $controller;
		$this->modelName = ucfirst(Inflector::singularize($controller->params["controller"]));
	}

	public function query($url, $method = "GET", $data = null, $jsonDecodeResponse = true) {
		$handle = curl_init();

		curl_setopt_array($handle, array(
			CURLOPT_CUSTOMREQUEST => $method,
			CURLOPT_HEADER => true,
			CURLOPT_NOBODY => $method === "HEAD",
			CURLOPT_POSTFIELDS => $data,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_URL => $url
		));

		$rawResponse = curl_exec($handle);
		curl_close($handle);

		// Split response text at empty lines to separate headers and data
		$response = explode("\r\n\r\n", $rawResponse);

		// Get most recent header from all received headers
		$rawHeader = explode("\r\n", $response[count($response) - 2]);

		// Get status code and header fields
		$header = array();
		foreach ($rawHeader as $i => $line) {
			if ($i === 0) {
				 $rawStatus = explode(" ", $line);
				 $status = (integer) $rawStatus[1];
				 $header["Status"] = $line;
			} else {
				list($fieldName, $fieldValue) = explode(": ", $line, 2);
				$header[$fieldName] = $fieldValue;
			}
		}

		// Get data
		$data = $response[count($response) - 1];

		if ($jsonDecodeResponse) {
			$data = json_decode($data, true);
		}

		return array($status, $header, $data);
	}

	public function getDocument($documentId, $method = "GET") {
		$url = DATABASE_HOST . "/" . rawurlencode(DATABASE_NAME) . "/" . rawurlencode($documentId);
		list($status, $header, $data) = $this->query($url, $method);

		if ($status === 200) {
			return array($header, $data);
		} elseif ($status === 404) {
			throw new Exception("The document you try to fetch does not exist.");
		} else {
			throw new Exception("The document could not be fetched (Status $status).");
		}
	}

	public function documentExists($documentId) {
		try {
			$this->getDocument($documentId, "HEAD");
			return true;
		} catch (Exception $exception) {
			return false;
		}
	}

	public function storeDocument($documentId, $data, $options) {
		if (empty($options["fieldList"])) {
			throw new Exception("Please whitelist fields. Aborted storing document.");
		}

		// Abort if non-whitelisted fields are present
		foreach ($data as $fieldName => $value) {
			if (!in_array($fieldName, $options["fieldList"])) {
				throw new Exception("Non-whitelisted field '$fieldName' is present. Aborted storing document.");
			}
		}

		// Add _id field
		$data["_id"] = $documentId;

		// Add type field
		if (!isset($data["type"])) {
			$data["type"] = lcfirst($this->modelName);
		}

		// Validate data
		$this->validationErrors = $this->validate($data);
		$this->controller->validationErrors = $this->validationErrors;

		if (empty($this->validationErrors)) {
			$url = DATABASE_HOST . "/" . rawurlencode(DATABASE_NAME) . "/" . rawurlencode($data["_id"]);

			list($status, $header, $data) = $this->query($url, "PUT", json_encode($data));

			if ($status === 201) {
				return array($header, $data);
			} elseif ($status === 409) {
				throw new Exception("A document with this _id already exists.");
			} else {
				throw new Exception("Document could not be saved (Status $status).");
			}
		} else {
			throw new Exception("Data is not valid. Aborted storing document.");
		}
	}

	public function updateDocument($documentId, $data, $options = array()) {
		$data["_rev"] = $this->getRevision($documentId);
		$options["fieldList"][] = "_rev";

		return $this->storeDocument($documentId, $data, $options);
	}

	public function deleteDocument($documentId, $revision = null) {
		if ($revision === null) {
			$revision = $this->getRevision($documentId);
		}

		$url = DATABASE_HOST . "/" . rawurlencode(DATABASE_NAME) . "/" . rawurlencode($documentId) . "?rev=" . rawurlencode($revision);
		return $this->query($url, "DELETE");
	}

	/**
	 * Gets the revision by reading a document's e-tag.
	 *
	 * @param string $documentId
	 * @return string Document revision ("_rev")
	 */
	public function getRevision($documentId) {
		list($header) = $this->getDocument($documentId, "HEAD");

		if (isset($header["Etag"])) {
			if (preg_match('/^"([^"]+)"$/', $header["Etag"], $eTag) === 1) {
				return $eTag[1];
			} else {
				throw new Exception("Could not parse e-tag from response header. Getting the latest revision failed.");
			}
		} else {
			throw new Exception("E-tag is not present in response header. Getting the latest revision failed.");
		}
{'fraction_non_alphanumeric': 0.17359991400623453, 'fraction_numerical': 0.004084703858970224, 'mean_word_length': 4.00484131253362, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 54, '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': '2456121', 'n_tokens_mistral': 3345, 'n_tokens_neox': 2907, 'n_words': 770}