/** * Serialise an EasyRdf_Graph to the RDF format of choice. * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); $ntriples = parent::serialise($graph, 'ntriples'); // Open a pipe to the rapper command $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w")); // Hack to produce more concise RDF/XML if ($format == 'rdfxml') { $format = 'rdfxml-abbrev'; } $process = proc_open(escapeshellcmd($this->_rapperCmd) . " --quiet " . " --input ntriples " . " --output " . escapeshellarg($format) . " - " . 'unknown://', $descriptorspec, $pipes, '/tmp', null); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // 2 => readable handle connected to child stderr fwrite($pipes[0], $ntriples); fclose($pipes[0]); $output = stream_get_contents($pipes[1]); fclose($pipes[1]); $error = stream_get_contents($pipes[2]); fclose($pipes[2]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $returnValue = proc_close($process); if ($returnValue) { throw new EasyRdf_Exception("Failed to convert RDF: " . $error); } } else { throw new EasyRdf_Exception("Failed to execute rapper command."); } return $output; }
/** * Serialise an EasyRdf_Graph to the RDF format of choice. * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); $ntriples = parent::serialise($graph, 'ntriples'); // Hack to produce more concise RDF/XML if ($format == 'rdfxml') { $format = 'rdfxml-abbrev'; } return EasyRdf_Utils::execCommandPipe($this->_rapperCmd, array('--quiet', '--input', 'ntriples', '--output', $format, '-', 'unknown://'), $ntriples); }