예제 #1
0
  /**
   * Creates an EasyRdf_Graph object from the given URI.
   *
   * @param string $uri
   *     URL of a web resource or path of the cached file.
   * @param string $type
   *    Format of the document.
   *
   * @throws \Doctrine\Common\Proxy\Exception\InvalidArgumentException
   *    If invalid type or URL is passed as parameters.
   */
  protected function createGraph($uri, $type) {
    /*
     * Initialize an EasyRdf_Graph object using
     * _construct(string $uri = null, string $data = null,
     *     string $format = null)
     */
    if (!is_string($type) or $type == NULL or $type == '') {
      throw new InvalidArgumentException("\$type should be a string and cannot be null or empty");
    }
    if (!is_string($uri) or $uri == NULL or $uri == '') {
      throw new InvalidArgumentException("\$uri should be a string and cannot be null or empty");
    }

    try {
      if (preg_match('#^http#i', $uri) === 1) {
        $this->graph = new \EasyRdf_Graph($uri, NULL, $type);
        $this->graph->load();
      }
      else {
        $this->graph = new \EasyRdf_Graph(NULL);
        $this->graph->parseFile($uri);
      }
      $this->iterateGraph();
    }
    catch (\Exception $e) {
      throw new InvalidArgumentException("Invalid uri + $e");
    }

  }
예제 #2
0
 /**
  * Retrieve the latest RDFA version of schema.org and converts it to JSON-LD.
  *
  * Note: caches the file in data and retrieves it from there as long as it exists.
  */
 private function getJSONVersionOfSchema()
 {
     // Set cachefile
     $cacheFile = dirname(dirname(__DIR__)) . '/data/schemaorg.cache';
     if (!file_exists($cacheFile)) {
         // Create dir
         if (!file_exists(dirname($cacheFile))) {
             mkdir(dirname($cacheFile), 0777, true);
         }
         // Load RDFA Schema
         $graph = new \EasyRdf_Graph(self::RDFA_SCHEMA);
         $graph->load(self::RDFA_SCHEMA, 'rdfa');
         // Lookup the output format
         $format = \EasyRdf_Format::getFormat('jsonld');
         // Serialise to the new output format
         $output = $graph->serialise($format);
         if (!is_scalar($output)) {
             $output = var_export($output, true);
         }
         $this->schema = \ML\JsonLD\JsonLD::compact($graph->serialise($format), 'http://schema.org/');
         // Write cache file
         file_put_contents($cacheFile, serialize($this->schema));
     } else {
         $this->schema = unserialize(file_get_contents($cacheFile));
     }
 }
