Exemple #1
0
 /**
  * Determines type of a entity.
  *
  * @param  string $entity Entity string.
  * @return string|null    Returns either literal, typed-literal, uri or var. Returns null if it couldnt be
  *                        determined.
  */
 public function determineEntityType($entity)
 {
     // remove braces at the beginning (only if $entity looks like <http://...>)
     if ('<' == substr($entity, 0, 1)) {
         $entity = str_replace(array('>', '<'), '', $entity);
     }
     // checks if $entity is an URL
     if (true === NodeUtils::simpleCheckURI($entity)) {
         return 'uri';
         // checks if ^^< is in $entity OR if $entity is surrounded by quotation marks
     } elseif (false !== strpos($entity, '"^^<') || '"' == substr($entity, 0, 1) && '"' == substr($entity, strlen($entity) - 1, 1)) {
         return 'typed-literal';
         // checks if $entity is an URL, which was written with prefix, such as rdfs:label
     } elseif (false !== strpos($entity, ':')) {
         return 'uri';
         // checks if "@ is in $entity
     } elseif (false !== strpos($entity, '"@')) {
         return 'literal';
         // checks if $entity is a string; only strings can be a variable
     } elseif (true === is_string($entity)) {
         return 'var';
         // unknown type
     } else {
         return null;
     }
 }
Exemple #2
0
 public function testSimpleCheckURI()
 {
     $this->assertFalse(NodeUtils::simpleCheckURI(''));
     $this->assertFalse(NodeUtils::simpleCheckURI('http//foobar/'));
     $this->assertTrue(NodeUtils::simpleCheckURI('http:foobar/'));
     $this->assertTrue(NodeUtils::simpleCheckURI('http://foobar/'));
     $this->assertTrue(NodeUtils::simpleCheckURI('http://*****:*****@foobar/'));
 }
Exemple #3
0
 /**
  * @param  string $uri URI of the new named node.
  * @return NamedNode
  */
 public function createNamedNode($uri)
 {
     if ($uri === null) {
         throw new \Exception('Can\'t initialize node with null.');
     }
     if (!NodeUtils::simpleCheckURI($uri)) {
         throw new \Exception('Invalid URI was given for RDF NamedNode creation.');
     }
     // TODO catch invalid URIs
     $world = librdf_php_get_world();
     $uri = librdf_new_uri($world, $uri);
     $redlandNode = librdf_new_node_from_uri($world, $uri);
     if ($redlandNode === null) {
         throw new \Exception('Initialization of redland node failed.');
     }
     return new NamedNode($redlandNode);
 }
Exemple #4
0
 /**
  * Returns a list of all available graph URIs of the store. It can also respect access control,
  * to only returned available graphs in the current context. But that depends on the implementation
  * and can differ.
  *
  * @return array Simple array of key-value-pairs, which consists of graph URIs as key and NamedNode
  *               instance as value.
  */
 public function getGraphs()
 {
     $g2t = $this->configuration['table-prefix'] . '_g2t';
     $id2val = $this->configuration['table-prefix'] . '_id2val';
     // collects all values which have an ID (column g) in the g2t table.
     $query = 'SELECT id2val.val AS graphUri
         FROM ' . $g2t . ' g2t
         LEFT JOIN ' . $id2val . ' id2val ON g2t.g = id2val.id
         GROUP BY g';
     // send SQL query
     $result = $this->store->queryDB($query, $this->store->getDBCon());
     $graphs = array();
     // collect graph URI's
     while ($row = $result->fetch_assoc()) {
         if (NodeUtils::simpleCheckURI($row['graphUri'])) {
             $graphs[$row['graphUri']] = $this->nodeFactory->createNamedNode($row['graphUri']);
         }
     }
     return $graphs;
 }
