<commit_before><?
/*
    ***** BEGIN LICENSE BLOCK *****
    
    This file is part of the Zotero Data Server.
    
    Copyright © 2010 Center for History and New Media
                     George Mason University, Fairfax, Virginia, USA
                     http://zotero.org
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    ***** END LICENSE BLOCK *****
*/

class Zotero_Search {
	private $id;
	private $libraryID;
	private $key;
	private $name;
	private $dateAdded;
	private $dateModified;
	
	private $loaded;
	private $changed;
	
	private $conditions = array();
	private $maxSearchConditionID;
	//private $sql;
	//private $sqlParams;
	
	public function __construct() {
		$numArgs = func_num_args();
		if ($numArgs) {
			throw new Exception("Constructor doesn't take any parameters");
		}
	}
	
	
	public function __get($field) {
		if (($this->id || $this->key) && !$this->loaded) {
			$this->load(true);
		}
		
		if (!property_exists('Zotero_Search', $field)) {
			throw new Exception("Zotero_Search property '$field' doesn't exist");
		}
		
		return $this->$field;
	}
	
	
	public function __set($field, $value) {
		switch ($field) {
			case 'id':
			case 'libraryID':
			case 'key':
				if ($this->loaded) {
					throw new Exception("Cannot set $field after search is already loaded");
				}
				$this->checkValue($field, $value);
				$this->$field = $value;
				return;
		}
		
		if ($this->id || $this->key) {
			if (!$this->loaded) {
				$this->load(true);
			}
		}
		else {
			$this->loaded = true;
		}
		
		$this->checkValue($field, $value);
		
		if ($this->$field != $value) {
			$this->changed = true;
			$this->$field = $value;
		}
	}
	
	
	/**
	 * Check if search exists in the database
	 *
	 * @return	bool			TRUE if the item exists, FALSE if not
	 */
	public function exists() {
		if (!$this->id) {
			trigger_error('$this->id not set');
		}
		
		$sql = "SELECT COUNT(*) FROM savedSearches WHERE searchID=?";
		return !!Zotero_DB::valueQuery($sql, $this->id);
	}
	
	
	/*
	 * Save the search to the DB and return a savedSearchID
	 *
	 * If there are gaps in the searchConditionIDs, |fixGaps| must be true
	 * and the caller must dispose of the search or reload the condition ids,
	 * which may change after the save.
	 *
	 * For new searches, setName() must be called before saving
	 */
	public function save($fixGaps=false) {
		if (!$this->libraryID) {
			trigger_error("Library ID must be set before saving", E_USER_ERROR);
		}
		
		Zotero_Searches::editCheck($this);
		
		if (!$this->changed) {
			Z_Core::debug("Search $this->id has not changed");
			return false;
		}
		
		if (!$this->name) {
			trigger_error("Name not provided for saved search", E_USER_ERROR);
		}
		
		Zotero_DB::beginTransaction();
		
		$isNew = !$this->id || !$this->exists();
		
		try {
			$searchID = $this->id ? $this->id : Zotero_ID::get('savedSearches');
			
			Z_Core::debug("Saving search $this->id");
			
			if (!$isNew) {
				$sql = "DELETE FROM savedSearchConditions WHERE searchID=?";
				Zotero_DB::query($sql, $searchID);
			}
			
			$key = $this->key ? $this->key : $this->generateKey();
			
			$fields = "searchName=?, libraryID=?, `key`=?, dateAdded=?, dateModified=?,
						serverDateModified=?, serverDateModifiedMS=?";
			$timestamp = Zotero_DB::getTransactionTimestamp();
			$timestampMS = Zotero_DB::getTransactionTimestampMS();
			$params = array(
				$this->name,
				$this->libraryID,<commit_msg>Fix a sync error with saved searches named '0'<commit_after><?
/*
    ***** BEGIN LICENSE BLOCK *****
    
    This file is part of the Zotero Data Server.
    
    Copyright © 2010 Center for History and New Media
                     George Mason University, Fairfax, Virginia, USA
                     http://zotero.org
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    ***** END LICENSE BLOCK *****
*/

class Zotero_Search {
	private $id;
	private $libraryID;
	private $key;
	private $name;
	private $dateAdded;
	private $dateModified;
	
	private $loaded;
	private $changed;
	
	private $conditions = array();
	private $maxSearchConditionID;
	//private $sql;
	//private $sqlParams;
	
	public function __construct() {
		$numArgs = func_num_args();
		if ($numArgs) {
			throw new Exception("Constructor doesn't take any parameters");
		}
	}
	
	
	public function __get($field) {
		if (($this->id || $this->key) && !$this->loaded) {
			$this->load(true);
		}
		
		if (!property_exists('Zotero_Search', $field)) {
			throw new Exception("Zotero_Search property '$field' doesn't exist");
		}
		
		return $this->$field;
	}
	
	
	public function __set($field, $value) {
		switch ($field) {
			case 'id':
			case 'libraryID':
			case 'key':
				if ($this->loaded) {
					throw new Exception("Cannot set $field after search is already loaded");
				}
				$this->checkValue($field, $value);
				$this->$field = $value;
				return;
		}
		
		if ($this->id || $this->key) {
			if (!$this->loaded) {
				$this->load(true);
			}
		}
		else {
			$this->loaded = true;
		}
		
		$this->checkValue($field, $value);
		
		if ($this->$field != $value) {
			$this->changed = true;
			$this->$field = $value;
		}
	}
	
	
	/**
	 * Check if search exists in the database
	 *
	 * @return	bool			TRUE if the item exists, FALSE if not
	 */
	public function exists() {
		if (!$this->id) {
			trigger_error('$this->id not set');
		}
		
		$sql = "SELECT COUNT(*) FROM savedSearches WHERE searchID=?";
		return !!Zotero_DB::valueQuery($sql, $this->id);
	}
	
	
	/*
	 * Save the search to the DB and return a savedSearchID
	 *
	 * If there are gaps in the searchConditionIDs, |fixGaps| must be true
	 * and the caller must dispose of the search or reload the condition ids,
	 * which may change after the save.
	 *
	 * For new searches, setName() must be called before saving
	 */
	public function save($fixGaps=false) {
		if (!$this->libraryID) {
			throw new Exception("Library ID must be set before saving");
		}
		
		Zotero_Searches::editCheck($this);
		
		if (!$this->changed) {
			Z_Core::debug("Search $this->id has not changed");
			return false;
		}
		
		if (!isset($this->name)) {
			throw new Exception("Name not provided for saved search");
		}
		
		Zotero_DB::beginTransaction();
		
		$isNew = !$this->id || !$this->exists();
		
		try {
			$searchID = $this->id ? $this->id : Zotero_ID::get('savedSearches');
			
			Z_Core::debug("Saving search $this->id");
			
			if (!$isNew) {
				$sql = "DELETE FROM savedSearchConditions WHERE searchID=?";
				Zotero_DB::query($sql, $searchID);
			}
			
			$key = $this->key ? $this->key : $this->generateKey();
			
			$fields = "searchName=?, libraryID=?, `key`=?, dateAdded=?, dateModified=?,
						serverDateModified=?, serverDateModifiedMS=?";
			$timestamp = Zotero_DB::getTransactionTimestamp();
			$timestampMS = Zotero_DB::getTransactionTimestampMS();
			$params = array(
				$this->name,
				$this->libraryID,
{'fraction_non_alphanumeric': 0.13569321533923304, 'fraction_numerical': 0.001352015732546706, 'mean_word_length': 3.1075214538112066, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4399419', 'n_tokens_mistral': 2924, 'n_tokens_neox': 2497, 'n_words': 809}