server/src/app/Repositories/RdfDocumentRepository.php
changeset 19 eadaf0b8f02e
parent 4 f55970e41793
child 20 a9b98b16b053
--- a/server/src/app/Repositories/RdfDocumentRepository.php	Tue Nov 17 13:11:55 2015 +0100
+++ b/server/src/app/Repositories/RdfDocumentRepository.php	Fri Nov 27 17:59:36 2015 +0100
@@ -8,12 +8,14 @@
 use CorpusParole\Libraries\CorpusParoleException;
 use CorpusParole\Libraries\Sparql\SparqlClient;
 
+use EasyRdf\Graph;
+
 use Illuminate\Pagination\LengthAwarePaginator;
 use Illuminate\Pagination\Paginator;
 
 /**
  * Implement the DocumentRepository using EasyRdf
- * TODO: cetainly split the transaction management (+add, +delete +transaction ) to an extarnal class -> for this extend the sparql client.
+ * TODO: certainly split the transaction management (+add, +delete +transaction ) to an external class -> for this extend the sparql client.
  */
 class RdfDocumentRepository implements DocumentRepository {
 
@@ -33,7 +35,18 @@
         $data = [];
 
         foreach ($docs as $doc) {
-            array_push($data, new Document($doc->uri->getUri()));
+            $newGraph = new Graph($doc->uri->getUri());
+            $newGraph->add($doc->uri, "rdf:type", $newGraph->resource("http://www.openarchives.org/ore/terms/Aggregation"));
+            $newGraph->add($doc->uri, "http://www.europeana.eu/schemas/edm/aggregatedCHO", $doc->doc);
+            $newGraph->add($doc->doc, "rdf:type", $newGraph->resource("http://www.europeana.eu/schemas/edm/ProvidedCHO"));
+            $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/title", $doc->title);
+            if(isset($doc->issued)) {
+                $newGraph->add($doc->doc, "http://purl.org/dc/terms/issued", $doc->issued);
+            }
+            if(isset($doc->modified)) {
+                $newGraph->add($doc->doc, "http://purl.org/dc/terms/modified", $doc->modified);
+            }
+            array_push($data, new Document($doc->uri->getUri(), $newGraph));
         }
 
         return $data;
@@ -41,13 +54,20 @@
 
     public function all() {
 
-        return $this->queryDocs("SELECT DISTINCT ?uri WHERE { GRAPH ?uri { ?s ?p ?o } } ORDER BY ?uri");
-
+        return $this->queryDocs(
+        "SELECT DISTINCT ?uri ?doc ?title ?issued ?modified".
+        "    WHERE {".
+        "        GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
+        "        ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
+        "        OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
+        "        OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} }".
+        "    } ORDER BY ?uri"
+        );
     }
 
     public function get($id) {
 
-        $docUri = Config::get('corpusparole.cocoon_doc_id_base_uri').$id;
+        $docUri = Config::get('corpusparole.corpus_doc_id_base_uri').$id;
 
         // We want the CBD (Concise Bounded Description, cf. http://www.w3.org/Submission/CBD/)
         // WARNING: This seems to work in sesame for our dataset.
@@ -94,7 +114,7 @@
     }
 
     public function getCount() {
-        $res = $this->sparqlClient->query("SELECT (COUNT (DISTINCT ?g) as ?count) WHERE { GRAPH ?g { ?s ?p ?o } }");
+        $res = $this->sparqlClient->query("SELECT (COUNT (DISTINCT ?g) as ?count) WHERE { GRAPH ?g { ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> } }");
         assert(!is_null($res) && $res->count()==1);
         return $res[0]->count->getValue();
     }
@@ -110,13 +130,24 @@
      */
     public function paginateAll($perPage = 15, $pageName = 'page')
     {
+        assert(is_numeric($perPage));
+
         $page = Paginator::resolveCurrentPage($pageName);
 
+        assert(is_numeric($page));
+
         $total = $this->getCount();
 
         $offset = max(0,($page - 1) * $perPage);
-
-        $query = "SELECT DISTINCT ?uri WHERE { GRAPH ?uri { ?s ?p ?o } } ORDER BY ?uri OFFSET $offset LIMIT $perPage";
+        
+        $query =
+            "SELECT DISTINCT ?uri ?doc ?title ?issued ?modified".
+            "    WHERE {".
+            "        GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
+            "        ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
+            "        OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
+            "        OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} }".
+            "    } ORDER BY ?uri OFFSET $offset LIMIT $perPage";
 
         $results = $this->queryDocs($query);