server/src/app/Http/Controllers/Api/DiscourseController.php
author ymh <ymh.work@gmail.com>
Tue, 11 Oct 2016 02:49:59 +0200
changeset 326 226d5b17a119
parent 307 07b44a378ad8
child 377 52169c718513
permissions -rw-r--r--
- First implementation of filter for languages. - Language is now an array in the document - various corrections linked to the above change - Simplify the IndexDocumet loop
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
namespace CorpusParole\Http\Controllers\Api;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
// use CorpusParole\Http\Requests;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
use CorpusParole\Http\Controllers\Controller;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
use Illuminate\Http\Request;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
use EasyRdf\Resource;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
use EasyRdf\Literal;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
use CorpusParole\Libraries\Sparql\SparqlClient;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use CorpusParole\Services\LexvoResolverInterface;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use CorpusParole\Services\BnfResolverInterface;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
class DiscourseController extends Controller
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
{
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    private $sparqlClient = null;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    public function __construct(SparqlClient $sparqlClient) {
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        $this->sparqlClient = $sparqlClient;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    }
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    /**
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
     * Display the specified resource.
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
     *
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
     * @return \Illuminate\Http\Response
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
     */
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    public function index(Request $request)
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    {
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
307
07b44a378ad8 Add the datestats api
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    33
        $query =  preg_replace('/\s+/', ' ', "SELECT (?o AS ?res) (COUNT(?s) AS ?count) WHERE {
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO>.
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
            ?s <http://purl.org/dc/elements/1.1/type> ?o.
307
07b44a378ad8 Add the datestats api
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    36
            FILTER(uri(?o) in (<".implode('>,<', array_keys(config('corpusparole.corpus_discourse_type'))).">))
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        }
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        GROUP BY ?o
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        ORDER BY DESC(?count)");
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        $docs = $this->sparqlClient->query($query);
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        $discourses = [];
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        foreach ($docs as $row) {
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            $key = $row->res->getUri();
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            $count = $row->count->getValue();
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            $label = config('corpusparole.corpus_discourse_type')[$key];
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            $discourses[$key] = [
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
                "label" => $label,
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
                "count" => $count
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            ];
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        }
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        return response()->json(['discourses' => $discourses ]);
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
    }
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
}