* @package tao */ class tao_models_classes_export_RdfExporter extends Configurable implements tao_models_classes_export_ExportHandler { /** * @return EventManager */ protected function getEventManager() { return ServiceManager::getServiceManager()->get(EventManager::SERVICE_ID); } /** * @inheritdoc */ public function getLabel() { return __('RDF'); } /** * @inheritdoc */ public function getExportForm(core_kernel_classes_Resource $resource) { if ($resource instanceof core_kernel_classes_Class) { $formData = ['class' => $resource]; } else { $formData = ['instance' => $resource]; } return (new tao_models_classes_export_RdfExportForm($formData)) ->getForm(); } /** * Run the export process. * * @param array $formValues * @param string $destination * @return string * @throws EasyRdf_Exception * @throws common_exception_Error * @throws Exception */ public function export($formValues, $destination) { if (isset($formValues['filename'], $formValues['resource'])) { $class = new core_kernel_classes_Class($formValues['resource']); $adapter = new tao_helpers_data_GenerisAdapterRdf(); $rdf = $adapter->export($class); if (!empty($rdf)) { $name = $formValues['filename'] . '_' . time() . '.rdf'; $path = tao_helpers_File::concat([$destination, $name]); if (!tao_helpers_File::securityCheck($path, true)) { throw new Exception('Unauthorized file name'); } if (file_put_contents($path, $rdf)) { $this->getEventManager()->trigger(new RdfExportEvent($class)); return $path; } } } return ''; } /** * Exports an array of instances into an rdf string. * * @param array $instances * @return string */ public function getRdfString($instances) { $api = core_kernel_impl_ApiModelOO::singleton(); $rdf = ''; $xmls = []; foreach ($instances as $instance) { $xmls[] = $api->getResourceDescriptionXML($instance->getUri()); } if (count($xmls) === 1) { $rdf = $xmls[0]; } elseif (count($xmls) > 1) { $baseDom = new DomDocument(); $baseDom->formatOutput = true; $baseDom->loadXML($xmls[0]); for ($i = 1, $iMax = count($xmls); $i < $iMax; $i++) { $xmlDoc = new SimpleXMLElement($xmls[$i]); foreach ($xmlDoc->getNamespaces() as $nsName => $nsUri) { if (!$baseDom->documentElement->hasAttribute('xmlns:' . $nsName)) { $baseDom->documentElement->setAttribute('xmlns:' . $nsName, $nsUri); } } $newDom = new DOMDocument(); $newDom->loadXML($xmls[$i]); foreach ($newDom->getElementsByTagNameNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'Description') as $desc) { $newNode = $baseDom->importNode($desc, true); $baseDom->documentElement->appendChild($newNode); } } $rdf = $baseDom->saveXML(); } return $rdf; } }