<commit_before><?php
/**
 * @package plugins.contentDistribution
 * @subpackage lib
 */
class kContentDistributionManager
{
	
	/**
	 * @param entry $entry
	 * @param DistributionProfile $distributionProfile
	 * @param bool $submit
	 * @return EntryDistribution
	 */
	public static function addEntryDistribution(entry $entry, DistributionProfile $distributionProfile, $submit = false)
	{
		$entryDistribution = self::createEntryDistribution($entry, $distributionProfile);
		$entryDistribution->save();
		
		if($distributionProfile->getSubmitEnabled() == DistributionProfileActionStatus::AUTOMATIC)
			$submit = true;
			
		if($submit)
			self::submitAddEntryDistribution($entryDistribution, $distributionProfile);
			
		return $entryDistribution;
	}
	
	
	protected static function addImportJob($dc, $entryUrl, asset $asset)
	{
		$entryUrl = str_replace('//', '/', $entryUrl);
		$entryUrl = preg_replace('/^((https?)|(ftp)|(scp)|(sftp)):\//', '$1://', $entryUrl);
	
		$jobData = new kImportJobData();
		$jobData->setCacheOnly(true);
		$jobData->setSrcFileUrl($entryUrl);
		$jobData->setFlavorAssetId($asset->getId());
	
		$batchJob = new BatchJob();
		$batchJob->setDc($dc);
		$batchJob->setEntryId($asset->getEntryId());
		$batchJob->setPartnerId($asset->getPartnerId());
		
		return kJobsManager::addJob($batchJob, $jobData, BatchJobType::IMPORT);
	}
	
