server/src/app/Http/Controllers/Api/ThemeController.php
author ymh <ymh.work@gmail.com>
Tue, 11 Oct 2016 02:49:59 +0200
changeset 326 226d5b17a119
parent 323 47f0611cc57d
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:
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
namespace CorpusParole\Http\Controllers\Api;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
// use Illuminate\Http\Request;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
// use CorpusParole\Http\Requests;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
use CorpusParole\Http\Controllers\Controller;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
use Illuminate\Http\Request;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
use EasyRdf\Resource;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
use EasyRdf\Literal;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    13
use Es;
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    14
use Log;
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    15
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
use CorpusParole\Libraries\Sparql\SparqlClient;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
use CorpusParole\Services\LexvoResolverInterface;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
use CorpusParole\Services\BnfResolverInterface;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
class ThemeController extends Controller
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
{
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    private $sparqlClient = null;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    public function __construct(SparqlClient $sparqlClient, LexvoResolverInterface $lexvoResolver, BnfResolverInterface $bnfResolver) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        $this->sparqlClient = $sparqlClient;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        $this->lexvoResolver = $lexvoResolver;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        $this->bnfResolver = $bnfResolver;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    /**
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
     * Display the specified resource.
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
     *
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
     * @return \Illuminate\Http\Response
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
     */
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    public function index(Request $request)
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    {
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    38
        $filter = $request->input('filter', 'bnf');
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    39
        $index = $request->input('index', 0);
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    40
        $limit = $request->input('limit', config('corpusparole.theme_default_limit'));
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    41
        $sort = $request->input('sort', 'count');
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    43
        if($filter === 'bnf') {
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    44
            $include = ".*\\|bnf\\|.*";
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    45
        }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    46
        elseif($filter === 'lexvo') {
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    47
            $include = ".*\\|lxv\\|.*";
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    48
        }
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    49
        elseif($filter === 'olac') {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    50
            $include = ".*\\|olac\\|.*";
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    51
        }
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    52
        elseif($filter === 'txt') {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    53
            $include = ".*\\|txt\\|.*";
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    54
        }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    55
        elseif($filter === 'all') {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    56
            $include = false;
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    57
        }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    58
        else {
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    59
            abort(401,"Value for filter parameter must be in 'bnf', 'lexvo', 'olac', 'txt', 'all'");
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    60
        }
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    62
        if($sort == "count" || $sort == "descending") {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    63
            $order_field = "_count";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    64
            $order_dir = "desc";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    65
        } elseif($sort == "-count") {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    66
            $order_field = "_count";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    67
            $order_dir = "asc";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    68
        } elseif ($sort == "label" || $sort == "alphabetical") {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    69
            $order_field = "_term";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    70
            $order_dir = "asc";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    71
        } elseif ($sort == "-label") {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    72
            $order_field = "_term";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    73
            $order_dir = "desc";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    74
        } else {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    75
            $order_field = "_count";
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    76
            $order_dir = "desc";
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    79
        $query = [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    80
            'index' => env('ELASTICSEARCH_INDEX'),
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    81
            'body' => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    82
                'size' => 0,
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    83
                'aggs' => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    84
                    "subjects" => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    85
                        "nested" => [ "path" => "subject" ],
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    86
                        "aggs" => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    87
                            "subjects" => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    88
                                "terms" => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    89
                                    "field" => "subject.label_code",
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    90
                                    "size" => $limit * ($index+1),
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    91
                                    "order" => [ $order_field => $order_dir ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    92
                                ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    93
                            ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    94
                        ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    95
                    ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    96
                ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    97
            ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    98
        ];
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   100
        if($include) {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   101
            $query['body']['aggs']['subjects']['aggs']['subjects']['terms']['include'] = $include;
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   102
        }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   103
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   104
        $esRes = Es::search($query);
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        $themes = [];
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   108
        $bucketList = $esRes['aggregations']['subjects']['subjects']['buckets'];
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   109
        if($limit > 0) {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   110
            $bucketList = array_slice($bucketList, $index*$limit, $limit);
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   111
        }
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   113
        foreach($bucketList as $bucket) {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   114
            $parts = explode("|", $bucket['key']);
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   115
            $label = $parts[0];
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   116
            if($parts[1] === 'bnf' || $parts[1] === 'lxv') {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   117
                $url = ['bnf' => config('corpusparole.bnf_ark_base_url'), 'lxv' => config('corpusparole.lexvo_base_url')][$parts[1]].$parts[2];
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   118
            }
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   119
            else {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   120
                $url = $parts[2];
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   121
            }
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   122
            $themes[$url] = [
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
                "label" => $label,
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   124
                "count" => $bucket['doc_count']
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
            ];
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
        return response()->json(['themes' => $themes ]);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
    }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
}