<commit_before><?php

/**
 * Subclass for representing a row from the 'category' table.
 *
 * 
 *
 * @package Core
 * @subpackage model
 */ 
class category extends Basecategory
{
	protected $childs_for_save = array();
	
	protected $depth = 0;
	
	protected $parent_category;
	
	protected $old_full_name = "";
	
	protected $old_parent_id = null;
	
	const MAX_CATEGORY_DEPTH = 8;
	
	const CATEGORY_ID_THAT_DOES_NOT_EXIST = 0;
	
	public function save(PropelPDO $con = null)
	{
		if ($this->isNew())
		{
			$numOfCatsForPartner = categoryPeer::doCount(new Criteria());
			
			if ($numOfCatsForPartner >= Partner::MAX_NUMBER_OF_CATEGORIES)
			{
				throw new kCoreException("Max number of categories was reached", kCoreException::MAX_NUMBER_OF_CATEGORIES_REACHED);
			}
		}
		
		// set the depth of the parent category + 1
		if ($this->isNew() || $this->isColumnModified(categoryPeer::PARENT_ID))
		{
			$parentCat = $this->getParentCategory();
			if ($this->getParentId() !== 0){
				$this->setDepth($parentCat->getDepth() + 1);
			}else{
				$this->setDepth(0);
			}
				$this->setChildsDepth();
		}
		
		if ($this->getDepth() >= self::MAX_CATEGORY_DEPTH)
		{
			throw new kCoreException("Max depth was reached", kCoreException::MAX_CATEGORY_DEPTH_REACHED);
		} 
		
		if ($this->isColumnModified(categoryPeer::NAME) || $this->isColumnModified(categoryPeer::PARENT_ID))
		{
			$this->updateFullName();
			$this->renameOnEntries();
		}
		else if ($this->isColumnModified(categoryPeer::FULL_NAME))
		{
			$this->renameOnEntries();
		}

		
		// happens in 3 cases:
		// 1. name of the current category was updated
		// 2. full name of the parent category was updated and it was set here as child
		// 3. parent id was changed
		if ($this->isColumnModified(categoryPeer::FULL_NAME)) 
			$this->setChildsFullNames();
		

		// save the childs 
		foreach($this->childs_for_save as $child)
		{
			$child->save();
		}
		$this->childs_for_save = array();
		
		if ($this->isColumnModified(categoryPeer::DELETED_AT) && $this->getDeletedAt() !== null)
		{
			$this->moveEntriesToParent();
		}
		
		$updateEntriesCount = false;
		if ($this->isColumnModified(categoryPeer::PARENT_ID) && !$this->isNew())
		{
			$updateEntriesCount = true;
			$oldParentId = $this->old_parent_id;
			$newParentId = $this->parent_id;
			$this->old_parent_id = null;
		}
				
		parent::save($con);
		
		if ($updateEntriesCount)
		{
			// decrease for the old parent category
			if($oldParentId)
				$oldParentCat = categoryPeer::retrieveByPK($oldParentId);
			if ($oldParentCat)
				$this->updateCategoryCount($oldParentCat);
									
			// increase for the new parent category
			$newParentCat = categoryPeer::retrieveByPK($newParentId);
			if ($newParentCat)			
				$this->updateCategoryCount($newParentCat);
		}			
	}
	
