<commit_before><?php
/**
 * @package Scheduler
 * @subpackage MoveCategoryEntries
 */

/**
 * Will move category entries from source category to destination category
 *
 * @package Scheduler
 * @subpackage MoveCategoryEntries
 */
class KAsyncMoveCategoryEntries extends KJobHandlerWorker
{
	const CATEGORY_ENTRY_ALREADY_EXISTS = 'CATEGORY_ENTRY_ALREADY_EXISTS';
	
	/**
	 * Indicates that the moving of the entries could be started
	 * Used when the batch crash during the recursion
	 * and the move should start from the last crash point
	 * 
	 * @var bool
	 */
	private $startMove = true;
	
	/* (non-PHPdoc)
	 * @see KBatchBase::getType()
	 */
	public static function getType()
	{
		return KalturaBatchJobType::MOVE_CATEGORY_ENTRIES;
	}
	
	/* (non-PHPdoc)
	 * @see KBatchBase::getJobType()
	 */
	public function getJobType()
	{
		return self::getType();
	}
	
	/* (non-PHPdoc)
	 * @see KJobHandlerWorker::exec()
	 */
	protected function exec(KalturaBatchJob $job)
	{
		return $this->move($job, $job->data);
	}
	
	/**
	 * Moves category entries from source category to destination category
	 * 
	 * @param KalturaBatchJob $job
	 * @param KalturaMoveCategoryEntriesJobData $data
	 * 
	 * @return KalturaBatchJob
	 */
	protected function move(KalturaBatchJob $job, KalturaMoveCategoryEntriesJobData $data)
	{
	    $this->impersonate($job->partnerId);
		KalturaLog::debug("Move category entries job id [$job->id]");
		
		if($data->lastMovedCategoryId)
			$this->startMove = false;
			
		$job = $this->moveCategory($job, $data);
		$this->unimpersonate();
		$job = $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED);
		
