server/src/app/Repositories/RdfDocumentRepository.php
changeset 261 02e2396bcbbc
parent 169 8fddc113095e
child 275 a4d8618c2f1b
equal deleted inserted replaced
260:64caee7ce38d 261:02e2396bcbbc
     5 use Config;
     5 use Config;
     6 use Log;
     6 use Log;
     7 use CorpusParole\Models\DocumentResult;
     7 use CorpusParole\Models\DocumentResult;
     8 use CorpusParole\Models\Document;
     8 use CorpusParole\Models\Document;
     9 use CorpusParole\Libraries\CorpusParoleException;
     9 use CorpusParole\Libraries\CorpusParoleException;
       
    10 use CorpusParole\Libraries\Utils;
    10 use CorpusParole\Libraries\Sparql\SparqlClient;
    11 use CorpusParole\Libraries\Sparql\SparqlClient;
       
    12 
       
    13 
    11 use CorpusParole\Services\LexvoResolverInterface;
    14 use CorpusParole\Services\LexvoResolverInterface;
    12 
    15 
    13 use EasyRdf\Graph;
    16 use EasyRdf\Graph;
    14 
    17 
    15 use Illuminate\Pagination\LengthAwarePaginator;
    18 use Illuminate\Pagination\LengthAwarePaginator;
    19  * Implement the DocumentRepository using EasyRdf
    22  * Implement the DocumentRepository using EasyRdf
    20  * TODO: certainly split the transaction management (+add, +delete +transaction ) to an external class -> for this extend the sparql client.
    23  * TODO: certainly split the transaction management (+add, +delete +transaction ) to an external class -> for this extend the sparql client.
    21  */
    24  */
    22 class RdfDocumentRepository implements DocumentRepository {
    25 class RdfDocumentRepository implements DocumentRepository {
    23 
    26 
       
    27     const ALL_QUERIES = [
       
    28         "SELECT".
       
    29         "    ?uri".
       
    30         "    ?doc".
       
    31         "    ?title".
       
    32         "    ?issued".
       
    33         "    ?modified".
       
    34         "    ?lang".
       
    35         "    (group_concat(distinct ?publisher;separator=\", \") as ?publishers) ".
       
    36         "WHERE {".
       
    37         "GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
       
    38         "    ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
       
    39         "    OPTIONAL {?doc <http://purl.org/dc/elements/1.1/language> ?lang.} ".
       
    40         "    OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
       
    41         "    OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} ".
       
    42         "    OPTIONAL {?doc <http://purl.org/dc/elements/1.1/publisher> ?publisher.} }".
       
    43         "} ".
       
    44         "GROUP BY ?uri ?doc ?title ?issued ?modified ?lang ".
       
    45         "ORDER BY ?uri",
       
    46 
       
    47         "SELECT".
       
    48         "    ?uri".
       
    49         "    ?doc".
       
    50         "    (sample(distinct ?ext) as ?extent) ".
       
    51         "WHERE {".
       
    52         "    GRAPH ?uri {".
       
    53         "        ?s a <http://www.europeana.eu/schemas/edm/WebResource>. ".
       
    54         "        ?uri <http://www.europeana.eu/schemas/edm/isShownBy> ?s. ".
       
    55         "        ?uri <http://www.europeana.eu/schemas/edm/aggregatedCHO> ?doc. ".
       
    56         "        OPTIONAL {?s <http://purl.org/dc/terms/extent> ?ext.}".
       
    57         "    }".
       
    58         "} ".
       
    59         "GROUP BY ?uri ?doc ".
       
    60         "ORDER BY ?uri"
       
    61     ];
       
    62 
    24     private $sparqlClient;
    63     private $sparqlClient;
    25     private $lexvoResolver;
    64     private $lexvoResolver;
    26 
    65 
    27     public function __construct(SparqlClient $sparqlClient, LexvoResolverInterface $lexvoResolver) {
    66     public function __construct(SparqlClient $sparqlClient, LexvoResolverInterface $lexvoResolver) {
    28         $this->sparqlClient = $sparqlClient;
    67         $this->sparqlClient = $sparqlClient;
    31 
    70 
    32     public function getSparqlClient() {
    71     public function getSparqlClient() {
    33         return $this->sparqlClient;
    72         return $this->sparqlClient;
    34     }
    73     }
    35 
    74 
    36     private function queryDocs($query) {
    75     private function getResGraph($doc) {
    37         $docs = $this->sparqlClient->query($query);
    76         $newGraph = new Graph($doc->uri->getUri());
    38 
    77         $newGraph->add($doc->uri, "rdf:type", $newGraph->resource("http://www.openarchives.org/ore/terms/Aggregation"));
    39         $data = [];
    78         $newGraph->add($doc->uri, "http://www.europeana.eu/schemas/edm/aggregatedCHO", $doc->doc);
    40 
    79         $newGraph->add($doc->doc, "rdf:type", $newGraph->resource("http://www.europeana.eu/schemas/edm/ProvidedCHO"));
    41         foreach ($docs as $doc) {
    80         if(isset($doc->title)) {
    42             $newGraph = new Graph($doc->uri->getUri());
       
    43             $newGraph->add($doc->uri, "rdf:type", $newGraph->resource("http://www.openarchives.org/ore/terms/Aggregation"));
       
    44             $newGraph->add($doc->uri, "http://www.europeana.eu/schemas/edm/aggregatedCHO", $doc->doc);
       
    45             $newGraph->add($doc->doc, "rdf:type", $newGraph->resource("http://www.europeana.eu/schemas/edm/ProvidedCHO"));
       
    46             $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/title", $doc->title);
    81             $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/title", $doc->title);
    47             if(isset($doc->lang)) {
    82         }
    48                 $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/language", $doc->lang);
    83         if(isset($doc->lang)) {
    49             }
    84             $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/language", $doc->lang);
    50             if(isset($doc->issued)) {
    85         }
    51                 $newGraph->add($doc->doc, "http://purl.org/dc/terms/issued", $doc->issued);
    86         if(isset($doc->issued)) {
    52             }
    87             $newGraph->add($doc->doc, "http://purl.org/dc/terms/issued", $doc->issued);
    53             if(isset($doc->modified)) {
    88         }
    54                 $newGraph->add($doc->doc, "http://purl.org/dc/terms/modified", $doc->modified);
    89         if(isset($doc->modified)) {
    55             }
    90             $newGraph->add($doc->doc, "http://purl.org/dc/terms/modified", $doc->modified);
    56             array_push($data, new DocumentResult($doc->uri->getUri(), $newGraph));
    91         }
    57         }
    92         if(isset($doc->publishers)) {
    58 
    93             $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/publisher", $doc->publishers);
    59         return $data;
    94         }
       
    95         if(isset($doc->extent)) {
       
    96             $newGraph->add($doc->doc, "http://purl.org/dc/terms/extent", $doc->extent);
       
    97         }
       
    98         return $newGraph;
       
    99     }
       
   100 
       
   101     private function queryDocs($queries) {
       
   102 
       
   103         $resDocs = [];
       
   104 
       
   105         foreach($queries as $query) {
       
   106             $docs = $this->sparqlClient->query($query);
       
   107             foreach($docs as $doc) {
       
   108                 $graph = $this->getResGraph($doc);
       
   109 
       
   110                 $uri = $doc->uri->getUri();
       
   111                 if(array_key_exists($uri, $resDocs)) {
       
   112                     $resDocs[$uri] = Utils::mergeGraphs($resDocs[$uri], $graph);
       
   113                 } else {
       
   114                     $resDocs[$uri] = $graph;
       
   115                 }
       
   116 
       
   117             }
       
   118         }
       
   119 
       
   120         return array_map(function($g) { return new DocumentResult($g->getUri(), $g); }, array_values($resDocs));
    60     }
   121     }
    61 
   122 
    62     public function all() {
   123     public function all() {
    63 
   124         return $this->queryDocs(self::ALL_QUERIES);
    64         return $this->queryDocs(
       
    65         "SELECT DISTINCT ?uri ?doc ?title ?issued ?modified ?lang".
       
    66         "    WHERE {".
       
    67         "        GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
       
    68         "        ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
       
    69         "        OPTIONAL {?doc <http://purl.org/dc/elements/1.1/language> ?lang.} ".
       
    70         "        OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
       
    71         "        OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} }".
       
    72         "    } ORDER BY ?uri"
       
    73         );
       
    74     }
   125     }
    75 
   126 
    76     public function get($id, bool $short=false) {
   127     public function get($id, bool $short=false) {
    77 
   128 
    78         if(strpos($id, config('corpusparole.corpus_id_scheme')) === 0) {
   129         if(strpos($id, config('corpusparole.corpus_id_scheme')) === 0) {
   154 
   205 
   155         $total = $this->getCount();
   206         $total = $this->getCount();
   156 
   207 
   157         $offset = max(0,($page - 1) * $perPage);
   208         $offset = max(0,($page - 1) * $perPage);
   158 
   209 
   159         $query =
   210         $results = $this->queryDocs(array_map(function($q) use ($offset, $perPage) { return $q . "\nOFFSET $offset LIMIT $perPage"; }, self::ALL_QUERIES));
   160             "SELECT DISTINCT ?uri ?doc ?title ?issued ?modified ?lang".
       
   161             "    WHERE {".
       
   162             "        GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
       
   163             "        ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
       
   164             "        OPTIONAL {?doc <http://purl.org/dc/elements/1.1/language> ?lang.} ".
       
   165             "        OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
       
   166             "        OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} }".
       
   167             "    } ORDER BY ?uri OFFSET $offset LIMIT $perPage";
       
   168 
       
   169         $results = $this->queryDocs($query);
       
   170 
   211 
   171         return new LengthAwarePaginator($results, $total, $perPage, $page, [
   212         return new LengthAwarePaginator($results, $total, $perPage, $page, [
   172             'path' => Paginator::resolveCurrentPath(),
   213             'path' => Paginator::resolveCurrentPath(),
   173             'pageName' => $pageName,
   214             'pageName' => $pageName,
   174         ]);
   215         ]);
   198             }
   239             }
   199         }
   240         }
   200 
   241 
   201         return $docList;
   242         return $docList;
   202     }
   243     }
   203 
       
   204 
       
   205 
       
   206 }
   244 }