server/src/app/Repositories/RdfDocumentRepository.php
author ymh <ymh.work@gmail.com>
Thu, 20 Oct 2016 15:09:31 +0200
changeset 376 02f113d43f18
parent 372 796ebdbf6a25
child 378 5b47eab083f3
permissions -rw-r--r--
add date filter to documents api end point
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;
329
0a2c2ad49d75 Improvce language visualization. Generalize language node selection, change language query parameters, add resolution of node name (corpus-) to lexvo controler
ymh <ymh.work@gmail.com>
parents: 326
diff changeset
    12
use CorpusParole\Libraries\Filters\CorpusFilterManager;
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
    13
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
    14
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    15
use CorpusParole\Services\LexvoResolverInterface;
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    16
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    17
use EasyRdf\Graph;
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    18
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    19
use Illuminate\Pagination\LengthAwarePaginator;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    20
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
    21
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    22
use Es;
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    23
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
 * Implement the DocumentRepository using EasyRdf
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    26
 * 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
    27
 */
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
class RdfDocumentRepository implements DocumentRepository {
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    30
    const BASE_DOC_QUERY
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    31
        = "SELECT".
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
    32
        "    ?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
    33
        "    ?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
    34
        "    ?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
    35
        "    ?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
    36
        "    ?modified".
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    37
        "    (group_concat(distinct ?language;separator=\", \") as ?lang) ".
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
    38
        "    (group_concat(distinct ?publisher;separator=\", \") as ?publishers) ".
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    39
        "  WHERE {".
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    40
        "    GRAPH ?uri { ?doc a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.".
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    41
        "      ?doc <http://purl.org/dc/elements/1.1/title> ?title.".
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    42
        "      OPTIONAL {?doc <http://purl.org/dc/elements/1.1/language> ?language.} ".
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    43
        "      OPTIONAL {?doc <http://purl.org/dc/terms/issued> ?issued.} ".
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    44
        "      OPTIONAL {?doc <http://purl.org/dc/terms/modified> ?modified.} ".
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    45
        "      OPTIONAL {?doc <http://purl.org/dc/elements/1.1/publisher> ?publisher.}".
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    46
        "    }. ".
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    47
        "    %s".
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    48
        "  } ".
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
    49
        "  GROUP BY ?uri ?doc ?title ?issued ?modified ";
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
    50
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    51
    const ADDITIONAL_DOC_QUERIES = [
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
    52
        "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
    53
        "    ?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
    54
        "    ?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
    55
        "    (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
    56
        "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
    57
        "    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
    58
        "        ?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
    59
        "        ?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
    60
        "        ?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
    61
        "        OPTIONAL {?s <http://purl.org/dc/terms/extent> ?ext.}".
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    62
        "    }. ".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    63
        "    %s".
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
    64
        "} ".
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    65
        "GROUP BY ?uri ?doc",
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    66
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    67
        "SELECT".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    68
        "    ?uri".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    69
        "    ?doc".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    70
        "    (sample(distinct str(?s)) as ?transcript_url) ".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    71
        "WHERE {".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    72
        "    GRAPH ?uri {".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    73
        "        ?s a <http://www.europeana.eu/schemas/edm/WebResource>. ".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    74
        "        ?uri <http://www.europeana.eu/schemas/edm/aggregatedCHO> ?doc. ".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    75
        "        OPTIONAL {?s <http://purl.org/dc/elements/1.1/format> ?f.} ".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    76
        "    }. ".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    77
        "    FILTER(str(?f) IN ( \"application/xml\", \"application/pdf\" )). ".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    78
        "    %s".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    79
        "} ".
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
    80
        "GROUP BY ?uri ?doc"
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
    81
    ];
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
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    private $sparqlClient;
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    84
    private $lexvoResolver;
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    86
    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
    87
        $this->sparqlClient = $sparqlClient;
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    88
        $this->lexvoResolver = $lexvoResolver;
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    91
    public function getSparqlClient() {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    92
        return $this->sparqlClient;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
    93
    }
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
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
    95
    private function getResGraph($doc) {
276
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    96
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    97
        if(empty((array)$doc)) {
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    98
            return null;
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
    99
        }
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
   100
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
   101
        $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
   102
        $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
   103
        $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
   104
        $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
   105
        if(isset($doc->title)) {
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   106
            $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
   107
        }
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
        if(isset($doc->lang)) {
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   109
            foreach(explode(", ", $doc->lang) as $langStr) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   110
                $langStr = trim($langStr);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   111
                if(filter_var($langStr, FILTER_VALIDATE_URL)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   112
                    $newGraph->addResource($doc->doc, "http://purl.org/dc/elements/1.1/language", $langStr);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   113
                } else {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   114
                    $newGraph->addLiteral($doc->doc, "http://purl.org/dc/elements/1.1/language", $langStr);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   115
                }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   116
            }
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
   117
        }
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
   118
        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
   119
            $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
   120
        }
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
   121
        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
   122
            $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
   123
        }
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
        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
   125
            $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
   126
        }
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
   127
        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
   128
            $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
   129
        }
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   130
        if(isset($doc->transcript_url)) {
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   131
            $newGraph->add($doc->doc, config('corpusparole.corpus_ontology_url').'transcript', $doc->transcript_url);
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   132
        }
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
   133
        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
   134
    }
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
   135
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   136
    private function queryES($filters=null, $offset=null, $limit=null, $sort=null) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   137
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   138
        if(empty($sort)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   139
            $sort = ["_doc"];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   140
        } elseif (is_string($sort)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   141
            $sort = [$sort];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   142
        }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   143
        if(is_null($filters)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   144
            //$filters = ['language' => ["http://lexvo.org/id/iso639-3/oci", "http://lexvo.org/id/iso639-3/bre"]];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   145
            $filters = [];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   146
        }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   147
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   148
        $qFilterParts = [];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   149
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   150
        if(array_key_exists('language', $filters) && !empty($filters['language'])) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   151
            $languages = $filters['language'];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   152
            if(is_string($languages)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   153
                $languages = [ $languages, ];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   154
            }
