/** * Transform Xml file with conversion profile xsl * If xsl is not found, original Xml returned * @param string $filePath the original xml that was taken from partner file path * @return string - transformed Xml */ protected function xslTransform($filePath) { $xdoc = file_get_contents($filePath); if (is_null($xdoc) || is_null($this->conversionProfileXsl)) { return $xdoc; } libxml_clear_errors(); $xml = new KDOMDocument(); if (!$xml->loadXML($xdoc)) { KalturaLog::debug("Could not load xml"); $errorMessage = kXml::getLibXmlErrorDescription($xdoc); throw new KalturaBatchException("Could not load xml [{$this->job->id}], {$errorMessage}", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED); } libxml_clear_errors(); $proc = new XSLTProcessor(); $proc->registerPHPFunctions(kXml::getXslEnabledPhpFunctions()); $xsl = new KDOMDocument(); if (!$xsl->loadXML($this->conversionProfileXsl)) { KalturaLog::debug("Could not load xsl" . $this->conversionProfileXsl); $errorMessage = kXml::getLibXmlErrorDescription($this->conversionProfileXsl); throw new KalturaBatchException("Could not load xsl [{$this->job->id}], {$errorMessage}", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED); } $proc->importStyleSheet($xsl); libxml_clear_errors(); $transformedXml = $proc->transformToXML($xml); if (!$transformedXml) { KalturaLog::debug("Could not transform xml" . $this->conversionProfileXsl); $errorMessage = kXml::getLibXmlErrorDescription($this->conversionProfileXsl); throw new KalturaBatchException("Could not transform xml [{$this->job->id}], {$errorMessage}", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED); } return $transformedXml; }
public function loadProperties(KalturaDistributionJobData $distributionJobData, KalturaGenericDistributionProfile $distributionProfile, $action) { $actionName = self::$actionAttributes[$action]; $genericProviderAction = GenericDistributionProviderActionPeer::retrieveByProviderAndAction($distributionProfile->genericProviderId, $action); if (!$genericProviderAction) { KalturaLog::err("Generic provider [{$distributionProfile->genericProviderId}] action [{$actionName}] not found"); return; } if (!$distributionJobData->entryDistribution) { KalturaLog::err("Entry Distribution object not provided"); return; } if (!$distributionProfile->{$actionName}->protocol) { $distributionProfile->{$actionName}->protocol = $genericProviderAction->getProtocol(); } if (!$distributionProfile->{$actionName}->serverUrl) { $distributionProfile->{$actionName}->serverUrl = $genericProviderAction->getServerAddress(); } if (!$distributionProfile->{$actionName}->serverPath) { $distributionProfile->{$actionName}->serverPath = $genericProviderAction->getRemotePath(); } if (!$distributionProfile->{$actionName}->username) { $distributionProfile->{$actionName}->username = $genericProviderAction->getRemoteUsername(); } if (!$distributionProfile->{$actionName}->password) { $distributionProfile->{$actionName}->password = $genericProviderAction->getRemotePassword(); } if (!$distributionProfile->{$actionName}->ftpPassiveMode) { $distributionProfile->{$actionName}->ftpPassiveMode = $genericProviderAction->getFtpPassiveMode(); } if (!$distributionProfile->{$actionName}->httpFieldName) { $distributionProfile->{$actionName}->httpFieldName = $genericProviderAction->getHttpFieldName(); } if (!$distributionProfile->{$actionName}->httpFileName) { $distributionProfile->{$actionName}->httpFileName = $genericProviderAction->getHttpFileName(); } $entry = entryPeer::retrieveByPKNoFilter($distributionJobData->entryDistribution->entryId); if (!$entry) { KalturaLog::err("Entry [" . $distributionJobData->entryDistribution->entryId . "] not found"); return; } $mrss = kMrssManager::getEntryMrss($entry); if (!$mrss) { KalturaLog::err("MRSS not returned for entry [" . $entry->getId() . "]"); return; } $xml = new KDOMDocument(); if (!$xml->loadXML($mrss)) { KalturaLog::err("MRSS not is not valid XML:\n{$mrss}\n"); return; } $key = $genericProviderAction->getSyncKey(GenericDistributionProviderAction::FILE_SYNC_DISTRIBUTION_PROVIDER_ACTION_MRSS_TRANSFORMER); if (kFileSyncUtils::fileSync_exists($key)) { $xslPath = kFileSyncUtils::getLocalFilePathForKey($key); if ($xslPath) { $xsl = new KDOMDocument(); $xsl->load($xslPath); // set variables in the xsl $varNodes = $xsl->getElementsByTagName('variable'); foreach ($varNodes as $varNode) { $nameAttr = $varNode->attributes->getNamedItem('name'); if (!$nameAttr) { continue; } $name = $nameAttr->value; if ($name && $distributionJobData->{$name}) { $varNode->textContent = $distributionJobData->{$name}; $varNode->appendChild($xsl->createTextNode($distributionJobData->{$name})); } } $proc = new XSLTProcessor(); $proc->registerPHPFunctions(kXml::getXslEnabledPhpFunctions()); $proc->importStyleSheet($xsl); $xml = $proc->transformToDoc($xml); if (!$xml) { KalturaLog::err("Transform returned false"); return; } } } $key = $genericProviderAction->getSyncKey(GenericDistributionProviderAction::FILE_SYNC_DISTRIBUTION_PROVIDER_ACTION_MRSS_VALIDATOR); if (kFileSyncUtils::fileSync_exists($key)) { $xsdPath = kFileSyncUtils::getLocalFilePathForKey($key); if ($xsdPath && !$xml->schemaValidate($xsdPath)) { KalturaLog::err("Inavlid XML:\n" . $xml->saveXML()); KalturaLog::err("Schema [{$xsdPath}]:\n" . file_get_contents($xsdPath)); return; } } $this->xml = $xml->saveXML(); $key = $genericProviderAction->getSyncKey(GenericDistributionProviderAction::FILE_SYNC_DISTRIBUTION_PROVIDER_ACTION_RESULTS_TRANSFORMER); if (kFileSyncUtils::fileSync_exists($key)) { $this->resultParseData = kFileSyncUtils::file_get_contents($key, true, false); } $this->resultParserType = $genericProviderAction->getResultsParser(); }
/** * @param string $results * @param KalturaGenericDistributionProviderParser $resultParserType * @param string $resultParseData * @return array of parsed values */ protected function parseResults($results, $resultParserType, $resultParseData) { switch ($resultParserType) { case KalturaGenericDistributionProviderParser::XSL: $xml = new DOMDocument(); if (!$xml->loadXML($results)) { return false; } $xsl = new DOMDocument(); $xsl->loadXML($resultParseData); $proc = new XSLTProcessor(); $proc->registerPHPFunctions(kXml::getXslEnabledPhpFunctions()); $proc->importStyleSheet($xsl); $data = $proc->transformToDoc($xml); if (!$data) { return false; } return explode(',', $data); case KalturaGenericDistributionProviderParser::XPATH: $xml = new DOMDocument(); if (!$xml->loadXML($results)) { return false; } $xpath = new DOMXPath($xml); $elements = $xpath->query($resultParseData); if (is_null($elements)) { return false; } $matches = array(); foreach ($elements as $element) { $matches[] = $element->textContent; } return $matches; case KalturaGenericDistributionProviderParser::REGEX: $matches = array(); if (!preg_match("/{$resultParseData}/", $results, $matches)) { return false; } return array_shift($matches); default: return false; } }
/** * @param string $xml * @param string $xslt * @param array $xsltParams * @return string */ public static function transformXmlUsingXslt($xmlStr, $xslt, $xsltParams = array(), &$xsltErrors = array()) { $xml = new KDOMDocument(); if (!$xml->loadXML($xmlStr)) { KalturaLog::debug("Could not load xmlStr"); return null; } $xsl = new KDOMDocument(); if (!$xsl->loadXML($xslt)) { KalturaLog::debug("Could not load xslt"); return null; } $proc = new XSLTProcessor(); foreach ($xsltParams as $key => $value) { $proc->setParameter('', $key, $value); } $proc->registerPHPFunctions(kXml::getXslEnabledPhpFunctions()); @$proc->importStyleSheet($xsl); $errorHandler = new XSLTErrorCollector(); $xml = @$proc->transformToDoc($xml); $errorHandler->restoreErrorHandler(); $xsltErrors = $errorHandler->errors; if (!$xml) { KalturaLog::err("XML Transformation failed"); return null; } if (isset($xml->documentElement)) { $xml->documentElement->removeAttributeNS('http://php.net/xsl', 'php'); } return $xml->saveXML(); }
/** * @return string path value * @param entry $entry * @param string $xslStr */ protected function getPathValue(entry $entry, $xslStr) { // set the default criteria to use the current entry distribution partner id (it is restored later) // this is needed for related entries under kMetadataMrssManager which is using retrieveByPK without the correct partner id filter $oldEntryCriteria = entryPeer::getCriteriaFilter()->getFilter(); myPartnerUtils::resetPartnerFilter('entry'); myPartnerUtils::addPartnerToCriteria('entry', $entry->getPartnerId(), true); $mrss = null; $mrssParams = new kMrssParameters(); $mrssParams->setStatuses(array(flavorAsset::ASSET_STATUS_READY, flavorAsset::ASSET_STATUS_EXPORTING)); $mrss = kMrssManager::getEntryMrssXml($entry, $mrss, $mrssParams); $mrssStr = $mrss->asXML(); // restore the original criteria entryPeer::getCriteriaFilter()->setFilter($oldEntryCriteria); if (!$mrssStr) { KalturaLog::err('No MRSS returned for entry [' . $entry->getId() . ']'); return null; } $mrssObj = new DOMDocument(); if (!$mrssObj->loadXML($mrssStr)) { KalturaLog::err('Error loading MRSS XML object for entry [' . $entry->getId() . ']'); return null; } $xslObj = new DOMDocument(); $xslStr = trim($xslStr); if (!$xslObj->loadXML($xslStr)) { KalturaLog::err('Error loading XSL'); return null; } $proc = new XSLTProcessor(); $proc->registerPHPFunctions(kXml::getXslEnabledPhpFunctions()); $proc->importStyleSheet($xslObj); $resultXmlObj = $proc->transformToDoc($mrssObj); if (!$resultXmlObj) { KalturaLog::err('Error transforming XML for entry id [' . $entry->getId() . ']'); return null; } /* DEBUG logs KalturaLog::log('entry mrss = '.$mrssStr); KalturaLog::log('profile xslt = '.$xslStr); */ KalturaLog::debug('Result XML: ' . $resultXmlObj->saveXML()); $xpath = new DOMXPath($resultXmlObj); $fieldElement = $xpath->query("//path_value")->item(0); if (!$fieldElement) { KalturaLog::err('Cannot find element <path_value> in XML'); return null; } $fieldValue = $fieldElement->nodeValue; return $fieldValue; }
/** * Transform XML using XSLT * @param string $xmlStr * @param string $xslStr * @return string the result XML */ protected function transformXml($xmlStr, $xslStr) { $xmlObj = new DOMDocument(); if (!$xmlObj->loadXML($xmlStr)) { throw new Exception('Error loading source XML'); } $xslObj = new DOMDocument(); if (!$xslObj->loadXML($xslStr)) { throw new Exception('Error loading XSLT'); } $proc = new XSLTProcessor(); $proc->registerPHPFunctions(kXml::getXslEnabledPhpFunctions()); $proc->importStyleSheet($xslObj); $resultXmlObj = $proc->transformToDoc($xmlObj); if (!$resultXmlObj) { throw new Exception('Error transforming XML'); return null; } $resultXmlStr = $resultXmlObj->saveXML(); return $resultXmlStr; }
protected function getFieldValuesXml(EntryDistribution $entryDistribution, $fieldName = null) { $entry = entryPeer::retrieveByPKNoFilter($entryDistribution->getEntryId()); if (!$entry) { KalturaLog::err('Entry not found with ID [' . $entryDistribution->getEntryId() . ']'); return null; } // set the default criteria to use the current entry distribution partner id (it is restored later) // this is needed for related entries under kMetadataMrssManager which is using retrieveByPK without the correct partner id filter $oldEntryCriteria = entryPeer::getCriteriaFilter()->getFilter(); myPartnerUtils::resetPartnerFilter('entry'); myPartnerUtils::addPartnerToCriteria('entry', $entryDistribution->getPartnerId(), true); try { $mrss = null; $mrssParams = new kMrssParameters(); if ($this->getItemXpathsToExtend()) { $mrssParams->setItemXpathsToExtend($this->getItemXpathsToExtend()); } $mrss = kMrssManager::getEntryMrssXml($entry, $mrss, $mrssParams); $mrssStr = $mrss->asXML(); } catch (Exception $e) { // restore the original criteria so it will not get stuck due to the exception entryPeer::getCriteriaFilter()->setFilter($oldEntryCriteria); throw $e; } // restore the original criteria entryPeer::getCriteriaFilter()->setFilter($oldEntryCriteria); if (!$mrssStr) { KalturaLog::err('No MRSS returned for entry [' . $entry->getId() . ']'); return null; } $mrssObj = new DOMDocument(); if (!$mrssObj->loadXML($mrssStr)) { KalturaLog::err('Error loading MRSS XML object for entry [' . $entry->getId() . ']'); return null; } $xslObj = new DOMDocument(); $xslStr = $this->getFieldValuesXslt($entryDistribution, $fieldName); $xslStr = trim($xslStr); if (!$xslObj->loadXML($xslStr)) { KalturaLog::err('Error loading distribution profile XSLT for profile ID [' . $this->getId() . ']'); return null; } $proc = new XSLTProcessor(); $proc->registerPHPFunctions(kXml::getXslEnabledPhpFunctions()); $proc->importStyleSheet($xslObj); $resultXml = $proc->transformToXml($mrssObj); //in order to keep the UTF-8 encoding we transformToXml http://www.php.net/manual/en/xsltprocessor.transformtodoc.php#69305 $resultXmlObj = new DOMDocument(); $resultXmlObj->loadXML($resultXml); if (!$resultXmlObj) { KalturaLog::err('Error transforming XML for distribution profile [' . $this->getId() . '] and entry id [' . $entry->getId() . ']'); return null; } /* DEBUG logs KalturaLog::log('entry mrss = '.$mrssStr); KalturaLog::log('profile xslt = '.$xslStr); */ KalturaLog::debug('Result XML: ' . $resultXmlObj->saveXML()); return $resultXmlObj; }
protected function getOriginalOrTransformIfNeeded(DropFolder $folder, $xmlPath) { if (!file_exists($xmlPath) || !filesize($xmlPath)) { throw new Exception('Empty file supplied as input'); } if (!$folder->getConversionProfileId()) { KalturaLog::debug('No conversion profile found on drop folder [' . $folder->getId() . '] assuming no xsl transformation is needed'); return file_get_contents($xmlPath); } $conversionProfile = conversionProfile2Peer::retrieveByPK($folder->getConversionProfileId()); if (!$conversionProfile || strlen($conversionProfile->getXsl()) == 0) { KalturaLog::debug('No conversion profile found Or no xsl transform found'); return file_get_contents($xmlPath); } $originalXmlDoc = file_get_contents($xmlPath); $origianlXml = new KDOMDocument(); if (!$origianlXml->loadXML($originalXmlDoc)) { KalturaLog::debug('Could not load original xml'); $errorMessage = kXml::getLibXmlErrorDescription($originalXmlDoc); throw new Exception(DropFolderXmlBulkUploadPlugin::MALFORMED_XML_FILE_MESSAGE, DropFolderXmlBulkUploadPlugin::getErrorCodeCoreValue(DropFolderXmlBulkUploadErrorCode::MALFORMED_XML_FILE)); } libxml_clear_errors(); $proc = new XSLTProcessor(); $proc->registerPHPFunctions(kXml::getXslEnabledPhpFunctions()); $xsl = new KDOMDocument(); if (!$xsl->loadXML($conversionProfile->getXsl())) { KalturaLog::debug('Could not load xsl ' . $conversionProfile->getXsl()); $errorMessage = kXml::getLibXmlErrorDescription($conversionProfile->getXsl()); throw new Exception(DropFolderXmlBulkUploadPlugin::MALFORMED_XML_FILE_MESSAGE, DropFolderXmlBulkUploadPlugin::getErrorCodeCoreValue(DropFolderXmlBulkUploadErrorCode::MALFORMED_XML_FILE)); } $proc->importStyleSheet($xsl); libxml_clear_errors(); $transformedXml = $proc->transformToXML($origianlXml); if (!$transformedXml) { KalturaLog::debug('Could not transform xml ' . $conversionProfile->getXsl()); $errorMessage = kXml::getLibXmlErrorDescription($conversionProfile->getXsl()); throw new Exception(DropFolderXmlBulkUploadPlugin::MALFORMED_XML_FILE_MESSAGE, DropFolderXmlBulkUploadPlugin::getErrorCodeCoreValue(DropFolderXmlBulkUploadErrorCode::MALFORMED_XML_FILE)); } $xmlDoc = new KDOMDocument(); $res = $xmlDoc->loadXML($transformedXml); if (!$res) { throw new Exception(DropFolderXmlBulkUploadPlugin::MALFORMED_XML_FILE_MESSAGE, DropFolderXmlBulkUploadPlugin::getErrorCodeCoreValue(DropFolderXmlBulkUploadErrorCode::MALFORMED_XML_FILE)); } return $transformedXml; }