<commit_before><?php 
class Books_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function getBookInfoByISBN($isbn){
		$query_book = $this->db->query("SELECT * FROM book WHERE isbn10 = '$isbn' OR isbn13 = '$isbn' ");

		//if not exists , get data from douban
		if($query_book->num_rows() == 0){
			$book_info = $this->getDataFromDoubanByISBN($isbn);
			if($book_info == FALSE)
				return FALSE;

			if(trim($book_info['isbn13']) != $isbn AND trim($book_info['isbn10']) != $isbn)
				return FALSE;
			$this->saveBookInfoToDB($book_info);

		}

		$query_book = $this->db->query("SELECT book.* , 
			publisher.name AS publisher_name , publisher.id AS publisher_id
			FROM book 
			INNER JOIN publisher ON book.publisher_id = publisher.id 
			WHERE isbn10 = '$isbn' OR isbn13 = '$isbn'  ");

		$row = $query_book->first_row("array");
		$book_id = $row["id"];
		//author
		$query_author = $this->db->query("SELECT author.name AS author_name , author.id AS author_id 
			FROM book_author 
			INNER JOIN author ON book_author.author_id = author.id
			WHERE book_author.book_id = $book_id ");
		$result_author = $query_author->result("array");
		$row["authors"] = $result_author;
		//translator
		$query_translator = $this->db->query("SELECT author.name AS translator_name , author.id AS translator_id 
			FROM book_translator 
			INNER JOIN author ON book_translator.author_id = author.id 
			WHERE book_translator.book_id = $book_id ");
		$result_translator = $query_translator->result("array");
		$row["translators"] = $result_translator;
		//tag
		$query_tag = $this->db->query("SELECT tag.name AS tag_name , book_tag.tag_id AS tag_id 
			FROM book_tag 
			INNER JOIN tag ON book_tag.tag_id = tag.id
			WHERE book_tag.book_id = $book_id ");
		$result_tag = $query_tag->result("array");
		$row["tags"] = $result_tag;

		return $row;
	}

	private function saveBookInfoToDB($book_info){
		$publisher_id = $this->createPublisher($book_info["publisher"]);
		$book = array(
			'isbn10' => $book_info['isbn10'] ,
			'isbn13' => $book_info['isbn13'] ,
			'title' => $book_info['title'] ,
			'description' => $book_info['summary'] ,
			'isbn13' => $book_info['isbn13'] ,
			'publisher_id' => $publisher_id ,
			'douban_url' => $book_info['alternate'] ,

			'image_url' => $book_info['image'] ,
			'pages' => isset($book_info['pages'])?$book_info['pages']:"",
			'price' => isset($book_info['price'])?$book_info['price']:"" ,
			'binding' => isset($book_info['binding'])?$book_info['binding']:"" ,
			'pubdate' =>isset($book_info['pubdate'])?$book_info['pubdate']:"" ,

			'rate_score' => $book_info['rate']['average'] ,
			'rate_num' => $book_info["rate"]['numRaters'] ,
			);
		$insert_book = $this->db->insert( "book" , $book );
		$book_id = $this->db->insert_id();

		foreach ($book_info['authors'] as $key => $author) {
			$author_id = $this->createAuthor($author);
			$book_author_arr = array( 'book_id' => $book_id , 'author_id' => $author_id );
			$this->db->insert('book_author' , $book_author_arr);
		}
		foreach ($book_info['translators'] as $key => $author) {
			$author_id = $this->createAuthor($author);
			$book_author_arr = array( 'book_id' => $book_id , 'author_id' => $author_id );
			$this->db->insert('book_translator' , $book_author_arr);
		}
		foreach ($book_info['tags'] as $key => $tag) {
			$tag_id = $this->createTag($tag["name"]);
			$book_tag_arr = array( 'book_id' => $book_id , 'tag_id' => $tag_id , 'count' => $tag["count"] );
			$this->db->insert('book_tag' , $book_tag_arr);
		}
		
	}

	private function createAuthor($author_name){
		$query_author = $this->db->query("SELECT * FROM author WHERE name = '$author_name'");
		if($query_author->num_rows() == 0){
			$insert_author = $this->db->query("INSERT INTO author(name) VALUE('$author_name')");
			$author_id = $this->db->insert_id();<commit_msg>fix tag error bug<commit_after><?php 
class Books_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function getBookInfoByISBN($isbn){
		$query_book = $this->db->query("SELECT * FROM book WHERE isbn10 = '$isbn' OR isbn13 = '$isbn' ");

		//if not exists , get data from douban
		if($query_book->num_rows() == 0){
			$book_info = $this->getDataFromDoubanByISBN($isbn);
			if($book_info == FALSE)
				return FALSE;

			if(trim($book_info['isbn13']) != $isbn AND trim($book_info['isbn10']) != $isbn)
				return FALSE;
			$this->saveBookInfoToDB($book_info);

		}

		$query_book = $this->db->query("SELECT book.* , 
			publisher.name AS publisher_name , publisher.id AS publisher_id
			FROM book 
			INNER JOIN publisher ON book.publisher_id = publisher.id 
			WHERE isbn10 = '$isbn' OR isbn13 = '$isbn'  ");

		$row = $query_book->first_row("array");
		$book_id = $row["id"];
		//author
		$query_author = $this->db->query("SELECT author.name AS author_name , author.id AS author_id 
			FROM book_author 
			INNER JOIN author ON book_author.author_id = author.id
			WHERE book_author.book_id = $book_id ");
		$result_author = $query_author->result("array");
		$row["authors"] = $result_author;
		//translator
		$query_translator = $this->db->query("SELECT author.name AS translator_name , author.id AS translator_id 
			FROM book_translator 
			INNER JOIN author ON book_translator.author_id = author.id 
			WHERE book_translator.book_id = $book_id ");
		$result_translator = $query_translator->result("array");
		$row["translators"] = $result_translator;
		//tag
		$query_tag = $this->db->query("SELECT tag.name AS tag_name , book_tag.tag_id AS tag_id 
			FROM book_tag 
			INNER JOIN tag ON book_tag.tag_id = tag.id
			WHERE book_tag.book_id = $book_id ");
		$result_tag = $query_tag->result("array");
		$row["tags"] = $result_tag;

		return $row;
	}

	private function saveBookInfoToDB($book_info){
		$publisher_id = $this->createPublisher($book_info["publisher"]);
		$book = array(
			'isbn10' => $book_info['isbn10'] ,
			'isbn13' => $book_info['isbn13'] ,
			'title' => $book_info['title'] ,
			'description' => $book_info['summary'] ,
			'isbn13' => $book_info['isbn13'] ,
			'publisher_id' => $publisher_id ,
			'douban_url' => $book_info['alternate'] ,

			'image_url' => $book_info['image'] ,
			'pages' => isset($book_info['pages'])?$book_info['pages']:"",
			'price' => isset($book_info['price'])?$book_info['price']:"" ,
			'binding' => isset($book_info['binding'])?$book_info['binding']:"" ,
			'pubdate' =>isset($book_info['pubdate'])?$book_info['pubdate']:"" ,

			'rate_score' => $book_info['rate']['average'] ,
			'rate_num' => $book_info["rate"]['numRaters'] ,
			);
		$insert_book = $this->db->insert( "book" , $book );
		$book_id = $this->db->insert_id();

		foreach ($book_info['authors'] as $key => $author) {
			$author_id = $this->createAuthor($author);
			$book_author_arr = array( 'book_id' => $book_id , 'author_id' => $author_id );
			$this->db->insert('book_author' , $book_author_arr);
		}
		foreach ($book_info['translators'] as $key => $author) {
			$author_id = $this->createAuthor($author);
			$book_author_arr = array( 'book_id' => $book_id , 'author_id' => $author_id );
			$this->db->insert('book_translator' , $book_author_arr);
		}
		foreach ($book_info['tags'] as $key => $tag){
			$book_info['tags'][$key] = (int)$tag;
		}
		array_unique($book_info['tags']);
		foreach ($book_info['tags'] as $key => $tag) {
			$tag_id = $this->createTag($tag["name"]);
			$book_tag_arr = array( 'book_id' => $book_id , 'tag_id' => (int)$tag_id , 'count' => (int)$tag["count"] );
			$this->db->insert('book_tag' , $book_tag_arr);
		}
		
	}

	private function createAuthor($author_name){
		$query_author = $this->db->query("SELECT * FROM author WHERE name = '$author_name'");
		if($query_author->num_rows() == 0){
			$insert_author = $this->db->query("INSERT INTO author(name) VALUE('$author_name')");
			$author_id = $this->db->insert_id();
{'fraction_non_alphanumeric': 0.19105846388995032, 'fraction_numerical': 0.006623360081518278, 'mean_word_length': 4.756598240469208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 128, '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': '4103511', 'n_tokens_mistral': 3201, 'n_tokens_neox': 2861, 'n_words': 514}