376
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   155
            $languages = array_slice($languages, config('corpusparole.filter_max_languages_nb', 200));
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   156
            $fp = CorpusFilterManager::getLanguagesFilterPart($languages);
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   157
            if(!empty($fp)) {
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   158
                $qFilterParts[] = $fp;
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   159
            }
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   160
        }
369
796725d33b67 Add location filter to documents api end-point
ymh <ymh.work@gmail.com>
parents: 329
diff changeset
   161
        if(array_key_exists('location', $filters) && !empty($filters['location'])) {
796725d33b67 Add location filter to documents api end-point
ymh <ymh.work@gmail.com>
parents: 329
diff changeset
   162
            $location = $filters['location'];
796725d33b67 Add location filter to documents api end-point
ymh <ymh.work@gmail.com>
parents: 329
diff changeset
   163
            if(is_array($location)) {
796725d33b67 Add location filter to documents api end-point
ymh <ymh.work@gmail.com>
parents: 329
diff changeset
   164
                $location = $location[0]; // we know it is not empty
796725d33b67 Add location filter to documents api end-point
ymh <ymh.work@gmail.com>
parents: 329
diff changeset
   165
            }
376
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   166
            $fp = CorpusFilterManager::getLocationFilterPart($location);
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   167
            if(!empty($fp)) {
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   168
                $qFilterParts[] = $fp;
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   169
            }
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   170
369
796725d33b67 Add location filter to documents api end-point
ymh <ymh.work@gmail.com>
parents: 329
diff changeset
   171
        }
370
d7c5b43d309a add theme/subject filter to back documents api end point
ymh <ymh.work@gmail.com>
parents: 369
diff changeset
   172
        if(array_key_exists('themes', $filters) && !empty($filters['themes'])) {
d7c5b43d309a add theme/subject filter to back documents api end point
ymh <ymh.work@gmail.com>
parents: 369
diff changeset
   173
            $themes = $filters['themes'];
d7c5b43d309a add theme/subject filter to back documents api end point
ymh <ymh.work@gmail.com>
parents: 369
diff changeset
   174
            if(is_string($themes)) {
d7c5b43d309a add theme/subject filter to back documents api end point
ymh <ymh.work@gmail.com>
parents: 369
diff changeset
   175
                $themes = [$themes,]; // we know it is not empty
d7c5b43d309a add theme/subject filter to back documents api end point
ymh <ymh.work@gmail.com>
parents: 369
diff changeset
   176
            }
376
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   177
            $fp = CorpusFilterManager::getThemeFilterPart($themes);
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   178
            if(!empty($fp)) {
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   179
                $qFilterParts[] = $fp;
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   180
            }
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   181
370
d7c5b43d309a add theme/subject filter to back documents api end point
ymh <ymh.work@gmail.com>
parents: 369
diff changeset
   182
        }
