/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $export_types = $this->config('rdf_export.settings')->get('export_types'); // @todo: Check which format are supported by the server? $formats = Format::getFormats(); $list = []; /** @var \EasyRdf\Format $format */ foreach ($formats as $format) { if ($format->getSerialiserClass()) { $list[$format->getName()] = $format->getLabel(); } } $form['export_types'] = array('#type' => 'select', '#title' => t('Export types'), '#options' => $list, '#multiple' => TRUE, '#default_value' => empty($export_types) ? [] : $export_types, '#description' => t('Select the export types for rdf entities.')); return parent::buildForm($form, $form_state); }
} } /** * Serialise an EasyRdf\Graph into RDF format of choice. * * @param Graph $graph An EasyRdf\Graph object. * @param string $format The name of the format to convert to. * @param array $options * * @throws Exception * * @return string The RDF in the new desired format. */ public function serialise($graph, $format, array $options = array()) { parent::checkSerialiseParams($graph, $format); if (array_key_exists($format, self::$supportedTypes)) { $className = self::$supportedTypes[$format]; } else { throw new Exception("EasyRdf\\Serialiser\\Arc does not support: {$format}"); } $serialiser = \ARC2::getSer($className); if ($serialiser) { return $serialiser->getSerializedIndex(parent::serialise($graph, 'php')); } else { throw new Exception("ARC2 failed to get a {$className} serialiser."); } } } Format::register('posh', 'poshRDF');
/** * Builds a list of supported serialization formats. */ protected function getSerializerFormats() { // Many more are supported... // @todo Move this to a settings form. $white_list = $this->config('rdf_export.settings')->get('export_types'); $list = []; $formats = Format::getFormats(); /** @var \EasyRdf\Format $format */ foreach ($formats as $format) { if (!in_array($format->getName(), $white_list)) { continue; } if ($format->getSerialiserClass()) { $list[$format->getName()] = $format; } } return $list; }
/** * Parses a given stream and returns an iterator containing Statement instances representing the * previously read data. The stream parses the data not as a whole but in chunks. * * @param string $inputStream Filename of the stream to parse which contains RDF * serialized data. * @param string $baseUri The base URI of the parsed content. If this URI is null * the inputStreams URL is taken as base URI. * @param string $serialization The serialization of the inputStream. If null is given * the parser will either apply some standard serialization, * or the only one it is supporting, or will try to guess * the correct serialization, or will throw an Exception. * Supported formats are a subset of the following: * json, rdfxml, sparql-xml, rdfa, turtle, ntriples, n3 * @return StatementIterator A StatementIterator containing all the Statements parsed by the parser to * far. * @throws \Exception If the base URI $baseUri is no valid URI. */ public function parseStreamToIterator($inputStream, $baseUri = null, $serialization = null) { $graph = new Graph(); // let EasyRdf guess the format if ($serialization === null) { // use PHP's file:// stream, if its a local file if (false === strpos($inputStream, '://')) { $inputStream = 'file://' . $inputStream; } $serialization = Format::guessFormat(file_get_contents($inputStream)); } else { $serialization = Format::getFormat($serialization); } // if format is still null, throw exception, because we dont know what format the given stream is if (null === $serialization) { throw new \Exception('Either given $format is unknown or i could not guess format.'); } $graph->parseFile($inputStream, $serialization->getName()); // transform parsed data to PHP array return $this->rdfPhpToStatementIterator($graph->toRdfPhp()); }
<?php use EasyRdf\Graph; use EasyRdf\Format; use EasyRdf\Serialiser\GraphViz; $app->get('/image', function () { $graph = Graph::newAndLoad(site_base_uri() . '/asset/dataset/countries.rdf'); $format = Format::getFormat('png'); $viz = new GraphViz(); header("Content-Type: " . $format->getDefaultMimeType()); echo $viz->renderImage($graph, $format); die; })->name('home');
/** * Build http-client object, execute request and return a response * * @param string $processed_query * @param string $type Should be either "query" or "update" * * @return Http\Response|\Zend\Http\Response * @throws Exception */ protected function executeQuery($processed_query, $type) { $client = Http::getDefaultHttpClient(); $client->resetParameters(); // Tell the server which response formats we can parse $sparql_results_types = array('application/sparql-results+json' => 1.0, 'application/sparql-results+xml' => 0.8); if ($type == 'update') { // accept anything, as "response body of a […] update request is implementation defined" // @see http://www.w3.org/TR/sparql11-protocol/#update-success $accept = Format::getHttpAcceptHeader($sparql_results_types); $client->setHeaders('Accept', $accept); $client->setMethod('POST'); $client->setUri($this->updateUri); $client->setRawData($processed_query); $client->setHeaders('Content-Type', 'application/sparql-update'); } elseif ($type == 'query') { $re = '(?:(?:\\s*BASE\\s*<.*?>\\s*)|(?:\\s*PREFIX\\s+.+:\\s*<.*?>\\s*))*' . '(CONSTRUCT|SELECT|ASK|DESCRIBE)[\\W]'; $result = null; $matched = mb_eregi($re, $processed_query, $result); if (false === $matched or count($result) !== 2) { // non-standard query. is this something non-standard? $query_verb = null; } else { $query_verb = strtoupper($result[1]); } if ($query_verb === 'SELECT' or $query_verb === 'ASK') { // only "results" $accept = Format::formatAcceptHeader($sparql_results_types); } elseif ($query_verb === 'CONSTRUCT' or $query_verb === 'DESCRIBE') { // only "graph" $accept = Format::getHttpAcceptHeader(); } else { // both $accept = Format::getHttpAcceptHeader($sparql_results_types); } $client->setHeaders('Accept', $accept); $encodedQuery = 'query=' . urlencode($processed_query); // Use GET if the query is less than 2kB // 2046 = 2kB minus 1 for '?' and 1 for NULL-terminated string on server if (strlen($encodedQuery) + strlen($this->queryUri) <= 2046) { $delimiter = $this->queryUri_has_params ? '&' : '?'; $client->setMethod('GET'); $client->setUri($this->queryUri . $delimiter . $encodedQuery); } else { // Fall back to POST instead (which is un-cacheable) $client->setMethod('POST'); $client->setUri($this->queryUri); $client->setRawData($encodedQuery); $client->setHeaders('Content-Type', 'application/x-www-form-urlencoded'); } } else { throw new Exception('unexpected request-type: ' . $type); } return $client->request(); }