		return $job;
	}
	
	/**
	 * Go through all categories tree and call moveEntries
	 * 
	 * @param KalturaBatchJob $job
	 * @param KalturaMoveCategoryEntriesJobData $data
	 * @param int $srcCategoryId Current source category id
	 * 
	 * @return KalturaBatchJob
	 */
	private function moveCategory(KalturaBatchJob $job, KalturaMoveCategoryEntriesJobData $data, $srcCategoryId = null)
	{
	    
		if(is_null($srcCategoryId))
			$srcCategoryId = $data->srcCategoryId;
		
		if(!$this->startMove && $data->lastMovedCategoryId == $srcCategoryId)
			$this->startMove = true;
		
		if($this->startMove)
			$movedEntries = $this->moveEntries($job, $data, $srcCategoryId);
		
		if($data->moveFromChildren)
		{
			$categoryFilter = new KalturaCategoryFilter();
			$categoryFilter->parentIdEqual = $srcCategoryId;
			
			$categoryPager = new KalturaFilterPager();
			$categoryPager->pageSize = 100;
			if($this->taskConfig->params->pageSize)
				$categoryPager->pageSize = $this->taskConfig->params->pageSize;
				
			if($data->lastMovedCategoryId == $srcCategoryId)
				$categoryPager->pageIndex = $data->lastMovedCategoryPageIndex;
				
			$categoriesList = $this->kClient->category->listAction($categoryFilter, $categoryPager);
			while(count($categoriesList->objects))
			{
				foreach($categoriesList->objects as $category)
				{
					/* @var $category KalturaCategory */
					$movedEntries += $this->moveCategory($job, $data, $category->id);
				}
				
				$categoryPager->pageIndex++;
				
				$data->lastMovedCategoryPageIndex = $categoryPager->pageIndex;
				$this->updateJob($job, null, KalturaBatchJobStatus::PROCESSING, null, $data);
				
				$categoriesList = $this->kClient->category->listAction($categoryFilter, $categoryPager);
			}
		}
		
		$data->lastMovedCategoryId = $srcCategoryId;
		
		$this->unimpersonate();
		$this->updateJob($job, "Moved [$movedEntries] entries", KalturaBatchJobStatus::PROCESSING, null, $data);
		$this->impersonate($job->partnerId);
		
		return $job;
	}

	/**
	 * Moves category entries from source category to destination category
	 */
	private function moveEntries(KalturaBatchJob $job, KalturaMoveCategoryEntriesJobData $data, $srcCategoryId)
	{
		$categoryEntryFilter = new KalturaCategoryEntryFilter();
		$categoryEntryFilter->orderBy = KalturaCategoryEntryOrderBy::CREATED_AT_ASC;
		$categoryEntryFilter->categoryIdEqual = $srcCategoryId;
		
		$categoryEntryPager = new KalturaFilterPager();
		$categoryEntryPager->pageSize = 100;
		if($this->taskConfig->params->pageSize)
			$categoryEntryPager->pageSize = $this->taskConfig->params->pageSize;
			
		if($data->lastMovedCategoryId == $srcCategoryId)
			$categoryPager->pageIndex = $data->lastMovedCategoryEntryPageIndex;
			
		$movedEntries = 0;
		$categoryEntriesList = $this->kClient->categoryEntry->listAction($categoryEntryFilter, $categoryEntryPager);
		while(count($categoryEntriesList->objects))
		{
			$this->kClient->startMultiRequest();
			$entryIds = array();
			foreach($categoryEntriesList->objects as $oldCategoryEntry)
			{
				/* @var $categoryEntry KalturaCategoryEntry */
				$newCategoryEntry = new KalturaCategoryEntry();
				$newCategoryEntry->entryId = $oldCategoryEntry->entryId;
				$newCategoryEntry->categoryId = $data->destCategoryId;
				$this->kClient->categoryEntry->add($newCategoryEntry);
				$entryIds[] = $oldCategoryEntry->entryId;
			}
			$addedCategoryEntriesResults = $this->kClient->doMultiRequest();
	
			$this->kClient->startMultiRequest();
			foreach($addedCategoryEntriesResults as $index => $addedCategoryEntryResult)
			{
				if(		is_array($addedCategoryEntryResult) 
					&&	isset($addedCategoryEntryResult['code']) 
					&& 	$addedCategoryEntryResult['code'] != 'CATEGORY_ENTRY_ALREADY_EXISTS'
				)<commit_msg>qnd: fix endless loop when entry is deleted. <commit_after><?php
/**
 * @package Scheduler
 * @subpackage MoveCategoryEntries
 */

/**
 * Will move category entries from source category to destination category
 *
 * @package Scheduler
 * @subpackage MoveCategoryEntries
 */
class KAsyncMoveCategoryEntries extends KJobHandlerWorker
{
	const CATEGORY_ENTRY_ALREADY_EXISTS = 'CATEGORY_ENTRY_ALREADY_EXISTS';
	
	/**
	 * Indicates that the moving of the entries could be started
	 * Used when the batch crash during the recursion
	 * and the move should start from the last crash point
	 * 
	 * @var bool
	 */
	private $startMove = true;
	
	/* (non-PHPdoc)
	 * @see KBatchBase::getType()
	 */
	public static function getType()
	{
		return KalturaBatchJobType::MOVE_CATEGORY_ENTRIES;
	}
	
	/* (non-PHPdoc)
	 * @see KBatchBase::getJobType()
	 */
	public function getJobType()
	{
		return self::getType();
	}
	
	/* (non-PHPdoc)
	 * @see KJobHandlerWorker::exec()
	 */
	protected function exec(KalturaBatchJob $job)
	{
		return $this->move($job, $job->data);
	}
	
	/**
	 * Moves category entries from source category to destination category
	 * 
	 * @param KalturaBatchJob $job
	 * @param KalturaMoveCategoryEntriesJobData $data
	 * 
	 * @return KalturaBatchJob
	 */
	protected function move(KalturaBatchJob $job, KalturaMoveCategoryEntriesJobData $data)
	{
	    $this->impersonate($job->partnerId);
		KalturaLog::debug("Move category entries job id [$job->id]");
		
		if($data->lastMovedCategoryId)
			$this->startMove = false;
			
		$job = $this->moveCategory($job, $data);
		$this->unimpersonate();
		$job = $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED);
		
		return $job;
	}
	
	/**
	 * Go through all categories tree and call moveEntries
	 * 
	 * @param KalturaBatchJob $job
	 * @param KalturaMoveCategoryEntriesJobData $data
	 * @param int $srcCategoryId Current source category id
	 * 
	 * @return KalturaBatchJob
	 */
	private function moveCategory(KalturaBatchJob $job, KalturaMoveCategoryEntriesJobData $data, $srcCategoryId = null)
	{
	    
		if(is_null($srcCategoryId))
			$srcCategoryId = $data->srcCategoryId;
		
		if(!$this->startMove && $data->lastMovedCategoryId == $srcCategoryId)
			$this->startMove = true;
		
		if($this->startMove)
			$movedEntries = $this->moveEntries($job, $data, $srcCategoryId);
		
		if($data->moveFromChildren)
		{
			$categoryFilter = new KalturaCategoryFilter();
			$categoryFilter->parentIdEqual = $srcCategoryId;
			
			$categoryPager = new KalturaFilterPager();
			$categoryPager->pageSize = 100;
			if($this->taskConfig->params->pageSize)
				$categoryPager->pageSize = $this->taskConfig->params->pageSize;
				
			if($data->lastMovedCategoryId == $srcCategoryId)
				$categoryPager->pageIndex = $data->lastMovedCategoryPageIndex;
				
			$categoriesList = $this->kClient->category->listAction($categoryFilter, $categoryPager);
			while(count($categoriesList->objects))
			{
				foreach($categoriesList->objects as $category)
				{
					/* @var $category KalturaCategory */
					$movedEntries += $this->moveCategory($job, $data, $category->id);
				}
				
				$categoryPager->pageIndex++;
				
				$data->lastMovedCategoryPageIndex = $categoryPager->pageIndex;
				$this->updateJob($job, null, KalturaBatchJobStatus::PROCESSING, null, $data);
				
				$categoriesList = $this->kClient->category->listAction($categoryFilter, $categoryPager);
			}
		}
		
		$data->lastMovedCategoryId = $srcCategoryId;
		
		$this->unimpersonate();
		$this->updateJob($job, "Moved [$movedEntries] entries", KalturaBatchJobStatus::PROCESSING, null, $data);
		$this->impersonate($job->partnerId);
		
		return $job;
	}

	/**
	 * Moves category entries from source category to destination category
	 */
	private function moveEntries(KalturaBatchJob $job, KalturaMoveCategoryEntriesJobData $data, $srcCategoryId)
	{
		$categoryEntryFilter = new KalturaCategoryEntryFilter();
		$categoryEntryFilter->orderBy = KalturaCategoryEntryOrderBy::CREATED_AT_ASC;
		$categoryEntryFilter->categoryIdEqual = $srcCategoryId;
		
		$categoryEntryPager = new KalturaFilterPager();
		$categoryEntryPager->pageSize = 100;
		if($this->taskConfig->params->pageSize)
			$categoryEntryPager->pageSize = $this->taskConfig->params->pageSize;
			
		if($data->lastMovedCategoryId == $srcCategoryId)
			$categoryPager->pageIndex = $data->lastMovedCategoryEntryPageIndex;
			
		$movedEntries = 0;
		$categoryEntriesList = $this->kClient->categoryEntry->listAction($categoryEntryFilter, $categoryEntryPager);
		while(count($categoryEntriesList->objects))
		{
			$this->kClient->startMultiRequest();
			$entryIds = array();
			foreach($categoryEntriesList->objects as $oldCategoryEntry)
			{
				/* @var $categoryEntry KalturaCategoryEntry */
				$newCategoryEntry = new KalturaCategoryEntry();
				$newCategoryEntry->entryId = $oldCategoryEntry->entryId;
				$newCategoryEntry->categoryId = $data->destCategoryId;
				$this->kClient->categoryEntry->add($newCategoryEntry);
				$entryIds[] = $oldCategoryEntry->entryId;
			}
			$addedCategoryEntriesResults = $this->kClient->doMultiRequest();
	
			$this->kClient->startMultiRequest();
			foreach($addedCategoryEntriesResults as $index => $addedCategoryEntryResult)
			{
				if(		is_array($addedCategoryEntryResult) 
					&&	isset($addedCategoryEntryResult['code']) 
					&& 	!( $addedCategoryEntryResult['code'] == 'CATEGORY_ENTRY_ALREADY_EXISTS' ||
						  $addedCategoryEntryResult['code'] == 'INVALID_ENTRY_ID') //deleted entry
				)
{'fraction_non_alphanumeric': 0.13165394869908983, 'fraction_numerical': 0.0012871196101866323, 'mean_word_length': 4.6804177545691905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 165, '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': '830068', 'n_tokens_mistral': 3880, 'n_tokens_neox': 3339, 'n_words': 618}