372
796ebdbf6a25 Add filter for discourse types on documents list api end point
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
   183
        if(array_key_exists('discourses', $filters) && !empty($filters['discourses'])) {
796ebdbf6a25 Add filter for discourse types on documents list api end point
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
   184
            $discourses = $filters['discourses'];
796ebdbf6a25 Add filter for discourse types on documents list api end point
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
   185
            if(is_string($discourses)) {
796ebdbf6a25 Add filter for discourse types on documents list api end point
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
   186
                $discourses = [$discourses,]; // we know it is not empty
796ebdbf6a25 Add filter for discourse types on documents list api end point
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
   187
            }
376
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   188
            $fp = CorpusFilterManager::getDiscourseFilterPart($discourses);
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   189
            if(!empty($fp)) {
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   190
                $qFilterParts[] = $fp;
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   191
            }
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   192
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   193
        }
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   194
        if(array_key_exists('dates', $filters) && !empty($filters['dates'])) {
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   195
            $dates = $filters['dates'];
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   196
            if(is_string($dates)) {
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   197
                $dates = [$dates,]; // we know it is not empty
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   198
            }
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   199
            $fp = CorpusFilterManager::getDateFilterPart($dates);
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   200
            if(!empty($fp)) {
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   201
                $qFilterParts[] = $fp;
02f113d43f18 add date filter to documents api end point
ymh <ymh.work@gmail.com>
parents: 372
diff changeset
   202
            }
372
796ebdbf6a25 Add filter for discourse types on documents list api end point
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
   203
        }
796ebdbf6a25 Add filter for discourse types on documents list api end point
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
   204
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   205
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   206
        $query = [
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   207
            'index' => config('corpusparole.elasticsearch_index'),
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   208
            'body' => [
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   209
                "size" => empty($limit)?0:$limit,
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   210
                "from" => $offset,
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   211
                "sort" => $sort
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   212
            ]
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   213
        ];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   214
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   215
        if(count($qFilterParts)>0) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   216
            $query['body']['query'] = ['constant_score' => [
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   217
                'filter' => [
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   218
                    'bool' => [
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   219
                        'must' => $qFilterParts
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   220
                    ]
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   221
                ]
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   222
            ] ];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   223
        }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   224
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   225
        $esRes = Es::search($query);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   226
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   227
        return ['total' => $esRes['hits']['total'], 'documents' => array_map(function($r) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   228
            return $r['_id'];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   229
        }, $esRes['hits']['hits'])];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   230
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   231
    }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   232
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   233
    /**
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   234
     * Query docs.
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   235
     * if $filter is empty or null and $sort is '_graph', the documents list is fetched from the triple store, otherwise, They are fetched from ElasticSearch
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   236
     */
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   237
    private function queryDocs($filters=null, $offset=null, $limit=null, $sort=null) {
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
   238
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
   239
        $resDocs = [];
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   240
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   241
        $limitsClauses = [];
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   242
        $sortClauseStr = "";
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   243
        $limitsClausesStr = "";
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   244
        $filterUris = "";
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
   245
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   246
        if(empty($filters) && $sort === "_graph") {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   247
            if(!is_null($offset)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   248
                array_push($limitsClauses, "OFFSET $offset");
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   249
            }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   250
            if(!is_null($limit)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   251
                array_push($limitsClauses, "LIMIT $limit");
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   252
            }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   253
            if(!empty($limitsClauses)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   254
                $limitsClausesStr = "\n" . join(" ", $limitsClauses);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   255
            }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   256
            $sortClauseStr = "\n ORDER BY ?uri";
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   257
            $total = $this->getCount();
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   258
        } else {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   259
            $esRes = $this->queryES($filters, $offset, $limit);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   260
            // WARNING: we count on the fact that php keep keys order
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   261
            $total = intval($esRes['total']);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   262
            foreach($esRes['documents'] as $esDocId) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   263
                $uri = config('corpusparole.corpus_doc_id_base_uri_prefix').$esDocId;
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   264
                $resDocs[$uri] = null;
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   265
            }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   266
            if(count($resDocs) > 0) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   267
                $filterUris = "FILTER(?uri in (<".join(">, <" , array_keys($resDocs)).">)) ";
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   268
            } else {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   269
                return ['meta' => [ 'total'=> $total ], 'documents' => []];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   270
            }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   271
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   272
        }
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   273
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   274
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   275
        $sparqlQuery = sprintf(self::BASE_DOC_QUERY.$sortClauseStr.$limitsClausesStr, $filterUris);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   276
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   277
        $docs = $this->sparqlClient->query($sparqlQuery);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   278
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   279
        foreach($docs as $doc) {
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   280
            $graph = $this->getResGraph($doc);
276
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
   281
            if(is_null($graph)) {
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   282
                Log::debug("NULL GRAPH - odd");
276
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
   283
                continue;
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
   284
            }
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   285
            $uri = $doc->uri->getUri();
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   286
            $resDocs[$uri] = $graph;
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   287
        }
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   288
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   289
        if(count($resDocs) == 0) {
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   290
            return ['meta' => [ 'total'=> $total ], 'documents' => []];
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   291
        }
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   292
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   293
        if(empty($filterUris)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   294
            $filterUris = "FILTER(?uri in (<".join(">, <" , array_keys($resDocs)).">)) ";
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   295
        }
