<commit_before><?php
/**
 * @package api
 * @subpackage services
 */
class KalturaEntryService extends KalturaBaseService 
{
	
	  //amount of time for attempting to grab kLock
	  const KLOCK_MEDIA_UPDATECONTENT_GRAB_TIMEOUT = 0.1;
	
	  //amount of time for holding kLock
	  const KLOCK_MEDIA_UPDATECONTENT_HOLD_TIMEOUT = 7;

	public function initService($serviceId, $serviceName, $actionName)
	{
		$ks = kCurrentContext::$ks_object ? kCurrentContext::$ks_object : null;
		
		if (($actionName == 'list' || $actionName == 'count' || $actionName == 'listByReferenceId') &&
		  (!$ks || (!$ks->isAdmin() && !$ks->verifyPrivileges(ks::PRIVILEGE_LIST, ks::PRIVILEGE_WILDCARD))))
		{			
			KalturaCriterion::enableTag(KalturaCriterion::TAG_WIDGET_SESSION);
			entryPeer::setUserContentOnly(true);
		}
		
		
/*		//to support list categories with entitlmenet for user that is a member of more then 100 large categories
 		//large category is a category with > 10 members or > 100 entries. 				
  		if ($actionName == 'list' && kEntitlementUtils::getEntitlementEnforcement())
		{
			$dispatcher = KalturaDispatcher::getInstance();
			$arguments = $dispatcher->getArguments();
			
			$categoriesIds = array();
			$categories = array();
			foreach($arguments as $argument)
			{
				if ($argument instanceof KalturaBaseEntryFilter)
				{
					if(isset($argument->categoriesMatchAnd))
						$categories = array_merge($categories, explode(',', $argument->categoriesMatchAnd));
						
					if(isset($argument->categoriesMatchOr))
						$categories = array_merge($categories, explode(',', $argument->categoriesMatchOr));
					
					if(isset($argument->categoriesFullNameIn))
						$categories = array_merge($categories, explode(',', $argument->categoriesFullNameIn));
						
					if(count($categories))
					{
						$categories = categoryPeer::getByFullNamesExactMatch($categories);
						
						foreach ($categories as $category)
							$categoriesIds[] = $category->getId();
					}
										
					if(isset($argument->categoriesIdsMatchAnd))
						$categoriesIds = array_merge($categoriesIds, explode(',', $argument->categoriesIdsMatchAnd));
					
					if(isset($argument->categoriesIdsMatchOr))
						$categoriesIds = array_merge($categoriesIds, explode(',', $argument->categoriesIdsMatchOr));
					
					if(isset($argument->categoryAncestorIdIn))
						$categoriesIds = array_merge($categoriesIds, explode(',', $argument->categoryAncestorIdIn));
				}
			}
			
			foreach($categoriesIds as $key => $categoryId)
			{
				if(!$categoryId)
				{
					unset($categoriesIds[$key]);
				}
			}
			
			if(count($categoriesIds))
				entryPeer::setFilterdCategoriesIds($categoriesIds);
		}*/
		
		parent::initService($serviceId, $serviceName, $actionName);
		$this->applyPartnerFilterForClass('ConversionProfile');
		$this->applyPartnerFilterForClass('conversionProfile2');
	}
	
	/**
	 * @param kResource $resource
	 * @param entry $dbEntry
	 * @param asset $asset
	 * @return asset
	 * @throws KalturaErrors::ENTRY_TYPE_NOT_SUPPORTED
	 */
	protected function attachResource(kResource $resource, entry $dbEntry, asset $asset = null)
	{
		throw new KalturaAPIException(KalturaErrors::ENTRY_TYPE_NOT_SUPPORTED, $dbEntry->getType());
	}
	
	/**
	 * @param KalturaResource $resource
	 * @param entry $dbEntry
	 */
	protected function replaceResource(KalturaResource $resource, entry $dbEntry)
	{
		throw new KalturaAPIException(KalturaErrors::ENTRY_TYPE_NOT_SUPPORTED, $dbEntry->getType());
	}
	