	private function updateCategoryCount($category)
	{
		$allChildren = $category->getAllChildren();
		$allSubCategoriesIds = array();
		$allSubCategoriesIds[] = $category->getId();
		
		if (count($allChildren))
		{
			foreach ($allChildren as $child)
				$allSubCategoriesIds[] = $child->getId();	
		}

		$c = KalturaCriteria::create(entryPeer::OM_CLASS);
		$entryFilter = new entryFilter();
		$entryFilter->set("_matchor_categories_ids", implode(',',$allSubCategoriesIds));
		$entryFilter->attachToCriteria($c);
		$entries = entryPeer::doSelect($c);

		$category->setEntriesCount(count($entries));
		$category->save();<commit_msg>qnd: for count more then 1000<commit_after><?php

/**
 * Subclass for representing a row from the 'category' table.
 *
 * 
 *
 * @package Core
 * @subpackage model
 */ 
class category extends Basecategory
{
	protected $childs_for_save = array();
	
	protected $depth = 0;
	
	protected $parent_category;
	
	protected $old_full_name = "";
	
	protected $old_parent_id = null;
	
	const MAX_CATEGORY_DEPTH = 8;
	
	const CATEGORY_ID_THAT_DOES_NOT_EXIST = 0;
	
	public function save(PropelPDO $con = null)
	{
		if ($this->isNew())
		{
			$numOfCatsForPartner = categoryPeer::doCount(new Criteria());
			
			if ($numOfCatsForPartner >= Partner::MAX_NUMBER_OF_CATEGORIES)
			{
				throw new kCoreException("Max number of categories was reached", kCoreException::MAX_NUMBER_OF_CATEGORIES_REACHED);
			}
		}
		
		// set the depth of the parent category + 1
		if ($this->isNew() || $this->isColumnModified(categoryPeer::PARENT_ID))
		{
			$parentCat = $this->getParentCategory();
			if ($this->getParentId() !== 0){
				$this->setDepth($parentCat->getDepth() + 1);
			}else{
				$this->setDepth(0);
			}
				$this->setChildsDepth();
		}
		
		if ($this->getDepth() >= self::MAX_CATEGORY_DEPTH)
		{
			throw new kCoreException("Max depth was reached", kCoreException::MAX_CATEGORY_DEPTH_REACHED);
		} 
		
		if ($this->isColumnModified(categoryPeer::NAME) || $this->isColumnModified(categoryPeer::PARENT_ID))
		{
			$this->updateFullName();
			$this->renameOnEntries();
		}
		else if ($this->isColumnModified(categoryPeer::FULL_NAME))
		{
			$this->renameOnEntries();
		}

		
		// happens in 3 cases:
		// 1. name of the current category was updated
		// 2. full name of the parent category was updated and it was set here as child
		// 3. parent id was changed
		if ($this->isColumnModified(categoryPeer::FULL_NAME)) 
			$this->setChildsFullNames();
		

		// save the childs 
		foreach($this->childs_for_save as $child)
		{
			$child->save();
		}
		$this->childs_for_save = array();
		
		if ($this->isColumnModified(categoryPeer::DELETED_AT) && $this->getDeletedAt() !== null)
		{
			$this->moveEntriesToParent();
		}
		
		$updateEntriesCount = false;
		if ($this->isColumnModified(categoryPeer::PARENT_ID) && !$this->isNew())
		{
			$updateEntriesCount = true;
			$oldParentId = $this->old_parent_id;
			$newParentId = $this->parent_id;
			$this->old_parent_id = null;
		}
				
		parent::save($con);
		
		if ($updateEntriesCount)
		{
			// decrease for the old parent category
			if($oldParentId)
				$oldParentCat = categoryPeer::retrieveByPK($oldParentId);
			if ($oldParentCat)
				$this->updateCategoryCount($oldParentCat);
									
			// increase for the new parent category
			$newParentCat = categoryPeer::retrieveByPK($newParentId);
			if ($newParentCat)			
				$this->updateCategoryCount($newParentCat);
		}			
	}
	
	private function updateCategoryCount($category)
	{
		$allChildren = $category->getAllChildren();
		$allSubCategoriesIds = array();
		$allSubCategoriesIds[] = $category->getId();
		
		if (count($allChildren))
		{
			foreach ($allChildren as $child)
				$allSubCategoriesIds[] = $child->getId();	
		}

		$c = KalturaCriteria::create(entryPeer::OM_CLASS);
		$entryFilter = new entryFilter();
		$entryFilter->set("_matchor_categories_ids", implode(',',$allSubCategoriesIds));
		$entryFilter->attachToCriteria($c);
		$entries = entryPeer::doSelect($c);

		$category->setEntriesCount($c->getRecordsCount());
		$category->save();
{'fraction_non_alphanumeric': 0.14734228971962618, 'fraction_numerical': 0.003796728971962617, 'mean_word_length': 4.145755071374906, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 84, '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': '6172955', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2265, 'n_words': 438}