275
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   296
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   297
        foreach(self::ADDITIONAL_DOC_QUERIES as $query) {
a4d8618c2f1b add transcript_url property on document list results
ymh <ymh.work@gmail.com>
parents: 261
diff changeset
   298
            $docs = $this->sparqlClient->query(sprintf($query, $filterUris));
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
   299
            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
   300
                $graph = $this->getResGraph($doc);
276
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
   301
                if(is_null($graph)) {
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
   302
                    continue;
ef32bf573d9c correct error in test with empty document returned by query
ymh <ymh.work@gmail.com>
parents: 275
diff changeset
   303
                }
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
   304
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
   305
                $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
   306
                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
   307
                    $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
   308
                } 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
   309
                    $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
   310
                }
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   311
            }
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
        }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   314
        $documentsResults = array_map(function($g) { return new DocumentResult($g->getUri(), $g); }, array_values($resDocs));
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   315
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   316
        return ['meta' => [ 'total'=> $total ], 'documents' => $documentsResults];
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
    }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   319
    public function all() {
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   320
        return $this->queryDocs(null, null, null, "_graph")['documents'];
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   321
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   322
306
3fccf43160a7 Some more changes linked to the change of api organization + some jshint error cleaning
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
   323
    public function get($id, $short=false) {
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
122
b37fde30dd4a correct problem of identifier
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   325
        if(strpos($id, config('corpusparole.corpus_id_scheme')) === 0) {
b37fde30dd4a correct problem of identifier
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   326
            $id = substr($id, strlen(config('corpusparole.corpus_id_scheme')));
b37fde30dd4a correct problem of identifier
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
   327
        }
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   328
        $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
   329
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
        // 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
   331
        // 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
   332
        $doc = $this->sparqlClient->query(
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   333
            "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
   334
        );
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   335
        //TODO: return null if not found
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   336
        if($doc->isEmpty()) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   337
            return null;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   338
        }
169
8fddc113095e Correct proble on geo info merging + add geo resource
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   339
        //print($doc->dump());
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   341
        // 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
   342
        $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
   343
        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
   344
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
    }
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   347
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   348
     * save document.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   349
     * @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
   350
     * @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
   351
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   352
    public function save(Document $doc) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   353
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   354
        $transactionStarted = $this->sparqlClient->startTransaction();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   355
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   356
        try {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   357
            foreach($doc->getDeltaList() as $delta) {
277
bd4bc1db4f40 add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents: 276
diff changeset
   358
                $this->sparqlClient->deleteWhere($delta->getDeleteWhere(), $delta->getUri());
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   359
                $this->sparqlClient->delete($delta->getDeletedGraph());
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   360
                $this->sparqlClient->add($delta->getAddedGraph());
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   361
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   362
            if($transactionStarted) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   363
                $transactionStarted = false;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   364
                return $this->sparqlClient->commit();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   365
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   366
            else  {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   367
                return false;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   368
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   369
        }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   370
        catch(CorpusParoleException $e) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   371
            if($transactionStarted) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   372
                $this->sparqlClient->rollback();
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   373
            }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   374
            throw $e;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   375
        }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   376
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   377
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   378
    public function getCount($filters=null) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   379
        $count = 0;
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   380
        if(empty($filters)) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   381
            $res = $this->sparqlClient->query("SELECT (COUNT (DISTINCT ?g) as ?count) WHERE { GRAPH ?g { ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> } }");
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   382
            assert(!is_null($res) && count($res)==1);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   383
            $count = intval($res[0]->count->getValue());
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   384
        } else {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   385
            $esRes = $this->queryES($filters, 0, 0);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   386
            $count = intval($esRes['hits']['total']);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   387
        }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   388
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   389
        return $count;
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   390
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   391
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   392
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   393
    //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
   394
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   395
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   396
     * Paginate all document as a paginator.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   397
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   398
     * @param  int  $perPage
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   399
     * @param  string  $pageName
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   400
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   401
     */