예제 #3
0
 /** Get the data for reuse based off sparql endpoint
  * @access public
  * @return array $data
  * */
 public function getInfo($identifier)
 {
     $key = md5($identifier . 'ocre');
     $uri = self::CRRO . $identifier;
     if (!$this->getCache()->test($key)) {
         EasyRdf_Namespace::set('nm', 'http://nomisma.org/id/');
         EasyRdf_Namespace::set('nmo', 'http://nomisma.org/ontology#');
         EasyRdf_Namespace::set('skos', 'http://www.w3.org/2004/02/skos/core#');
         EasyRdf_Namespace::set('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
         $request = new EasyRdf_Http_Client();
         $request->setUri($uri);
         $response = $request->request()->getStatus();
         if ($response == 200) {
             $graph = new EasyRdf_Graph($uri);
             $graph->load();
             $data = $graph->resource($uri);
             $this->getCache()->save($data);
         } else {
             $data = NULL;
         }
     } else {
         $data = $this->getCache()->load($key);
     }
     return $data;
 }
예제 #4
0
 /** Fetch a named graph from the graph store
  *
  * The URI can either be a full absolute URI or
  * a URI relative to the URI of the graph store.
  *
  * @param string $uriRef The URI of graph desired
  * @return object EasyRdf_Graph The graph requested
  */
 public function get($uriRef)
 {
     $graphUri = EasyRdf_Utils::resolveUriReference($this->_uri, $uriRef);
     $dataUrl = $this->urlForGraph($graphUri);
     $graph = new EasyRdf_Graph($graphUri);
     $graph->load($dataUrl);
     return $graph;
 }
 /** Fetch a named graph from the graph store
  *
  * The URI can either be a full absolute URI or
  * a URI relative to the URI of the graph store.
  *
  * @param string $uriRef The URI of graph desired
  * @return object EasyRdf_Graph The graph requested
  */
 public function get($uriRef)
 {
     $graphUri = $this->_parsedUri->resolve($uriRef)->toString();
     $dataUrl = $this->urlForGraph($graphUri);
     $graph = new EasyRdf_Graph($graphUri);
     $graph->load($dataUrl);
     return $graph;
 }
예제 #6
0
 public function actionEasyRDF()
 {
     $this->layout = "test";
     $foaf = new EasyRdf_Graph("http://njh.me/foaf.rdf");
     $foaf->load();
     $me = $foaf->primaryTopic();
     echo "My name is: " . $me->get('foaf:name') . "\n";
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $relations = [];
     $schemaOrg = new \EasyRdf_Graph();
     $schemaOrg->load(TypesGeneratorConfiguration::SCHEMA_ORG_RDFA_URL, 'rdfa');
     $relations[] = $schemaOrg;
     $goodRelations = [new \SimpleXMLElement(TypesGeneratorConfiguration::GOOD_RELATIONS_OWL_URL, 0, true)];
     $goodRelationsBridge = new GoodRelationsBridge($goodRelations);
     $cardinalitiesExtractor = new CardinalitiesExtractor($relations, $goodRelationsBridge);
     $result = $cardinalitiesExtractor->extract();
     $output->writeln(json_encode($result, JSON_PRETTY_PRINT));
 }
예제 #8
0
 /** Get data from the endpoint
  * @access protected
  * @return string
  */
 protected function getData()
 {
     $key = md5($this->_uri);
     if (!$this->_cache->test($key)) {
         $graph = new EasyRdf_Graph(self::URI . $this->_uri . self::SUFFIX);
         $graph->load();
         $data = $graph->resource(self::URI . $this->_uri);
         $this->_cache->save($data);
     } else {
         $data = $this->_cache->load($key);
     }
     EasyRdf_Namespace::set('dcterms', 'http://purl.org/dc/terms/');
     EasyRdf_Namespace::set('pleiades', 'http://pleiades.stoa.org/places/vocab#');
     return $data;
 }
예제 #9
0
 /** Get the graph to parse
  * @access protected
  * @returns object
  */
 protected function getData()
 {
     $key = md5($this->_uri);
     if (!$this->_cache->test($key)) {
         $graph = new EasyRdf_Graph($this->_uri);
         $graph->load();
         $data = $graph->resource($this->_uri);
         $this->_cache->save($data);
     } else {
         $data = $this->_cache->load($key);
     }
     EasyRdf_Namespace::set('dbpediaowl', 'http://dbpedia.org/ontology/');
     EasyRdf_Namespace::set('dbpprop', 'http://dbpedia.org/property/');
     EasyRdf_Namespace::set('dbpedia', 'http://dbpedia.org/resource/');
     return $data;
 }
예제 #10
0
 public function queryWorldCatFromOCLC()
 {
     $oclc = (string) (int) $this->OCLC;
     $url = 'http://www.worldcat.org/oclc/' . $oclc;
     $rdf = new EasyRdf_Graph($url . '.rdf');
     $rdf->load();
     $resources = $rdf->resources();
     $book = $resources[$url];
     //Core info
     $this->Title = (string) $book->getLiteral('schema:name');
     $publisherData = $book->get('schema:publisher');
     if ($publisherData) {
         $this->Publisher = (string) $publisherData->get('schema:name');
     }
     //Publishing date
     $date = (string) $book->getLiteral('schema:datePublished');
     if (strlen($date) == 4) {
         $this->YYYY = $date;
     } else {
         echo '<div class="alert-box">Publishing date: ', $date, " / check the template, the code doesn't know how to parse this format and only made a guess. ", '<a href="" class="close">&times;</a></div>';
         $date = date_parse($date);
         $this->YYYY = $date['year'];
         $this->MM = $date['month'];
         $this->DD = $date['day'];
     }
     //Authors
     $this->Authors = [];
     //TODO: look type mapping
     $contributors = $book->allResources('schema:contributor');
     foreach ($contributors as $contributor) {
         $this->Authors[] = [(string) $contributor->get('schema:givenName'), (string) $contributor->get('schema:familyName')];
     }
     //Kludge for library:placeOfPublication
     //We have generally two links, one for the city, one for the country.
     //Only the city has a schema:name, the country is only a reference.
     $rdf_content = file_get_contents($url . '.rdf');
     if (preg_match_all('@<library:placeOfPublication rdf:resource="(.*)"/>@', $rdf_content, $matches)) {
         foreach ($matches[1] as $place) {
             if ($cityCandidate = (string) $resources[$place]->get('schema:name')) {
                 $this->Place = $cityCandidate;
                 break;
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $configArgument = $input->getArgument('config');
     if ($configArgument) {
         $parser = new Parser();
         $config = $parser->parse(file_get_contents($configArgument));
         unset($parser);
     } else {
         $config = [];
     }
     $processor = new Processor();
     $configuration = new TypesGeneratorConfiguration();
     $processedConfiguration = $processor->processConfiguration($configuration, [$config]);
     $processedConfiguration['output'] = realpath($input->getArgument('output'));
     if (!$processedConfiguration['output']) {
         throw new \RuntimeException('The specified output is invalid');
     }
     $graphs = [];
     foreach ($processedConfiguration['rdfa'] as $rdfa) {
         $graph = new \EasyRdf_Graph();
         if ('http://' === substr($rdfa['uri'], 0, 7) || 'https://' === substr($rdfa['uri'], 0, 8)) {
             $graph->load($rdfa['uri'], $rdfa['format']);
         } else {
             $graph->parseFile($rdfa['uri'], $rdfa['format']);
         }
         $graphs[] = $graph;
     }
     $relations = [];
     foreach ($processedConfiguration['relations'] as $relation) {
         $relations[] = new \SimpleXMLElement($relation, 0, true);
     }
     $goodRelationsBridge = new GoodRelationsBridge($relations);
     $cardinalitiesExtractor = new CardinalitiesExtractor($graphs, $goodRelationsBridge);
     $ucfirstFilter = new \Twig_SimpleFilter('ucfirst', 'ucfirst');
     $loader = new \Twig_Loader_Filesystem(__DIR__ . '/../../templates/');
     $twig = new \Twig_Environment($loader, ['autoescape' => false, 'debug' => $processedConfiguration['debug']]);
     $twig->addFilter($ucfirstFilter);
     if ($processedConfiguration['debug']) {
         $twig->addExtension(new \Twig_Extension_Debug());
     }
     $logger = new ConsoleLogger($output);
     $entitiesGenerator = new TypesGenerator($twig, $logger, $graphs, $cardinalitiesExtractor, $goodRelationsBridge);
     $entitiesGenerator->generate($processedConfiguration);
 }
예제 #12
0
 /** Get the data for rendering
  * @access public
  * @return
  * */
 public function getData()
 {
     $key = md5($this->getUri());
     if (!$this->getCache()->test($key)) {
         $request = new EasyRdf_Http_Client();
         $request->setUri($this->getUri());
         $response = $request->request()->getStatus();
         if ($response == 200) {
             $graph = new EasyRdf_Graph($this->_uri);
             $graph->load();
             $data = $graph->resource($this->_uri);
         } else {
             $data = NULL;
         }
         $this->getCache()->save($data);
     } else {
         $data = $this->getCache()->load($key);
     }
     return $data;
 }
    print "<div style='margin: 10px'>\n";
    print form_tag();
    print label_tag('data', 'Input Data: ') . '<br />' . text_area_tag('data', '', array('cols' => 80, 'rows' => 10)) . "<br />\n";
    print label_tag('uri', 'or Uri: ') . text_field_tag('uri', 'http://www.dajobe.org/foaf.rdf', array('size' => 80)) . "<br />\n";
    print label_tag('input_format', 'Input Format: ') . select_tag('input_format', $input_format_options) . "<br />\n";
    print label_tag('output_format', 'Output Format: ') . select_tag('output_format', $output_format_options) . "<br />\n";
    print label_tag('raw', 'Raw Output: ') . check_box_tag('raw') . "<br />\n";
    print reset_tag() . submit_tag();
    print form_end_tag();
    print "</div>\n";
}
if (isset($_REQUEST['uri']) or isset($_REQUEST['data'])) {
    // Parse the input
    $graph = new EasyRdf_Graph($_REQUEST['uri']);
    if (empty($_REQUEST['data'])) {
        $graph->load($_REQUEST['uri'], $_REQUEST['input_format']);
    } else {
        $graph->parse($_REQUEST['data'], $_REQUEST['input_format'], $_REQUEST['uri']);
    }
    // Lookup the output format
    $format = EasyRdf_Format::getFormat($_REQUEST['output_format']);
    // Serialise to the new output format
    $output = $graph->serialise($format);
    if (!is_scalar($output)) {
        $output = var_export($output, true);
    }
    // Send the output back to the client
    if (isset($_REQUEST['raw'])) {
        header('Content-Type: ' . $format->getDefaultMimeType());
        print $output;
    } else {
예제 #14
0
 function del_friend($uri, $format = 'rdfxml')
 {
     $uri = urldecode($uri);
     $path = $this->get_local_path($this->webid);
     // Create the new graph object in which we store data
     $graph = new EasyRdf_Graph($this->webid);
     $graph->load();
     $person = $graph->resource($this->webid);
     $graph->deleteResource($person, 'foaf:knows', $uri);
     // write profile to file
     $data = $graph->serialise($format);
     if (!is_scalar($data)) {
         $data = var_export($data, true);
     } else {
         $data = print_r($data, true);
     }
     $pf = fopen($path . '/foaf.rdf', 'w') or die('Cannot open profile RDF file!');
     fwrite($pf, $data);
     fclose($pf);
     $pf = fopen($path . '/foaf.txt', 'w') or die('Cannot open profile TXT file!');
     fwrite($pf, $data);
     fclose($pf);
     // get the user's name
     $friend = new MyProfile($uri, $this->base_uri, SPARQL_ENDPOINT);
     $friend->load();
     // everything is fine
     return success("You have just removed " . $friend->get_name() . " from your list of friends.");
 }
예제 #15
0
 public function getData()
 {
     $graph = new EasyRdf_Graph($this->getUri());
     $graph->load();
 }
 /**
  * Try to load a given URI as RDF Graph
  * @param string $ps_uri
  * @return bool|EasyRdf_Graph
  */
 static function getURIAsRDFGraph($ps_uri)
 {
     if (!$ps_uri) {
         return false;
     }
     if (CompositeCache::contains($ps_uri, 'GettyLinkedDataRDFGraphs')) {
         return CompositeCache::fetch($ps_uri, 'GettyLinkedDataRDFGraphs');
     }
     try {
         $o_graph = new EasyRdf_Graph("http://vocab.getty.edu/download/rdf?uri={$ps_uri}.rdf");
         $o_graph->load();
     } catch (Exception $e) {
         return false;
     }
     CompositeCache::save($ps_uri, $o_graph, 'GettyLinkedDataRDFGraphs');
     return $o_graph;
 }
 public function testLoadRedirect()
 {
     // Check that loading the same URL as a redirected request
     // doesn't result in multiple HTTP GETs
     $this->_client->addMockRedirect('GET', 'http://www.example.org/', 'http://www.example.com/', 301);
     $this->_client->addMockRedirect('GET', 'http://www.example.com/', 'http://www.example.com/foaf.rdf', 303);
     $this->_client->addMockOnce('GET', 'http://www.example.com/foaf.rdf', readFixture('foaf.json'));
     $graph = new EasyRdf_Graph();
     $this->assertSame(0, $graph->countTriples());
     $this->assertSame(14, $graph->load('http://www.example.org/', 'json'));
     $this->assertSame(14, $graph->countTriples());
     $this->assertSame(0, $graph->load('http://www.example.com/foaf.rdf', 'json'));
     $this->assertSame(14, $graph->countTriples());
     $this->assertStringEquals('Joe Bloggs', $graph->get('http://www.example.com/joe#me', 'foaf:name'));
 }
 public function testLoadMockHttpClient()
 {
     $this->_client->addMock('GET', 'http://www.example.com/', readFixture('foaf.json'));
     $graph = new EasyRdf_Graph('http://www.example.com/');
     $graph->load();
     $this->assertStringEquals('Joe Bloggs', $graph->get('http://www.example.com/joe#me', 'foaf:name'));
 }
 * the primary topic of the document (me, Nicholas Humfrey) is returned
 * and then used to display my name.
 *
 * @package    EasyRdf
 * @copyright  Copyright (c) 2009-2011 Nicholas J Humfrey
 * @license    http://unlicense.org/
 */
set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/');
require_once "EasyRdf.php";
?>
<html>
<head>
  <title>Basic FOAF example</title>
</head>
<body>

<?php 
$foaf = new EasyRdf_Graph("http://www.aelius.com/njh/foaf.rdf");
$foaf->load();
$me = $foaf->primaryTopic();
?>

<p>
  My name is: <?php 
echo $me->get('foaf:name');
?>
</p>

</body>
</html>
예제 #20
0
 public function testLoadDuplicateBNodes()
 {
     $foafName = 'http://xmlns.com/foaf/0.1/name';
     $bnodeA = array('_:genid1' => array($foafName => array(array('type' => 'literal', 'value' => 'A'))));
     $bnodeB = array('_:genid1' => array($foafName => array(array('type' => 'literal', 'value' => 'B'))));
     $graph = new EasyRdf_Graph();
     $graph->load('file://bnodeA', $bnodeA);
     $graph->load('file://bnodeB', $bnodeB);
     $this->assertStringEquals('A', $graph->resource('_:eid1')->get('foaf:name'));
     $this->assertStringEquals('B', $graph->resource('_:eid2')->get('foaf:name'));
 }
?>
<br />
  <?php 
echo reset_tag();
?>
 <?php 
echo submit_tag();
?>
  <?php 
echo form_end_tag();
?>
</div>

<?php 
if (isset($_REQUEST['uri']) or isset($_REQUEST['data'])) {
    $graph = new EasyRdf_Graph($_REQUEST['uri']);
    if (empty($_REQUEST['data'])) {
        $graph->load();
    } else {
        $graph->parse($_REQUEST['data'], $_REQUEST['input_format'], $_REQUEST['uri']);
    }
    $output = $graph->serialise($_REQUEST['output_format']);
    if (!is_scalar($output)) {
        $output = var_export($output, true);
    }
    print "<pre>" . htmlspecialchars($output) . "</pre>";
}
?>
</body>
</html>
예제 #22
0
<?php

require_once 'common.php';
require_once '../cpss.php';
require_once '../vendor/autoload.php';
$specimen_uri = $_GET['specimen_uri'];
$doc = new EasyRdf_Graph($_GET['rdf_uri']);
$doc->load();
?>
<h2>Parsing RDF</h2>

<h3>CETAF Specimen Preview Profile</h3>
<p>Prefered URI's are listed first followed by any depricated URIs that may contain the required values.</p>
<table>
    <tr>
        <th>CSPP Element</th>
        <th>Mandatory</th>
        <th>RDF resource URI</th>
        <th>Resource Expected</th>
        <th>Value</th>
    </tr>    
<?php 
foreach ($cpss as $prop) {
    echo '<tr>';
    echo '<td>' . $prop->display_name . '</td>';
    echo '<td>' . ($prop->required ? 'Yes' : 'No') . '</td>';
    // work out which uri we will use
    echo '<td>';
    $is_first = true;
    $is_resource = false;
    foreach ($prop->qnames as $uri) {
예제 #23
0
  <title>phpspecgen</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>

<?php 
$options = getopt('u:');
if (!empty($_REQUEST['uri'])) {
    $uri = $_REQUEST['uri'];
} elseif (array_key_exists('u', $options)) {
    $uri = $options['u'];
}
if (!empty($uri)) {
    // Parse the document
    $graph = new EasyRdf_Graph($uri);
    $graph->load($uri);
    // Get the first ontology in the document
    $vocab = $graph->get('owl:Ontology', '^rdf:type');
    if (!isset($vocab)) {
        print "<p>Error: No OWL ontologies defined at that URL.</p>\n";
    } else {
        // FIXME: register the preferredNamespacePrefix
        print $vocab->htmlHeader();
        print $vocab->htmlSummaryOfTerms();
        print $vocab->htmlTerms('Phpspecgen_Class', 'Classes');
        print $vocab->htmlTerms('Phpspecgen_Property', 'Properties');
        print $graph->dump();
    }
} else {
    $examples = array('FOAF' => 'http://xmlns.com/foaf/spec/', 'DOAP' => 'http://usefulinc.com/ns/doap#', 'LODE' => 'http://linkedevents.org/ontology/', 'Ordered List Ontology' => 'http://purl.org/ontology/olo/core#', 'Whisky Vocabulary' => 'http://vocab.org/whisky/terms.rdf', 'Sport Ontology' => 'http://www.bbc.co.uk/ontologies/sport/2011-02-17.rdf', 'Music Ontology' => 'http://purl.org/ontology/mo/', 'Programme Ontology' => 'http://www.bbc.co.uk/ontologies/programmes/2009-09-07.rdf');
    print "<h1>phpspecgen</h1>\n";
예제 #24
0
 /** Fetch a named graph from the graph store
  *
  * The URI can either be a full absolute URI or
  * a URI relative to the URI of the graph store.
  *
  * @param string $uriRef The URI of graph desired
  * @return EasyRdf_Graph The graph requested
  */
 public function get($uriRef)
 {
     if ($uriRef === self::DEFAULT_GRAPH) {
         $dataUrl = $this->urlForGraph(self::DEFAULT_GRAPH);
         $graph = new EasyRdf_Graph();
     } else {
         $graphUri = $this->parsedUri->resolve($uriRef)->toString();
         $dataUrl = $this->urlForGraph($graphUri);
         $graph = new EasyRdf_Graph($graphUri);
     }
     $graph->load($dataUrl);
     return $graph;
 }
예제 #25
0
 /** Get the data from rdf graph
  * @access protected
  * @return object
  */
 protected function getData()
 {
     $uri = $this->getUri();
     $key = md5($uri);
     if (!$this->getCache()->test($key)) {
         $graph = new EasyRdf_Graph($uri);
         $graph->load();
         $data = $graph->resource($uri);
         $this->getCache()->save($data);
     } else {
         $data = $this->getCache()->load($key);
     }
     $this->registerNameSpaces();
     return $data;
 }