server/src/app/Repositories/RdfDocumentRepository.php
author ymh <ymh.work@gmail.com>
Thu, 02 Jun 2016 18:24:19 +0200
changeset 168 17f10b56c079
parent 125 e550b10fe3ca
child 169 8fddc113095e
permissions -rw-r--r--
improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
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;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    10
use CorpusParole\Libraries\Sparql\SparqlClient;
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    11
use CorpusParole\Services\LexvoResolverInterface;
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    12
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    13
use EasyRdf\Graph;
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    14
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    15
use Illuminate\Pagination\LengthAwarePaginator;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    16
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
    17
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
/**
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * Implement the DocumentRepository using EasyRdf
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    20
 * 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
    21
 */
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
class RdfDocumentRepository implements DocumentRepository {
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    private $sparqlClient;
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    25
    private $lexvoResolver;
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    27
    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
    28
        $this->sparqlClient = $sparqlClient;
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    29
        $this->lexvoResolver = $lexvoResolver;
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    32
    public function getSparqlClient() {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    33
        return $this->sparqlClient;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    34
    }
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    36
    private function queryDocs($query) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    37
        $docs = $this->sparqlClient->query($query);
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        $data = [];
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        foreach ($docs as $doc) {
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    42
            $newGraph = new Graph($doc->uri->getUri());
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    43
            $newGraph->add($doc->uri, "rdf:type", $newGraph->resource("http://www.openarchives.org/ore/terms/Aggregation"));
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    44
            $newGraph->add($doc->uri, "http://www.europeana.eu/schemas/edm/aggregatedCHO", $doc->doc);
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    45
            $newGraph->add($doc->doc, "rdf:type", $newGraph->resource("http://www.europeana.eu/schemas/edm/ProvidedCHO"));
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    46
            $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/title", $doc->title);
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    47
            if(isset($doc->lang)) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    48
                $newGraph->add($doc->doc, "http://purl.org/dc/elements/1.1/language", $doc->lang);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    49
            }
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    50
            if(isset($doc->issued)) {
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    51
                $newGraph->add($doc->doc, "http://purl.org/dc/terms/issued", $doc->issued);
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    52
            }
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    53
            if(isset($doc->modified)) {
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    54
                $newGraph->add($doc->doc, "http://purl.org/dc/terms/modified", $doc->modified);
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    55
            }
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
    56
            array_push($data, new DocumentResult($doc->uri->getUri(), $newGraph));
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        return $data;
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    62
    public function all() {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    63
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    64
        return $this->queryDocs(
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    65
        "SELECT DISTINCT ?uri ?doc ?title ?issued ?modified ?lang".
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    66
        "    WHERE {".
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    67
        "        GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    68
        "        ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    69
        "        OPTIONAL {?doc <http://purl.org/dc/elements/1.1/language> ?lang.} ".
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    70
        "        OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    71
        "        OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} }".
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    72
        "    } ORDER BY ?uri"
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    73
        );
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    74
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    75
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
    76
    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
    77
122
b37fde30dd4a correct problem of identifier
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    78
        if(strpos($id, config('corpusparole.corpus_id_scheme')) === 0) {
b37fde30dd4a correct problem of identifier
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    79
            $id = substr($id, strlen(config('corpusparole.corpus_id_scheme')));
b37fde30dd4a correct problem of identifier
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    80
        }
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    81
        $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
    82
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        // 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
    84
        // 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
    85
        $doc = $this->sparqlClient->query(
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    86
            "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
    87
        );
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    88
        //TODO: return null if not found
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    89
        if($doc->isEmpty()) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    90
            return null;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    91
        }
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    93
        // 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
    94
        $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
    95
        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
    96
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
    }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    99
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   100
     * save document.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   101
     * @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
   102
     * @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
   103
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   104
    public function save(Document $doc) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   105
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   106
        $transactionStarted = $this->sparqlClient->startTransaction();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   107
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   108
        try {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   109
            foreach($doc->getDeltaList() as $delta) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   110
                $this->sparqlClient->delete($delta->getDeletedGraph());
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   111
                $this->sparqlClient->add($delta->getAddedGraph());
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   112
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   113
            if($transactionStarted) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   114
                $transactionStarted = false;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   115
                return $this->sparqlClient->commit();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   116
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   117
            else  {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   118
                return false;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   119
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   120
        }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   121
        catch(CorpusParoleException $e) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   122
            if($transactionStarted) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   123
                $this->sparqlClient->rollback();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   124
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   125
            throw $e;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   126
        }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   127
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   128
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   129
    public function getCount() {
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   130
        $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
   131
        assert(!is_null($res) && $res->count()==1);
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   132
        return $res[0]->count->getValue();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   133
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   134
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   135
    //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
   136
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   137
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   138
     * Paginate all document as a paginator.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   139
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   140
     * @param  int  $perPage
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   141
     * @param  string  $pageName
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   142
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   143
     */
22
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   144
    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
   145
    {
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   146
        assert(is_numeric($perPage));
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   147
22
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   148
        if(is_null($page)) {
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   149
            $page = Paginator::resolveCurrentPage($pageName);
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   150
        }
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   151
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   152
        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
   153
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   154
        $total = $this->getCount();
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
        $offset = max(0,($page - 1) * $perPage);
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   157
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   158
        $query =
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   159
            "SELECT DISTINCT ?uri ?doc ?title ?issued ?modified ?lang".
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   160
            "    WHERE {".
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   161
            "        GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   162
            "        ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   163
            "        OPTIONAL {?doc <http://purl.org/dc/elements/1.1/language> ?lang.} ".
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   164
            "        OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   165
            "        OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} }".
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   166
            "    } ORDER BY ?uri OFFSET $offset LIMIT $perPage";
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   167
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   168
        $results = $this->queryDocs($query);
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   169
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   170
        return new LengthAwarePaginator($results, $total, $perPage, $page, [
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   171
            'path' => Paginator::resolveCurrentPath(),
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   172
            'pageName' => $pageName,
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   173
        ]);
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   174
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   175
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   176
    /**
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   177
     * 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
   178
     * 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
   179
     * @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
   180
     */
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   181
    public function resolveLexvo(Array $docList) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   182
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   183
        $languageIds = [];
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   184
        #get the list pf language needing resolving
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   185
        foreach ($docList as $doc) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   186
            if($doc->getLanguageValue() && is_null($doc->getLanguageResolved())) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   187
                $languageIds[$doc->getLanguageValue()] = true;
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   188
            }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   189
        }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   190
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   191
        # call LexvoResolver
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   192
        $langNames = $this->lexvoResolver->getNames(array_keys($languageIds));
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   193
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   194
        foreach ($docList as $doc) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   195
            if($doc->getLanguageValue() && is_null($doc->getLanguageResolved())) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   196
                $doc->setLanguageResolved($langNames[$doc->getLanguageValue()]);
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   197
            }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   198
        }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   199
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   200
        return $docList;
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   201
    }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   202
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   203
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   204
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
}