	/**
	 * General code that replaces given entry resource with a given resource, and mark the original
	 * entry as replaced
	 * @param KalturaEntry $dbEntry The original entry we'd like to replace
	 * @param KalturaResource $resource The resource we'd like to attach
	 * @param KalturaEntry $tempMediaEntry The replacing entry
	 * @throws KalturaAPIException
	 */
	protected function replaceResourceByEntry($dbEntry, $resource, $tempMediaEntry) 
	{
		$partner = $this->getPartner();
		if(!$partner->getEnabledService(PermissionName::FEATURE_ENTRY_REPLACEMENT))
		{
			throw new KalturaAPIException(KalturaErrors::FEATURE_FORBIDDEN, PermissionName::FEATURE_ENTRY_REPLACEMENT);
		}
		
		if($dbEntry->getReplacingEntryId())
			throw new KalturaAPIException(KalturaErrors::ENTRY_REPLACEMENT_ALREADY_EXISTS);
		
		$resource->validateEntry($dbEntry);
		
		// create the temp db entry first and mark it as isTemporary == true
		$entryType = kPluginableEnumsManager::apiToCore('entryType', $tempMediaEntry->type);
		$class = entryPeer::getEntryClassByType($entryType);
			
		KalturaLog::debug("Creating new entry of API type [{$tempMediaEntry->type}] core type [$entryType] class [$class]");
		$tempDbEntry = new $class();
		$tempDbEntry->setIsTemporary(true);
		$tempDbEntry->setDisplayInSearch(mySearchUtils::DISPLAY_IN_SEARCH_SYSTEM);
		$tempDbEntry->setReplacedEntryId($dbEntry->getId());

		$kResource = $resource->toObject();
		if ($kResource->getType() == 'kOperationResource')
			$tempDbEntry->setTempTrimEntry(true);

		$tempDbEntry = $this->prepareEntryForInsert($tempMediaEntry, $tempDbEntry);
		$tempDbEntry->setPartnerId($dbEntry->getPartnerId());
		$tempDbEntry->save();
		
		$dbEntry->setReplacingEntryId($tempDbEntry->getId());
		$dbEntry->setReplacementStatus(entryReplacementStatus::NOT_READY_AND_NOT_APPROVED);
		if(!$partner->getEnabledService(PermissionName::FEATURE_ENTRY_REPLACEMENT_APPROVAL) || $dbEntry->getSourceType() == EntrySourceType::KALTURA_RECORDED_LIVE)
			$dbEntry->setReplacementStatus(entryReplacementStatus::APPROVED_BUT_NOT_READY);
		$dbEntry->save();

		$this->attachResource($kResource, $tempDbEntry);
	}

	protected function validateEntryForReplace($entryId, $dbEntry, $entryType = null)
	{
		if (!$dbEntry)
		{
			throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
		}

		if ($entryType && $dbEntry->getType() != $entryType)
		{
			throw new KalturaAPIException(KalturaErrors::INVALID_ENTRY_TYPE, $entryId, $dbEntry->getType(), $entryType);
		}
	}

	/**
	 * Approves entry replacement
	 *
	 * @param $dbEntry
	 * @throws KalturaAPIException
	 */
	protected function approveReplace($dbEntry)
	{

		switch ($dbEntry->getReplacementStatus())
		{
			case entryReplacementStatus::APPROVED_BUT_NOT_READY:
				break;

			case entryReplacementStatus::READY_BUT_NOT_APPROVED:
				kBusinessConvertDL::replaceEntry($dbEntry);
				break;

			case entryReplacementStatus::NOT_READY_AND_NOT_APPROVED:
				$dbEntry->setReplacementStatus(entryReplacementStatus::APPROVED_BUT_NOT_READY);
				$dbEntry->save();

				//preventing race conditions of temp entry being ready just as you approve the replacement
				$dbReplacingEntry = entryPeer::retrieveByPK($dbEntry->getReplacingEntryId());
				if ($dbReplacingEntry && $dbReplacingEntry->getStatus() == entryStatus::READY)
					kBusinessConvertDL::replaceEntry($dbEntry);
				break;

			case entryReplacementStatus::NONE:
			case entryReplacementStatus::FAILED:
			default:
				throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_REPLACED, $dbEntry->getId());
				break;
		}
	}
<commit_msg>PLAT-9871:Move cache versions to dynamic map<commit_after><?php
/**
 * @package api
 * @subpackage services
 */
class KalturaEntryService extends KalturaBaseService 
{
	
	  //amount of time for attempting to grab kLock
	  const KLOCK_MEDIA_UPDATECONTENT_GRAB_TIMEOUT = 0.1;
	
