server/src/app/Repositories/RdfDocumentRepository.php
author ymh <ymh.work@gmail.com>
Sat, 06 Aug 2016 21:29:33 +0700
changeset 261 02e2396bcbbc
parent 169 8fddc113095e
child 275 a4d8618c2f1b
permissions -rw-r--r--
Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
namespace CorpusParole\Repositories;
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
use Config;
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
use Log;
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 125
diff changeset
     7
use CorpusParole\Models\DocumentResult;
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
use CorpusParole\Models\Document;
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
     9
use CorpusParole\Libraries\CorpusParoleException;
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    10
use CorpusParole\Libraries\Utils;
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    11
use CorpusParole\Libraries\Sparql\SparqlClient;
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    12
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    13
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    14
use CorpusParole\Services\LexvoResolverInterface;
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    15
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    16
use EasyRdf\Graph;
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    17
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    18
use Illuminate\Pagination\LengthAwarePaginator;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    19
use Illuminate\Pagination\Paginator;
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
/**
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * Implement the DocumentRepository using EasyRdf
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    23
 * TODO: certainly split the transaction management (+add, +delete +transaction ) to an external class -> for this extend the sparql client.
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 */
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
class RdfDocumentRepository implements DocumentRepository {
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    27
    const ALL_QUERIES = [
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    28
        "SELECT".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    29
        "    ?uri".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    30
        "    ?doc".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    31
        "    ?title".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    32
        "    ?issued".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    33
        "    ?modified".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    34
        "    ?lang".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    35
        "    (group_concat(distinct ?publisher;separator=\", \") as ?publishers) ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    36
        "WHERE {".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    37
        "GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    38
        "    ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    39
        "    OPTIONAL {?doc <http://purl.org/dc/elements/1.1/language> ?lang.} ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    40
        "    OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    41
        "    OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    42
        "    OPTIONAL {?doc <http://purl.org/dc/elements/1.1/publisher> ?publisher.} }".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    43
        "} ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    44
        "GROUP BY ?uri ?doc ?title ?issued ?modified ?lang ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    45
        "ORDER BY ?uri",
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    46
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    47
        "SELECT".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    48
        "    ?uri".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    49
        "    ?doc".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    50
        "    (sample(distinct ?ext) as ?extent) ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    51
        "WHERE {".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    52
        "    GRAPH ?uri {".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    53
        "        ?s a <http://www.europeana.eu/schemas/edm/WebResource>. ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    54
        "        ?uri <http://www.europeana.eu/schemas/edm/isShownBy> ?s. ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    55
        "        ?uri <http://www.europeana.eu/schemas/edm/aggregatedCHO> ?doc. ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    56
        "        OPTIONAL {?s <http://purl.org/dc/terms/extent> ?ext.}".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    57
        "    }".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    58
        "} ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    59
        "GROUP BY ?uri ?doc ".
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    60
        "ORDER BY ?uri"
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    61
    ];
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    62
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    private $sparqlClient;
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    64
    private $lexvoResolver;
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    66
    public function __construct(SparqlClient $sparqlClient, LexvoResolverInterface $lexvoResolver) {
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    67
        $this->sparqlClient = $sparqlClient;
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    68
        $this->lexvoResolver = $lexvoResolver;
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    71
    public function getSparqlClient() {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    72
        return $this->sparqlClient;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    73
    }
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    75
    private function getResGraph($doc) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    76
        $newGraph = new Graph($doc->uri->getUri());
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    77
        $newGraph->add($doc->uri, "rdf:type", $newGraph->resource("http://www.openarchives.org/ore/terms/Aggregation"));
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    78
        $newGraph->add($doc->uri, "http://www.europeana.eu/schemas/edm/aggregatedCHO", $doc->doc);
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    79
        $newGraph->add($doc->doc, "rdf:type", $newGraph->resource("http://www.europeana.eu/schemas/edm/ProvidedCHO"));
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    80
        if(isset($doc->title)) {
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    81
            $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/title", $doc->title);
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    82
        }
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    83
        if(isset($doc->lang)) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    84
            $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/language", $doc->lang);
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    85
        }
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    86
        if(isset($doc->issued)) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    87
            $newGraph->add($doc->doc, "http://purl.org/dc/terms/issued", $doc->issued);
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    88
        }
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    89
        if(isset($doc->modified)) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    90
            $newGraph->add($doc->doc, "http://purl.org/dc/terms/modified", $doc->modified);
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    91
        }
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    92
        if(isset($doc->publishers)) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    93
            $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/publisher", $doc->publishers);
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    94
        }
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    95
        if(isset($doc->extent)) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    96
            $newGraph->add($doc->doc, "http://purl.org/dc/terms/extent", $doc->extent);
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    97
        }
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    98
        return $newGraph;
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
    99
    }
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   100
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   101
    private function queryDocs($queries) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   102
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   103
        $resDocs = [];
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   104
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   105
        foreach($queries as $query) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   106
            $docs = $this->sparqlClient->query($query);
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   107
            foreach($docs as $doc) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   108
                $graph = $this->getResGraph($doc);
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   109
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   110
                $uri = $doc->uri->getUri();
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   111
                if(array_key_exists($uri, $resDocs)) {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   112
                    $resDocs[$uri] = Utils::mergeGraphs($resDocs[$uri], $graph);
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   113
                } else {
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   114
                    $resDocs[$uri] = $graph;
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   115
                }
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   116
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   117
            }
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
        }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   120
        return array_map(function($g) { return new DocumentResult($g->getUri(), $g); }, array_values($resDocs));
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
    }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   123
    public function all() {
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   124
        return $this->queryDocs(self::ALL_QUERIES);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   125
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   126
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 125
diff changeset
   127
    public function get($id, bool $short=false) {
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
122
b37fde30dd4a correct problem of identifier
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   129
        if(strpos($id, config('corpusparole.corpus_id_scheme')) === 0) {
b37fde30dd4a correct problem of identifier
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   130
            $id = substr($id, strlen(config('corpusparole.corpus_id_scheme')));
b37fde30dd4a correct problem of identifier
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   131
        }
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   132
        $docUri = Config::get('corpusparole.corpus_doc_id_base_uri').$id;
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
        // We want the CBD (Concise Bounded Description, cf. http://www.w3.org/Submission/CBD/)
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
        // WARNING: This seems to work in sesame for our dataset.
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
        $doc = $this->sparqlClient->query(
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   137
            "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <$docUri> { ?s ?p ?o } }"
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        );
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   139
        //TODO: return null if not found
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   140
        if($doc->isEmpty()) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   141
            return null;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   142
        }
169
8fddc113095e Correct proble on geo info merging + add geo resource
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   143
        //print($doc->dump());
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   145
        // clone the graph to force the URI
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 125
diff changeset
   146
        $DocumentKlass = $short?DocumentResult::class:Document::class;
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 125
diff changeset
   147
        return new $DocumentKlass($docUri, new Graph($docUri, $doc->toRdfPhp()));
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
    }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   151
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   152
     * save document.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   153
     * @return boolean true if a transaction was started, false otherwise
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   154
     * @throws CorpusParoleException if one of the operation could not be performed
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   155
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   156
    public function save(Document $doc) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   157
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   158
        $transactionStarted = $this->sparqlClient->startTransaction();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   159
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   160
        try {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   161
            foreach($doc->getDeltaList() as $delta) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   162
                $this->sparqlClient->delete($delta->getDeletedGraph());
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   163
                $this->sparqlClient->add($delta->getAddedGraph());
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   164
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   165
            if($transactionStarted) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   166
                $transactionStarted = false;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   167
                return $this->sparqlClient->commit();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   168
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   169
            else  {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   170
                return false;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   171
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   172
        }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   173
        catch(CorpusParoleException $e) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   174
            if($transactionStarted) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   175
                $this->sparqlClient->rollback();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   176
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   177
            throw $e;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   178
        }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   179
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   180
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   181
    public function getCount() {
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   182
        $res = $this->sparqlClient->query("SELECT (COUNT (DISTINCT ?g) as ?count) WHERE { GRAPH ?g { ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> } }");
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   183
        assert(!is_null($res) && $res->count()==1);
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   184
        return $res[0]->count->getValue();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   185
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   186
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   187
    //SELECT ?g WHERE { GRAPH ?g { ?s ?p ?o } }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   188
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   189
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   190
     * Paginate all document as a paginator.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   191
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   192
     * @param  int  $perPage
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   193
     * @param  string  $pageName
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   194
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   195
     */
22
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   196
    public function paginateAll($perPage = 15, $pageName = 'page', $page = null)
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   197
    {
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   198
        assert(is_numeric($perPage));
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   199
22
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   200
        if(is_null($page)) {
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   201
            $page = Paginator::resolveCurrentPage($pageName);
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   202
        }
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   203
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   204
        assert(is_null($page) || is_numeric($page));
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   205
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   206
        $total = $this->getCount();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   207
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   208
        $offset = max(0,($page - 1) * $perPage);
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   209
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 169
diff changeset
   210
        $results = $this->queryDocs(array_map(function($q) use ($offset, $perPage) { return $q . "\nOFFSET $offset LIMIT $perPage"; }, self::ALL_QUERIES));
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   211
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   212
        return new LengthAwarePaginator($results, $total, $perPage, $page, [
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   213
            'path' => Paginator::resolveCurrentPath(),
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   214
            'pageName' => $pageName,
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   215
        ]);
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   216
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   217
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   218
    /**
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   219
     * Resolve lexvo id for all documents in the list
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   220
     * this allow to optimise the call of lexvo repository
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   221
     * @param $docList Array: a list (Array) of document to resolve
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   222
     */
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   223
    public function resolveLexvo(Array $docList) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   224
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   225
        $languageIds = [];
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   226
        #get the list pf language needing resolving
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   227
        foreach ($docList as $doc) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   228
            if($doc->getLanguageValue() && is_null($doc->getLanguageResolved())) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   229
                $languageIds[$doc->getLanguageValue()] = true;
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   230
            }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   231
        }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   232
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   233
        # call LexvoResolver
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   234
        $langNames = $this->lexvoResolver->getNames(array_keys($languageIds));
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   235
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   236
        foreach ($docList as $doc) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   237
            if($doc->getLanguageValue() && is_null($doc->getLanguageResolved())) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   238
                $doc->setLanguageResolved($langNames[$doc->getLanguageValue()]);
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   239
            }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   240
        }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   241
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   242
        return $docList;
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   243
    }
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
}