server/src/app/Repositories/RdfDocumentRepository.php
changeset 19 eadaf0b8f02e
parent 4 f55970e41793
child 20 a9b98b16b053
equal deleted inserted replaced
18:f2a40bbc27f6 19:eadaf0b8f02e
     6 use Log;
     6 use Log;
     7 use CorpusParole\Models\Document;
     7 use CorpusParole\Models\Document;
     8 use CorpusParole\Libraries\CorpusParoleException;
     8 use CorpusParole\Libraries\CorpusParoleException;
     9 use CorpusParole\Libraries\Sparql\SparqlClient;
     9 use CorpusParole\Libraries\Sparql\SparqlClient;
    10 
    10 
       
    11 use EasyRdf\Graph;
       
    12 
    11 use Illuminate\Pagination\LengthAwarePaginator;
    13 use Illuminate\Pagination\LengthAwarePaginator;
    12 use Illuminate\Pagination\Paginator;
    14 use Illuminate\Pagination\Paginator;
    13 
    15 
    14 /**
    16 /**
    15  * Implement the DocumentRepository using EasyRdf
    17  * Implement the DocumentRepository using EasyRdf
    16  * TODO: cetainly split the transaction management (+add, +delete +transaction ) to an extarnal class -> for this extend the sparql client.
    18  * TODO: certainly split the transaction management (+add, +delete +transaction ) to an external class -> for this extend the sparql client.
    17  */
    19  */
    18 class RdfDocumentRepository implements DocumentRepository {
    20 class RdfDocumentRepository implements DocumentRepository {
    19 
    21 
    20     private $sparqlClient;
    22     private $sparqlClient;
    21 
    23 
    31         $docs = $this->sparqlClient->query($query);
    33         $docs = $this->sparqlClient->query($query);
    32 
    34 
    33         $data = [];
    35         $data = [];
    34 
    36 
    35         foreach ($docs as $doc) {
    37         foreach ($docs as $doc) {
    36             array_push($data, new Document($doc->uri->getUri()));
    38             $newGraph = new Graph($doc->uri->getUri());
       
    39             $newGraph->add($doc->uri, "rdf:type", $newGraph->resource("http://www.openarchives.org/ore/terms/Aggregation"));
       
    40             $newGraph->add($doc->uri, "http://www.europeana.eu/schemas/edm/aggregatedCHO", $doc->doc);
       
    41             $newGraph->add($doc->doc, "rdf:type", $newGraph->resource("http://www.europeana.eu/schemas/edm/ProvidedCHO"));
       
    42             $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/title", $doc->title);
       
    43             if(isset($doc->issued)) {
       
    44                 $newGraph->add($doc->doc, "http://purl.org/dc/terms/issued", $doc->issued);
       
    45             }
       
    46             if(isset($doc->modified)) {
       
    47                 $newGraph->add($doc->doc, "http://purl.org/dc/terms/modified", $doc->modified);
       
    48             }
       
    49             array_push($data, new Document($doc->uri->getUri(), $newGraph));
    37         }
    50         }
    38 
    51 
    39         return $data;
    52         return $data;
    40     }
    53     }
    41 
    54 
    42     public function all() {
    55     public function all() {
    43 
    56 
    44         return $this->queryDocs("SELECT DISTINCT ?uri WHERE { GRAPH ?uri { ?s ?p ?o } } ORDER BY ?uri");
    57         return $this->queryDocs(
    45 
    58         "SELECT DISTINCT ?uri ?doc ?title ?issued ?modified".
       
    59         "    WHERE {".
       
    60         "        GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
       
    61         "        ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
       
    62         "        OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
       
    63         "        OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} }".
       
    64         "    } ORDER BY ?uri"
       
    65         );
    46     }
    66     }
    47 
    67 
    48     public function get($id) {
    68     public function get($id) {
    49 
    69 
    50         $docUri = Config::get('corpusparole.cocoon_doc_id_base_uri').$id;
    70         $docUri = Config::get('corpusparole.corpus_doc_id_base_uri').$id;
    51 
    71 
    52         // We want the CBD (Concise Bounded Description, cf. http://www.w3.org/Submission/CBD/)
    72         // We want the CBD (Concise Bounded Description, cf. http://www.w3.org/Submission/CBD/)
    53         // WARNING: This seems to work in sesame for our dataset.
    73         // WARNING: This seems to work in sesame for our dataset.
    54         $doc = $this->sparqlClient->query(
    74         $doc = $this->sparqlClient->query(
    55             "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <$docUri> { ?s ?p ?o } }"
    75             "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <$docUri> { ?s ?p ?o } }"
    92             throw $e;
   112             throw $e;
    93         }
   113         }
    94     }
   114     }
    95 
   115 
    96     public function getCount() {
   116     public function getCount() {
    97         $res = $this->sparqlClient->query("SELECT (COUNT (DISTINCT ?g) as ?count) WHERE { GRAPH ?g { ?s ?p ?o } }");
   117         $res = $this->sparqlClient->query("SELECT (COUNT (DISTINCT ?g) as ?count) WHERE { GRAPH ?g { ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> } }");
    98         assert(!is_null($res) && $res->count()==1);
   118         assert(!is_null($res) && $res->count()==1);
    99         return $res[0]->count->getValue();
   119         return $res[0]->count->getValue();
   100     }
   120     }
   101 
   121 
   102     //SELECT ?g WHERE { GRAPH ?g { ?s ?p ?o } }
   122     //SELECT ?g WHERE { GRAPH ?g { ?s ?p ?o } }
   108      * @param  string  $pageName
   128      * @param  string  $pageName
   109      * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
   129      * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
   110      */
   130      */
   111     public function paginateAll($perPage = 15, $pageName = 'page')
   131     public function paginateAll($perPage = 15, $pageName = 'page')
   112     {
   132     {
       
   133         assert(is_numeric($perPage));
       
   134 
   113         $page = Paginator::resolveCurrentPage($pageName);
   135         $page = Paginator::resolveCurrentPage($pageName);
       
   136 
       
   137         assert(is_numeric($page));
   114 
   138 
   115         $total = $this->getCount();
   139         $total = $this->getCount();
   116 
   140 
   117         $offset = max(0,($page - 1) * $perPage);
   141         $offset = max(0,($page - 1) * $perPage);
   118 
   142         
   119         $query = "SELECT DISTINCT ?uri WHERE { GRAPH ?uri { ?s ?p ?o } } ORDER BY ?uri OFFSET $offset LIMIT $perPage";
   143         $query =
       
   144             "SELECT DISTINCT ?uri ?doc ?title ?issued ?modified".
       
   145             "    WHERE {".
       
   146             "        GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
       
   147             "        ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
       
   148             "        OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
       
   149             "        OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} }".
       
   150             "    } ORDER BY ?uri OFFSET $offset LIMIT $perPage";
   120 
   151 
   121         $results = $this->queryDocs($query);
   152         $results = $this->queryDocs($query);
   122 
   153 
   123         return new LengthAwarePaginator($results, $total, $perPage, $page, [
   154         return new LengthAwarePaginator($results, $total, $perPage, $page, [
   124             'path' => Paginator::resolveCurrentPath(),
   155             'path' => Paginator::resolveCurrentPath(),