<commit_before><?php

require_once(__DIR__."/KalturaFacebookLanguageMatch.php");
/**
 * @package plugins.facebookDistribution
 * @subpackage lib
 */
class FacebookDistributionEngine extends DistributionEngine implements
	IDistributionEngineSubmit,
	IDistributionEngineDelete,
	IDistributionEngineUpdate
{
	protected $appId;
	protected $appSecret;

	const FACEBOOK_CUSTOM_DATA_DELIMITER = ';';

	/* (non-PHPdoc)
	 * @see DistributionEngine::configure()
	 */
	public function configure()
	{
		parent::configure();
		$this->appId = kConf::get(FacebookConstants::FACEBOOK_APP_ID_REQUEST_PARAM, 'facebook', null);
		$this->appSecret = kConf::get(FacebookConstants::FACEBOOK_APP_SECRET_REQUEST_PARAM, 'facebook', null);

	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineSubmit::submit()
	 */
	public function submit(KalturaDistributionSubmitJobData $data)
	{
		$this->validate($data);
		if ($data->entryDistribution->remoteId)
		{
			$data->remoteId = $data->entryDistribution->remoteId;
		}
		else
		{
			$this->doSubmit($data);
		}
		return true;
	}

	/** on submission of a video we can take care of the following :
	 * 1. video content
	 * 2. thumbnail
	 * 3. call to action
	 * 4. name + description
	 * 5. place
	 * 6. tags
	 * 7. targeting
	 * 8. scheduled_publishing_time
	 * 9. feed targeting
	 */
	protected function doSubmit(KalturaDistributionSubmitJobData $data)
	{
		$videoPath = $data->providerData->videoAssetFilePath;
		if (!$videoPath)
			throw new Exception('No video asset to distribute, the job will fail');
		if (!kFile::checkFileExists($videoPath))
			throw new KalturaDistributionException("The file [$videoPath] was not found (probably not synced yet), the job will retry");

		$facebookMetadata = $this->convertToFacebookData($data->providerData->fieldValues, true);

		$tempThumbFile = null;
		try
		{
			if ($data->providerData->thumbAssetId)
				$tempThumbFile = $this->getAssetFile($data->providerData->thumbAssetId, $this->tempDirectory);
			$data->remoteId = FacebookGraphSdkUtils::uploadVideo(
				$this->appId,
				$this->appSecret,
				$data->distributionProfile->pageId,
				$data->distributionProfile->pageAccessToken,
				$videoPath,
				$tempThumbFile,
				kFile::fileSize($videoPath),
				$this->tempDirectory,
				$facebookMetadata);
			unlink($tempThumbFile);
		}
		catch (Exception $e)
		{
			if (kFile::checkFileExists($tempThumbFile))
				kFile::unlink($tempThumbFile);
			throw new Exception("Failed to submit facebook video , reason:".$e->getMessage());
		}

		$tags = self::helperGetTags($data->providerData->fieldValues);
		$this->handleTags($data->distributionProfile->pageAccessToken,$tags ,$data->remoteId);

		if (isset($data->providerData->captionsInfo))
		{
			/* @var $captionInfo KalturaFacebookCaptionDistributionInfo */
			foreach ($data->providerData->captionsInfo as $captionInfo)
			{
				$data->mediaFiles[] = $this->submitCaption($data->distributionProfile, $captionInfo, $data->remoteId);
			}
		}

		return true;
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineUpdate::update()<commit_msg>CLOUDMGT-846-facebook distribution with caption<commit_after><?php

require_once(__DIR__."/KalturaFacebookLanguageMatch.php");
/**
 * @package plugins.facebookDistribution
 * @subpackage lib
 */
class FacebookDistributionEngine extends DistributionEngine implements
	IDistributionEngineSubmit,
	IDistributionEngineDelete,
	IDistributionEngineUpdate
{
	protected $appId;
	protected $appSecret;

	const FACEBOOK_CUSTOM_DATA_DELIMITER = ';';

	/* (non-PHPdoc)
	 * @see DistributionEngine::configure()
	 */
	public function configure()
	{
		parent::configure();
		$this->appId = kConf::get(FacebookConstants::FACEBOOK_APP_ID_REQUEST_PARAM, 'facebook', null);
		$this->appSecret = kConf::get(FacebookConstants::FACEBOOK_APP_SECRET_REQUEST_PARAM, 'facebook', null);

	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineSubmit::submit()
	 */
	public function submit(KalturaDistributionSubmitJobData $data)
	{
		$this->validate($data);
		if ($data->entryDistribution->remoteId)
		{
			$data->remoteId = $data->entryDistribution->remoteId;
		}
		else
		{
			$this->doSubmit($data);
		}
		return true;
	}

	/** on submission of a video we can take care of the following :
	 * 1. video content
	 * 2. thumbnail
	 * 3. call to action
	 * 4. name + description
	 * 5. place
	 * 6. tags
	 * 7. targeting
	 * 8. scheduled_publishing_time
	 * 9. feed targeting
	 */
	protected function doSubmit(KalturaDistributionSubmitJobData $data)
	{
		$videoPath = $data->providerData->videoAssetFilePath;
		if (!$videoPath)
			throw new Exception('No video asset to distribute, the job will fail');
		if (!kFile::checkFileExists($videoPath))
			throw new KalturaDistributionException("The file [$videoPath] was not found (probably not synced yet), the job will retry");

		$facebookMetadata = $this->convertToFacebookData($data->providerData->fieldValues, true);

		$tempThumbFile = null;
		try
		{
			if ($data->providerData->thumbAssetId)
			{
				$tempThumbFile = $this->getAssetFile($data->providerData->thumbAssetId, $this->tempDirectory);
			}

			$data->remoteId = FacebookGraphSdkUtils::uploadVideo(
				$this->appId,
				$this->appSecret,
				$data->distributionProfile->pageId,
				$data->distributionProfile->pageAccessToken,
				$videoPath,
				$tempThumbFile,
				kFile::fileSize($videoPath),
				$this->tempDirectory,
				$facebookMetadata);

			if($tempThumbFile)
			{
				unlink($tempThumbFile);
			}
		}
		catch (Exception $e)
		{
			if (kFile::checkFileExists($tempThumbFile))
				kFile::unlink($tempThumbFile);
			throw new Exception("Failed to submit facebook video , reason:".$e->getMessage());
		}

		$tags = self::helperGetTags($data->providerData->fieldValues);
		$this->handleTags($data->distributionProfile->pageAccessToken,$tags ,$data->remoteId);

		if (isset($data->providerData->captionsInfo))
		{
			/* @var $captionInfo KalturaFacebookCaptionDistributionInfo */
			foreach ($data->providerData->captionsInfo as $captionInfo)
			{
				$data->mediaFiles[] = $this->submitCaption($data->distributionProfile, $captionInfo, $data->remoteId);
			}
		}

		return true;
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineUpdate::update()
{'fraction_non_alphanumeric': 0.13707719695743648, 'fraction_numerical': 0.0033986081890273506, 'mean_word_length': 4.802816901408451, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5047393', 'n_tokens_mistral': 2202, 'n_tokens_neox': 1903, 'n_words': 367}