	/**
	 * @param EntryDistribution $entryDistribution
	 * @param DistributionProfile $distributionProfile
	 * @param int $dc
	 * @return bool true if the job could be created
	 */
	protected static function prepareDistributionJob(EntryDistribution $entryDistribution, DistributionProfile $distributionProfile, &$dc)
	{
		// prepare ids list of all the assets
		$assetIds = explode(',', implode(',', array(
			$entryDistribution->getThumbAssetIds(),
			$entryDistribution->getFlavorAssetIds()
		)));
		
		$assets = assetPeer::retrieveByIds($assetIds);
		$assetObjects = array();
		foreach($assets as $asset)
		{
			/* @var $asset asset */
			$assetObjects[$asset->getId()] = array(
				'asset' => $asset,
				'downloadUrl' => null,
			);
		}
		
		// lists all files from all assets
		$c = new Criteria();
		$c->add(FileSyncPeer::OBJECT_TYPE, FileSyncObjectType::ASSET);
		$c->add(FileSyncPeer::OBJECT_SUB_TYPE, asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET);
		$c->add(FileSyncPeer::OBJECT_ID, $assetIds, Criteria::IN);
		$c->add(FileSyncPeer::PARTNER_ID, $entryDistribution->getPartnerId());
		$c->add(FileSyncPeer::STATUS, FileSync::FILE_SYNC_STATUS_READY);
		$fileSyncs = FileSyncPeer::doSelect($c);
		
		$dcs = array();
		foreach($fileSyncs as $fileSync)
		{
			/* @var $fileSync FileSync */
			$assetId = $fileSync->getObjectId();
			
			if(!isset($assetObjects[$assetId])) // the object is not in the list of assets
				continue;
			
			$asset = $assetObjects[$assetId]['asset'];
			/* @var $asset asset */
			
			if($asset->getVersion() != $fileSync->getVersion()) // the file sync is not of the current asset version
				continue;
			
			$fileSync = kFileSyncUtils::resolve($fileSync);
			
			// use the best URL as the source for download in case it will be needed
			if($fileSync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_URL)
			{
				if(!is_null($assetObjects[$assetId]['downloadUrl']) && $fileSync->getDc() != $distributionProfile->getRecommendedStorageProfileForDownload())
					continue;
				
				$downloadUrl = $fileSync->getExternalUrl();
				if(!$downloadUrl)
					continue;
				
				$assetObjects[$assetId]['downloadUrl'] = $downloadUrl;
				continue;
			}
			
			// populates the list of files in each dc
			$fileSyncDc = $fileSync->getDc();
			if(!isset($dcs[$fileSyncDc]))
				$dcs[$fileSyncDc] = array();
			
			$dcs[$fileSyncDc][$assetId] = $fileSync->getId();					
		}
		
		if(isset($dcs[$dc]) && count($dcs[$dc]) == count($assets)) // all files exist in the current dc
			return true;
		
		// check if all files exist on any of the remote dcs
		$otherDcs = kDataCenterMgr::getAllDcs(true);
		foreach($otherDcs as $remoteDc)
		{
			$remoteDcId = $remoteDc['id'];
			if(count($dcs[$remoteDcId]) != count($assets))
				continue;
			
			$dc = $remoteDcId;
			return true;
		}
		
		if(
			$entryDistribution->getStatus() == EntryDistributionStatus::IMPORT_SUBMITTING<commit_msg>qnd: comment only<commit_after><?php
/**
 * @package plugins.contentDistribution
 * @subpackage lib
 */
class kContentDistributionManager
{
	
	/**
	 * @param entry $entry
	 * @param DistributionProfile $distributionProfile
	 * @param bool $submit
	 * @return EntryDistribution
	 */
	public static function addEntryDistribution(entry $entry, DistributionProfile $distributionProfile, $submit = false)
	{
		$entryDistribution = self::createEntryDistribution($entry, $distributionProfile);
		$entryDistribution->save();
		
		if($distributionProfile->getSubmitEnabled() == DistributionProfileActionStatus::AUTOMATIC)
			$submit = true;
			
		if($submit)
			self::submitAddEntryDistribution($entryDistribution, $distributionProfile);
			
		return $entryDistribution;
	}
	
	
	protected static function addImportJob($dc, $entryUrl, asset $asset)
	{
		$entryUrl = str_replace('//', '/', $entryUrl);
		$entryUrl = preg_replace('/^((https?)|(ftp)|(scp)|(sftp)):\//', '$1://', $entryUrl);
	
		$jobData = new kImportJobData();
		$jobData->setCacheOnly(true);
		$jobData->setSrcFileUrl($entryUrl);
		$jobData->setFlavorAssetId($asset->getId());
	
		$batchJob = new BatchJob();
		$batchJob->setDc($dc);
		$batchJob->setEntryId($asset->getEntryId());
		$batchJob->setPartnerId($asset->getPartnerId());
		
		return kJobsManager::addJob($batchJob, $jobData, BatchJobType::IMPORT);
	}
	
	/**
	 * @param EntryDistribution $entryDistribution
	 * @param DistributionProfile $distributionProfile
	 * @param int $dc
	 * @return bool true if the job could be created
	 */
	protected static function prepareDistributionJob(EntryDistribution $entryDistribution, DistributionProfile $distributionProfile, &$dc)
	{
		// prepare ids list of all the assets
		$assetIds = explode(',', implode(',', array(
			$entryDistribution->getThumbAssetIds(),
			$entryDistribution->getFlavorAssetIds()
		)));
		
		$assets = assetPeer::retrieveByIds($assetIds);
		$assetObjects = array();
		foreach($assets as $asset)
		{
			/* @var $asset asset */
			$assetObjects[$asset->getId()] = array(
				'asset' => $asset,
				'downloadUrl' => null,
			);
		}
		
		// lists all files from all assets
		$c = new Criteria();
		$c->add(FileSyncPeer::OBJECT_TYPE, FileSyncObjectType::ASSET);
		$c->add(FileSyncPeer::OBJECT_SUB_TYPE, asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET);
		$c->add(FileSyncPeer::OBJECT_ID, $assetIds, Criteria::IN);
		$c->add(FileSyncPeer::PARTNER_ID, $entryDistribution->getPartnerId());
		$c->add(FileSyncPeer::STATUS, FileSync::FILE_SYNC_STATUS_READY);
		$fileSyncs = FileSyncPeer::doSelect($c);
		
		$dcs = array();
		foreach($fileSyncs as $fileSync)
		{
			/* @var $fileSync FileSync */
			$assetId = $fileSync->getObjectId();
			
			if(!isset($assetObjects[$assetId])) // the object is not in the list of assets
				continue;
			
			$asset = $assetObjects[$assetId]['asset'];
			/* @var $asset asset */
			
			if($asset->getVersion() != $fileSync->getVersion()) // the file sync is not of the current asset version
				continue;
			
			$fileSync = kFileSyncUtils::resolve($fileSync);
			
			// use the best URL as the source for download in case it will be needed
			if($fileSync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_URL)
			{
				if(!is_null($assetObjects[$assetId]['downloadUrl']) && $fileSync->getDc() != $distributionProfile->getRecommendedStorageProfileForDownload())
					continue;
				
				$downloadUrl = $fileSync->getExternalUrl();
				if(!$downloadUrl)
					continue;
				
				$assetObjects[$assetId]['downloadUrl'] = $downloadUrl;
				continue;
			}
			
			// populates the list of files in each dc
			$fileSyncDc = $fileSync->getDc();
			if(!isset($dcs[$fileSyncDc]))
				$dcs[$fileSyncDc] = array();
			
			$dcs[$fileSyncDc][$assetId] = $fileSync->getId();					
		}
		
		if(isset($dcs[$dc]) && count($dcs[$dc]) == count($assets)) // all files exist in the current/preferred dc
			return true;
		
		// check if all files exist on any of the remote dcs
		$otherDcs = kDataCenterMgr::getAllDcs(true);
		foreach($otherDcs as $remoteDc)
		{
			$remoteDcId = $remoteDc['id'];
			if(count($dcs[$remoteDcId]) != count($assets))
				continue;
			
			$dc = $remoteDcId;
			return true;
		}
		
		if(
			$entryDistribution->getStatus() == EntryDistributionStatus::IMPORT_SUBMITTING
{'fraction_non_alphanumeric': 0.15558166862514688, 'fraction_numerical': 0.00023501762632197415, 'mean_word_length': 4.519455252918288, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2369415', 'n_tokens_mistral': 3152, 'n_tokens_neox': 2659, 'n_words': 543}