	  //amount of time for holding kLock
	  const KLOCK_MEDIA_UPDATECONTENT_HOLD_TIMEOUT = 7;

	public function initService($serviceId, $serviceName, $actionName)
	{
		$ks = kCurrentContext::$ks_object ? kCurrentContext::$ks_object : null;
		
		if (($actionName == 'list' || $actionName == 'count' || $actionName == 'listByReferenceId') &&
		  (!$ks || (!$ks->isAdmin() && !$ks->verifyPrivileges(ks::PRIVILEGE_LIST, ks::PRIVILEGE_WILDCARD))))
		{			
			KalturaCriterion::enableTag(KalturaCriterion::TAG_WIDGET_SESSION);
			entryPeer::setUserContentOnly(true);
		}
		
		
/*		//to support list categories with entitlmenet for user that is a member of more then 100 large categories
 		//large category is a category with > 10 members or > 100 entries. 				
  		if ($actionName == 'list' && kEntitlementUtils::getEntitlementEnforcement())
		{
			$dispatcher = KalturaDispatcher::getInstance();
			$arguments = $dispatcher->getArguments();
			
			$categoriesIds = array();
			$categories = array();
			foreach($arguments as $argument)
			{
				if ($argument instanceof KalturaBaseEntryFilter)
				{
					if(isset($argument->categoriesMatchAnd))
						$categories = array_merge($categories, explode(',', $argument->categoriesMatchAnd));
						
					if(isset($argument->categoriesMatchOr))
						$categories = array_merge($categories, explode(',', $argument->categoriesMatchOr));
					
					if(isset($argument->categoriesFullNameIn))
						$categories = array_merge($categories, explode(',', $argument->categoriesFullNameIn));
						
					if(count($categories))
					{
						$categories = categoryPeer::getByFullNamesExactMatch($categories);
						
						foreach ($categories as $category)
							$categoriesIds[] = $category->getId();
					}
										
					if(isset($argument->categoriesIdsMatchAnd))
						$categoriesIds = array_merge($categoriesIds, explode(',', $argument->categoriesIdsMatchAnd));
					
					if(isset($argument->categoriesIdsMatchOr))
						$categoriesIds = array_merge($categoriesIds, explode(',', $argument->categoriesIdsMatchOr));
					
					if(isset($argument->categoryAncestorIdIn))
						$categoriesIds = array_merge($categoriesIds, explode(',', $argument->categoryAncestorIdIn));
				}
			}
			
			foreach($categoriesIds as $key => $categoryId)
			{
				if(!$categoryId)
				{
					unset($categoriesIds[$key]);
				}
			}
			
			if(count($categoriesIds))
				entryPeer::setFilterdCategoriesIds($categoriesIds);
		}*/
		
		parent::initService($serviceId, $serviceName, $actionName);
		$this->applyPartnerFilterForClass('ConversionProfile');
		$this->applyPartnerFilterForClass('conversionProfile2');
	}
	
	/**
	 * @param kResource $resource
	 * @param entry $dbEntry
	 * @param asset $asset
	 * @return asset
	 * @throws KalturaErrors::ENTRY_TYPE_NOT_SUPPORTED
	 */
	protected function attachResource(kResource $resource, entry $dbEntry, asset $asset = null)
	{
		throw new KalturaAPIException(KalturaErrors::ENTRY_TYPE_NOT_SUPPORTED, $dbEntry->getType());
	}
	
	/**
	 * @param KalturaResource $resource
	 * @param entry $dbEntry
	 */
	protected function replaceResource(KalturaResource $resource, entry $dbEntry)
	{
		throw new KalturaAPIException(KalturaErrors::ENTRY_TYPE_NOT_SUPPORTED, $dbEntry->getType());
	}
	