Exemple #5
0
 /**
  * Transforms a statement array given by EasyRdf to a Saft StatementIterator instance.
  *
  * @param  array             $rdfPhp
  * @return StatementIterator
  */
 protected function rdfPhpToStatementIterator(array $rdfPhp)
 {
     $statements = array();
     // go through all subjects
     foreach ($rdfPhp as $subject => $predicates) {
         // predicates associated with the subject
         foreach ($predicates as $property => $objects) {
             // object(s)
             foreach ($objects as $object) {
                 /**
                  * Create subject node
                  */
                 if (true === NodeUtils::simpleCheckURI($subject)) {
                     $s = $this->nodeFactory->createNamedNode($subject);
                 } else {
                     $s = $this->nodeFactory->createLiteral($subject);
                 }
                 /**
                  * Create predicate node
                  */
                 if (true === NodeUtils::simpleCheckURI($property)) {
                     $p = $this->nodeFactory->createNamedNode($property);
                 } else {
                     $p = $this->nodeFactory->createLiteral($property);
                 }
                 /*
                  * Create object node
                  */
                 // URI
                 if (NodeUtils::simpleCheckURI($object['value'])) {
                     $o = $this->nodeFactory->createNamedNode($object['value']);
                     // datatype set
                 } elseif (isset($object['datatype'])) {
                     $o = $this->nodeFactory->createLiteral($object['value'], $object['datatype']);
                     // lang set
                 } elseif (isset($object['lang'])) {
                     $o = $this->nodeFactory->createLiteral($object['value'], 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString', $object['lang']);
                 } else {
                     $o = $this->nodeFactory->createLiteral($object['value']);
                 }
                 // build and add statement
                 $statements[] = $this->statementFactory->createStatement($s, $p, $o);
             }
         }
     }
     return new ArrayStatementIteratorImpl($statements);
 }
Exemple #6
0
 /**
  * Establish a connection to the endpoint and authenticate.
  *
  * @return Client Setup HTTP client.
  */
 protected function openConnection()
 {
     $this->client = new Client();
     $configuration = array_merge(array('authUrl' => '', 'password' => '', 'queryUrl' => '', 'username' => ''), $this->configuration);
     // authenticate only if an authUrl was given.
     if (NodeUtils::simpleCheckURI($configuration['authUrl'])) {
         $this->authenticateOnServer($configuration['authUrl'], $configuration['username'], $configuration['password']);
     }
     // check query URL
     if (false === NodeUtils::simpleCheckUri($configuration['queryUrl'])) {
         throw new \Exception('Parameter queryUrl is not an URI or empty: ' . $configuration['queryUrl']);
     }
     $this->client->setUrl($configuration['queryUrl']);
 }
 /**
  *
  */
 public function install()
 {
     // read configuration file
     $this->configuration = json_decode(file_get_contents($this->configurationFilepath), true);
     if (null == $this->configuration) {
         throw new \Exception('Configuration file contains invalid JSON.');
     }
     if (0 < count($this->configuration['version'])) {
         $requirements = array();
         foreach ($this->configuration['version'] as $version => $versionEntry) {
             $requirements = array_merge($requirements, $this->getRequirements($versionEntry['require']));
         }
         // set folder to install requirements
         if (isset($this->configuration['knowledge-directory'])) {
             $folderForRequirements = $this->defaultRootFolder . $this->configuration['knowledge-directory'] . '/';
         } else {
             $folderForRequirements = $this->defaultRootFolder . 'knowledge/';
         }
         // if there are requirements to install, create knowledge directory first
         if (0 < count($requirements) && false == file_exists($folderForRequirements)) {
             $fileObject = new File($folderForRequirements);
             $fileObject->mkdirs();
         }
         $nodeUtils = new NodeUtils();
         $curl = new Curl();
         $curl->setOpt(CURLOPT_ENCODING, 'gzip');
         $curl->setOpt(CURLOPT_FOLLOWLOCATION, true);
         foreach ($requirements as $name => $requirement) {
             // if a valid local file was given
             if (file_exists($requirement['file'])) {
                 $fileObject = new File($requirement['file']);
                 $fileObject->copy($targetPath);
                 // if a valid URL was given
             } elseif ($nodeUtils->simpleCheckURI($requirement['file'])) {
                 // split name to be able to create all folders
                 $name = explode('/', $name);
                 $vendor = $name[0];
                 $project = $name[1];
                 // remove all maybe existing files
                 if (file_exists($folderForRequirements . $vendor . '/' . $project . '.ttl')) {
                     $fileObject = new File($folderForRequirements . $vendor . '/' . $project . '.ttl');
                     $fileObject->delete();
                 }
                 if (false == file_exists($folderForRequirements . $vendor)) {
                     $fileObject = new File($folderForRequirements . $vendor);
                     $fileObject->mkdirs();
                 }
                 echo PHP_EOL . '- Download ' . $vendor . '/' . $project;
                 if (isset($requirement['file-format'])) {
                     $fileFormat = $requirement['file-format'];
                 } else {
                     $fileFormat = $this->getFileFormat($requirement['file']);
                 }
                 if (null !== $fileFormat) {
                     $curl->download($requirement['file'], $folderForRequirements . $vendor . '/' . $project . '.' . $fileFormat);
                     if ('xml' == $fileFormat) {
                         $fileFormatForParsing = 'rdf-xml';
                     } elseif ('ttl' == $fileFormat) {
                         $fileFormatForParsing = 'turtle';
                     } elseif ('n3' == $fileFormat || 'nt' == $fileFormat) {
                         $fileFormatForParsing = 'n-triples';
                     } else {
                         $fileFormatForParsing = $fileFormat;
                     }
                     if (isset($this->configuration['target-file-serialization']) && $this->configuration['target-file-serialization'] != $fileFormatForParsing) {
                         // get parser suiteable for the given file format
                         $parserFactory = new ParserFactory(new NodeFactoryImpl(), new StatementFactoryImpl());
                         $parser = $parserFactory->createParserFor($fileFormatForParsing);
                         if (null == $parser) {
                             echo ' - Unknown file format given: ' . $fileFormatForParsing . '; Leaving file at : ' . $fileFormat;
                             continue;
                         }
                         // parse file content and transform it into a statement
                         $statementIterator = $parser->parseStreamToIterator($folderForRequirements . $vendor . '/' . $project . '.' . $fileFormat);
                         /* go through iterator and output the first few statements
                                                     $i = 0;
                                                     foreach ($statementIterator as $statement) {
                                                         echo (string)$statement->getSubject()
                                                             . ' ' . (string)$statement->getPredicate()
                                                             . ' ' . (string)$statement->getObject()
                                                             . PHP_EOL;
                         
                                                         if ($i++ == 10) { break; }
                                                     }
                         
                                                     continue;*/
                         // get serializer for target file format
                         $serializerFactory = new SerializerFactory(new NodeFactoryImpl(), new StatementFactoryImpl());
                         $targetFormatForSerialization = $this->configuration['target-file-serialization'];
                         if ('rdf-xml' == $targetFormatForSerialization) {
                             $serializedFileFormat = 'xml';
                         } elseif ('turtle' == $targetFormatForSerialization) {
                             $serializedFileFormat = 'ttl';
                         } elseif ('n-triples' == $targetFormatForSerialization) {
                             $serializedFileFormat = 'n3';
                         } else {
                             $serializedFileFormat = $targetFormatForSerialization;
                         }
                         $targetFile = 'file://' . $folderForRequirements . $vendor . '/' . $project . '.' . $serializedFileFormat;
                         $serializer = $serializerFactory->createSerializerFor($targetFormatForSerialization);
                         $serializer->serializeIteratorToStream($statementIterator, fopen($targetFile, 'w'));
                         if (file_exists($targetFile)) {
                             unlink($folderForRequirements . $vendor . '/' . $project . '.' . $fileFormat);
                         }
                     }
                     echo ' - done';
                 } else {
                     echo ' - unknown file format for the ontology reference: ' . $requirement['file'];
                 }
             }
         }
         echo PHP_EOL;
     } else {
         return 'No version information found. Did you added elements to version array?';
     }
 }