22
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   402
    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
   403
    {
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   404
        return $this->paginate(null, $perPage, $pageName, null);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   405
    }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   406
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   407
    /**
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   408
     * Paginate filtered document as a paginator.
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   409
     *
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   410
     * @param  array $filters
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   411
     * @param  int  $perPage
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   412
     * @param  string  $pageName
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   413
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   414
     */
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   415
    public function paginate($filters = null, $perPage = 15, $pageName = 'page', $page = null, $sort=null) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   416
19
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   417
        assert(is_numeric($perPage));
eadaf0b8f02e Bo conception step. back to ember page
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
   418
22
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   419
        if(is_null($page)) {
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   420
            $page = Paginator::resolveCurrentPage($pageName);
a50cbd7d702f small change in pagination and add missing .env template
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   421
        }
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   422
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   423
        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
   424
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   425
        $offset = max(0,($page - 1) * $perPage);
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   426
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   427
        $results = $this->queryDocs($filters, $offset, $perPage, $sort);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   428
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   429
        return new LengthAwarePaginator($results['documents'], $results['meta']['total'], $perPage, $page, [
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   430
            'path' => Paginator::resolveCurrentPath(),
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   431
            'pageName' => $pageName,
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   432
        ]);
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   433
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   434
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   435
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   436
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   437
    /**
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   438
     * 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
   439
     * 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
   440
     * @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
   441
     */
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   442
    public function resolveLexvo(Array $docList) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   443
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   444
        $languageIds = [];
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   445
        #get the list pf language needing resolving
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   446
        foreach ($docList as $doc) {
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   447
            if(!empty($doc->getLanguagesValue()) && is_null($doc->getLanguagesResolved())) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   448
                foreach($doc->getLanguagesValue() as $lang) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   449
                    $languageIds[$lang]=true;
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   450
                }
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   451
            }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   452
        }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   453
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   454
        # call LexvoResolver
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   455
        $langNames = $this->lexvoResolver->getNames(array_keys($languageIds));
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   456
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   457
        foreach ($docList as $doc) {
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   458
            if(!empty($doc->getLanguagesValue()) && is_null($doc->getLanguagesResolved())) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   459
                $langResolved = [];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   460
                foreach($doc->getLanguagesValue() as $lang) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   461
                    $langResolved[] = $langNames[$lang];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   462
                }
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 306
diff changeset
   463
                $doc->setLanguageResolved($langResolved);
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   464
            }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   465
        }
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   466
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   467
        return $docList;
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
   468
    }
2
00e2916104fe Migrate to php 5.6 + Laravel 5.1 + add phpunit test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
}