	/**
	 * General code that replaces given entry resource with a given resource, and mark the original
	 * entry as replaced
	 * @param KalturaEntry $dbEntry The original entry we'd like to replace
	 * @param KalturaResource $resource The resource we'd like to attach
	 * @param KalturaEntry $tempMediaEntry The replacing entry
	 * @throws KalturaAPIException
	 */
	protected function replaceResourceByEntry($dbEntry, $resource, $tempMediaEntry) 
	{
		$partner = $this->getPartner();
		if(!$partner->getEnabledService(PermissionName::FEATURE_ENTRY_REPLACEMENT))
		{
			throw new KalturaAPIException(KalturaErrors::FEATURE_FORBIDDEN, PermissionName::FEATURE_ENTRY_REPLACEMENT);
		}
		
		if($dbEntry->getReplacingEntryId())
			throw new KalturaAPIException(KalturaErrors::ENTRY_REPLACEMENT_ALREADY_EXISTS);
		
		$resource->validateEntry($dbEntry);
		
		// create the temp db entry first and mark it as isTemporary == true
		$entryType = kPluginableEnumsManager::apiToCore('entryType', $tempMediaEntry->type);
		$class = entryPeer::getEntryClassByType($entryType);
			
		KalturaLog::debug("Creating new entry of API type [{$tempMediaEntry->type}] core type [$entryType] class [$class]");
		$tempDbEntry = new $class();
		$tempDbEntry->setIsTemporary(true);
		$tempDbEntry->setDisplayInSearch(mySearchUtils::DISPLAY_IN_SEARCH_SYSTEM);
		$tempDbEntry->setReplacedEntryId($dbEntry->getId());

		$kResource = $resource->toObject();
		if ($kResource->getType() == 'kOperationResource')
			$tempDbEntry->setTempTrimEntry(true);

		$tempDbEntry = $this->prepareEntryForInsert($tempMediaEntry, $tempDbEntry);
		$tempDbEntry->setPartnerId($dbEntry->getPartnerId());
		$tempDbEntry->save();
		
		$dbEntry->setReplacingEntryId($tempDbEntry->getId());
		$dbEntry->setReplacementStatus(entryReplacementStatus::NOT_READY_AND_NOT_APPROVED);
		if(!$partner->getEnabledService(PermissionName::FEATURE_ENTRY_REPLACEMENT_APPROVAL) || $dbEntry->getSourceType() == EntrySourceType::KALTURA_RECORDED_LIVE)
			$dbEntry->setReplacementStatus(entryReplacementStatus::APPROVED_BUT_NOT_READY);
		$dbEntry->save();

		$this->attachResource($kResource, $tempDbEntry);
	}

	protected function validateEntryForReplace($entryId, $dbEntry, $entryType = null)
	{
		if (!$dbEntry)
		{
			throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
		}

		if ($entryType && $dbEntry->getType() != $entryType)
		{
			throw new KalturaAPIException(KalturaErrors::INVALID_ENTRY_TYPE, $entryId, $dbEntry->getType(), $entryType);
		}
	}

	public function isApproveReplaceRequired($dbEntry)
	{
		if ($dbEntry->getMediaType() == KalturaMediaType::IMAGE)
		{
			return false;
		}
		return true;
	}

	/**
	 * Approves entry replacement
	 *
	 * @param $dbEntry
	 * @throws KalturaAPIException
	 */
	protected function approveReplace($dbEntry)
	{
		if (!$this->isApproveReplaceRequired($dbEntry))
		{
			return;
		}

		switch ($dbEntry->getReplacementStatus())
		{
			case entryReplacementStatus::APPROVED_BUT_NOT_READY:
				break;

			case entryReplacementStatus::READY_BUT_NOT_APPROVED:
				kBusinessConvertDL::replaceEntry($dbEntry);
				break;

			case entryReplacementStatus::NOT_READY_AND_NOT_APPROVED:
				$dbEntry->setReplacementStatus(entryReplacementStatus::APPROVED_BUT_NOT_READY);
				$dbEntry->save();

				//preventing race conditions of temp entry being ready just as you approve the replacement
				$dbReplacingEntry = entryPeer::retrieveByPK($dbEntry->getReplacingEntryId());
				if ($dbReplacingEntry && $dbReplacingEntry->getStatus() == entryStatus::READY)
					kBusinessConvertDL::replaceEntry($dbEntry);
				break;

			case entryReplacementStatus::NONE:
			case entryReplacementStatus::FAILED:
			default:
				throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_REPLACED, $dbEntry->getId());
				break;
		}
	}

{'fraction_non_alphanumeric': 0.12776617954070982, 'fraction_numerical': 0.0019485038274182323, 'mean_word_length': 5.175762784701332, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 117, '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': '4020883', 'n_tokens_mistral': 5158, 'n_tokens_neox': 4414, 'n_words': 792}