server/src/app/Console/Commands/IndexDocuments.php
author Chloe Laisne <chloe.laisne@gmail.com>
Mon, 17 Oct 2016 22:40:26 +0530
changeset 350 c3a6266ebe0a
parent 326 226d5b17a119
child 369 796725d33b67
permissions -rw-r--r--
Fix loading width height issues
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
     1
<?php
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
     2
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
     3
namespace CorpusParole\Console\Commands;
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
     4
325
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
     5
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
     6
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
     7
use Illuminate\Console\Command;
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
     8
use EasyRdf\Resource;
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
     9
use EasyRdf\Literal;
325
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
    10
use EasyRdf\Graph;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
    11
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
    12
use Carbon\Carbon;
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
    13
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    14
use GuzzleHttp\Client;
325
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
    15
use GuzzleHttp\Exception\TransferException;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
    16
use GuzzleHttp\Psr7;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
    17
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
    18
use CorpusParole\Libraries\Utils;
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    19
use CorpusParole\Repositories\DocumentRepository;
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    20
use CorpusParole\Libraries\CocoonUtils;
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    21
use CorpusParole\Models\GeonamesHierarchy;
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
    22
use CorpusParole\Services\BnfResolverInterface;
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    23
use CorpusParole\Services\LexvoResolverInterface;
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    24
use Es;
325
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
    25
use Log;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
    26
use Cache;
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    27
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    28
class IndexDocuments extends Command
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    29
{
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    30
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    31
    /**
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    32
     * The name and signature of the console command.
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    33
     *
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    34
     * @var string
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    35
     */
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    36
    protected $signature = 'corpus-parole:indexDocuments
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    37
                          {--limit=0 : index only the first n documents, 0 (default) means index everything }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    38
                          {--no-bulk : index documents one by one instead of using ElasticSearch bulk indexing }
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    39
                          {--step-size=100 : number of documents to retrieve from repository at a time before indexing}
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    40
                          {--reset-geo-cache : reset geo cache befr indexing}';
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    41
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    42
    /**
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    43
     * The console command description.
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    44
     *
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    45
     * @var string
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    46
     */
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    47
    protected $description = 'Index documents into ElasticSearch.';
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    48
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    49
    /**
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    50
     * Create a new command instance.
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    51
     *
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    52
     * @return void
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    53
     */
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    54
    public function __construct(
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    55
        DocumentRepository $documentRepository,
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    56
        Client $httpClient,
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    57
        BnfResolverInterface $bnfResolver,
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    58
        LexvoResolverInterface $lexvoResolver)
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    59
    {
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    60
        $this->documentRepository = $documentRepository;
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
    61
        $this->bnfResolver = $bnfResolver;
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
    62
        $this->lexvoResolver = $lexvoResolver;
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    63
        $this->httpClient = $httpClient;
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    64
        parent::__construct();
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    65
    }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    66
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    67
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    68
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    69
    /**
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    70
     * Reset Elasticsearch index
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    71
     *
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    72
     * @return int (1 if sucess, 0 if error)
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    73
     */
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
    74
    private function resetIndex()
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    75
    {
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    76
        $indexParams = [
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    77
            'index' => env('ELASTICSEARCH_INDEX')
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    78
        ];
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    79
        if(Es::indices()->exists($indexParams)){
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    80
            $response = Es::indices()->delete($indexParams);
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    81
            if($response['acknowledged']!=1){
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    82
                return 0;
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    83
            }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    84
        }
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    85
        // Note: removed the "'store' => True" parameters on fields and use _source on record instead
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    86
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    87
        $indexParams['body'] = [
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    88
            'settings' => [
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    89
                'number_of_shards' => config('elasticsearch.shards'),
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
    90
                'number_of_replicas' => config('elasticsearch.replicas'),
320
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    91
                'index.mapping.ignore_malformed' => True,
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    92
                'index.requests.cache.enable' => True
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    93
            ],
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    94
            'mappings' => [
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    95
                'document' => [
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    96
                    'properties' => [
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    97
                        'title' => [
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    98
                            'type' => 'string',
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
    99
                            'fields' => [
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   100
                                'raw' => [
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   101
                                    'type' => 'string',
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   102
                                    'index' => 'not_analyzed'
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   103
                                ]
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   104
                            ]
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   105
                        ],
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   106
                        'date' => [ 'type' => 'date', 'index' => 'not_analyzed'],
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   107
                        'geonames_hyerarchy' => [ 'type' => 'string', 'index' => 'not_analyzed'],
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   108
                        'location' => [ 'type' => 'geo_point'],
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   109
                        'creation_date' => ['type' => 'date', 'index' => 'not_analyzed'],
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   110
                        'language' => ['type' => 'string', 'index' => 'not_analyzed'],
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   111
                        'discourse_types' => ['type' => 'string', 'index' => 'not_analyzed'],
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   112
                        'subject' => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   113
                            'type' => 'nested',
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   114
                            'properties' => [
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   115
                                'label' => [ 'type' => 'string', 'index' => 'not_analyzed'],
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   116
                                'code' => [ 'type' => 'string', 'index' => 'not_analyzed'],
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   117
                                'label_code' => [ 'type' => 'string', 'index' => 'not_analyzed']
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   118
                            ]
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   119
                        ]
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   120
                    ]
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   121
                ]
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   122
            ]
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   123
        ];
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   124
        $response = Es::indices()->create($indexParams);
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   125
        if($response['acknowledged']!=1){
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   126
            return 0;
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   127
        }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   128
        return 1;
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   129
    }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   130
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   131
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   132
    private function getGeonamesHierarchyArray($geonamesid) {
325
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   133
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   134
        $hcache = GeonamesHierarchy::where('geonamesid', $geonamesid)->first();
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   135
        if(is_null($hcache)) {
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   136
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   137
            // TODO: add delay to respect geonames 2k request/hour
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   138
            // TODO: manage errors
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   139
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   140
            $apiBody = $this->httpClient->get(
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   141
                config('corpusparole.geonames_hierarchy_webservice_url'),
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   142
                [ 'query' =>
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   143
                    [ 'geonameId' => $geonamesid,
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   144
                      'username' => config('corpusparole.geonames_username') ],
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   145
                  'accept' => 'application/json' // TODO: check this
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   146
                ]
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   147
            )->getBody();
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   148
            $hjson = json_decode($apiBody);
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   149
            $hcache = new GeonamesHierarchy();
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   150
            $hcache->geonamesid = $geonamesid;
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   151
            $hcache->hierarchy = $hjson;
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   152
            $hcache->save();
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   153
        }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   154
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   155
        $res = [];
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   156
        foreach($hcache->hierarchy['geonames'] as $hierarchyElem) {
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   157
            if(in_array($hierarchyElem['fcode'], ['CONT','PCLI', 'PCL','PCLD', 'PCLF', 'PCLH', 'PCLIX', 'PCLIS', 'ADM1'])) {
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   158
                array_push($res, $hierarchyElem['geonameId']);
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   159
            }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   160
        }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   161
        return $res;
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   162
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   163
    }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   164
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   165
    /**
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   166
     * get geonames hierarchy data.
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   167
     * @return array list of geonames ids
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   168
     */
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   169
    private function getGeonamesHierarchy($doc) {
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   170
        $geoRes = $doc->getGeoInfo();
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   171
        if(is_null($geoRes)) {
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   172
            return [];
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   173
        }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   174
        // aggregate hierachy list from geonames results
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   175
        $res = [];
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   176
        foreach($geoRes->getGeonamesLocs() as $gurl) {
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   177
            $geonamesId = CocoonUtils::getGeonamesidFromUrl($gurl);
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   178
            if(is_null($geonamesId)) {
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   179
                continue;
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   180
            }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   181
            $hierarchyIds = $this->getGeonamesHierarchyArray($geonamesId);
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   182
            $res = array_unique(array_merge($res, $hierarchyIds));
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   183
        }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   184
        return $res;
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   185
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   186
    }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   187
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   188
    /**
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   189
     * get subjects as { 'label': label, 'code': code } objects
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   190
     * Takes only into account the bnf subjects
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   191
     */
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   192
    private function getSubjects($doc) {
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   193
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   194
        $sres = array_reduce($doc->getSubjects(), function($res, $s) {
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   195
            $mBnf = [];
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   196
            $mLexvo = [];
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   197
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   198
            if($s instanceof Resource && preg_match(config('corpusparole.bnf_ark_url_regexp'), $s->getUri(), $mBnf) === 1) {
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   199
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   200
                array_push($res, [
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   201
                    'uri' => $mBnf[0],
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   202
                    'code' => $mBnf[1],
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   203
                    'type' => 'bnf'
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   204
                ]);
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   205
            } elseif($s instanceof Resource && preg_match(config('corpusparole.lexvo_url_regexp'), $s->getUri(), $mLexvo) === 1) {
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   206
                array_push($res, [
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   207
                    'uri' => $mLexvo[0],
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   208
                    'code' => $mLexvo[1],
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   209
                    'type' => 'lxv'
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   210
                ]);
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   211
            } elseif($s instanceof Literal && strpos($s->getDatatypeUri(), config('corpusparole.olac_base_url')) === 0 ) {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   212
                array_push($res, [
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   213
                    'uri' => $s->getValue(),
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   214
                    'code' => $s->getValue(),
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   215
                    'type' => 'olac'
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   216
                ]);
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   217
            } elseif($s instanceof Literal) {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   218
                array_push($res, [
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   219
                    'uri' => $s->getValue(),
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   220
                    'code' => $s->getValue(),
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   221
                    'type' => 'txt'
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   222
                ]);
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   223
            }
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   224
            return $res;
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   225
        }, []);
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   226
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   227
        $labelsBnf = $this->bnfResolver->getLabels(
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   228
            array_unique(array_reduce(
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   229
                $sres,
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   230
                function($r, $so) {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   231
                    if($so['type'] === 'bnf') {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   232
                        array_push($r, $so['uri']);
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   233
                    }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   234
                    return $r;
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   235
                },[]
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   236
            ))
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   237
        );
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   238
        $labelsLexvo = $this->lexvoResolver->getNames(
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   239
            array_unique(array_reduce(
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   240
                $sres,
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   241
                function($r, $so) {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   242
                    if($so['type'] === 'lxv') {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   243
                        array_push($r, $so['uri']);
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   244
                    }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   245
                    return $r;
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   246
                },[]
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   247
            ))
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   248
        );
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   249
322
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   250
        return array_map(function($so) use ($labelsBnf, $labelsLexvo) {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   251
            $label = $so['uri'];
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   252
            if($so['type'] === 'bnf') {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   253
                $label = $labelsBnf[$label];
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   254
            } elseif ($so['type'] === 'lxv') {
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   255
                $label = $labelsLexvo[$label];
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   256
            }
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   257
            return [ 'label' => $label, 'code' => $so['code'], 'label_code' =>  $label."|".$so['type']."|".$so['code'] ]; }, $sres
084aae09edf4 correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents: 321
diff changeset
   258
        );
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   259
    }
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   260
325
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   261
    private function graphResolvCoordinate($loc, $graph) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   262
        $latLit = $graph->getLiteral($loc, "<http://www.w3.org/2003/01/geo/wgs84_pos#lat>");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   263
        if(is_null($latLit) || empty($latLit->getValue())) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   264
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   265
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   266
        $lat = $latLit->getValue();
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   267
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   268
        $longLit = $graph->getLiteral($loc, "<http://www.w3.org/2003/01/geo/wgs84_pos#long>");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   269
        if(is_null($longLit) || empty($longLit->getValue())) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   270
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   271
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   272
        $long = $longLit->getValue();
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   273
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   274
        return [ $lat, $long ];
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   275
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   276
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   277
    private function loadGraph($url, $type) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   278
        try {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   279
            $r = $this->httpClient->get($url);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   280
        } catch (TransferException $e) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   281
            $this->error("loadGraph : Error Loading $url");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   282
            Log::error("loadGraph : Error Loading $url");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   283
            Log::error("loadGraph : Error request " . Psr7\str($e->getRequest()));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   284
            if ($e->hasResponse()) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   285
                $this->error("loadGraph : Error response " . Psr7\str($e->getResponse()));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   286
                Log::error("loadGraph : Error response " . Psr7\str($e->getResponse()));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   287
            }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   288
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   289
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   290
        try {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   291
            $message = (string)$r->getBody();
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   292
            $graph = new Graph($url, $message, $type);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   293
            return $graph;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   294
        } catch (EasyRdf\Exception $e) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   295
            $this->error("loadGraph : Error parsing $url");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   296
            Log::error("loadGraph : Error parsing $url");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   297
            if($e instanceof EasyRdf\Parser\Exception) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   298
                Log::error("loadGraph : Error exception line ".$e->getLine().", column: ".$e->getColumn());
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   299
            }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   300
            $this->error("loadGraph : Error exception message ".$e->getMessage());
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   301
            Log::error("loadGraph : Error exception message ".$e->getMessage());
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   302
            Log::error("loadGraph : Error content $message");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   303
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   304
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   305
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   306
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   307
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   308
    private function geonamesResolveCoordinates($loc) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   309
        $coords = cache("corpus.geonames.coord.$loc");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   310
        if(is_null($coords)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   311
            $graph = $this->loadGraph("{$loc}about.rdf", 'rdfxml');
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   312
            $coords = is_null($graph)?null:$this->graphResolvCoordinate($loc, $graph);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   313
            cache(["corpus.geonames.coord.$loc" => is_null($coords)?false:$coords], Carbon::now()->addMinutes(20));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   314
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   315
        return ($coords===false)?null:$coords;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   316
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   317
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   318
    private function dbpediaResolveCoordinates($loc) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   319
        $coords = cache("corpus.dbpedia.coord.$loc");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   320
        if(is_null($coords)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   321
            $graph = $this->loadGraph("$loc.rdf", 'rdfxml');
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   322
            $coords = is_null($graph)?null:$this->graphResolvCoordinate($loc, $graph);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   323
            cache(["corpus.dbpedia.coord.$loc"=> is_null($coords)?false:$coords], Carbon::now()->addMinutes(20));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   324
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   325
        return ($coords===false)?null:$coords;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   326
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   327
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   328
    private function getLocation($doc) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   329
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   330
        $geoRes = $doc->getGeoInfo();
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   331
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   332
        if(is_null($geoRes)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   333
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   334
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   335
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   336
        $locUrls = [];
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   337
        foreach($geoRes->getRefLocs() as $loc) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   338
            if(preg_match(config('corpusparole.geonames_url_regexp'), $loc, $m) === 1) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   339
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   340
                if(!array_key_exists('geonames', $locUrls)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   341
                    $locUrls['geonames'] = [];
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   342
                }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   343
                array_push($locUrls['geonames'], "http://sws.geonames.org/$m[1]/");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   344
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   345
            } elseif(preg_match(config('corpusparole.dbpedia_url_regexp'), $loc, $md) === 1) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   346
                if(!array_key_exists('dbpedia', $locUrls)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   347
                    $locUrls['dbpedia'] = [];
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   348
                }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   349
                //$this->line("DBPEDIA MATCH $loc ".print_r($md,true));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   350
                array_push($locUrls['dbpedia'], "http://$md[1]/data/$md[4]");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   351
            }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   352
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   353
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   354
        $coordinates = null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   355
        foreach($locUrls as $locType => $locList) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   356
            foreach($locList as $locationUrl) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   357
                $coordinates = call_user_func([$this, "${locType}ResolveCoordinates"], $locationUrl);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   358
                if(!is_null($coordinates)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   359
                    break;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   360
                }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   361
            }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   362
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   363
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   364
        if(is_null($coordinates)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   365
            $coordinates = [$geoRes->getLatitudeValue(), $geoRes->getLongitudeValue()];
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   366
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   367
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   368
        if(empty($coordinates[0]) || empty($coordinates[1])) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   369
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   370
        } else {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   371
            return [floatval($coordinates[0]), floatval($coordinates[1])];
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   372
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   373
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   374
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   375
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   376
    private function getCreationDate($doc) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   377
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   378
        $created = $doc->getCreated();
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   379
        if(is_null($created)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   380
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   381
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   382
        $dateType = $created->getDatatypeUri();
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   383
        $res = null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   384
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   385
        if($dateType === "http://purl.org/dc/terms/Period") {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   386
            $res = $this->processPeriod($created->getValue());
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   387
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   388
        elseif($dateType === "http://purl.org/dc/terms/W3CDTF") {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   389
            $res = $this->processDate($created->getValue());
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   390
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   391
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   392
        return $res;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   393
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   394
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   395
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   396
    private function extractDate($dateStr) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   397
        if(preg_match("/^\\d{4}$/", $dateStr) === 1) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   398
            $dateStr = "$dateStr-1-1";
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   399
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   400
        $date = date_create($dateStr);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   401
        if($date === false ) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   402
            Log::warning("DateStatsController:extractYear bad format for date $dateStr");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   403
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   404
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   405
        return $date;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   406
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   407
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   408
    private function processPeriod($periodStr) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   409
        $start = null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   410
        $end = null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   411
        foreach(explode(";", $periodStr) as $elem) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   412
            $elem = trim($elem);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   413
            if(strpos($elem, 'start=') === 0) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   414
                $startDate = $this->extractDate(trim(substr($elem, 6)));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   415
                if(is_null($startDate)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   416
                    return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   417
                }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   418
                $start = intval($startDate->format("Y"));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   419
                if($start === false) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   420
                    return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   421
                }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   422
            } elseif(strpos($elem, 'end=') === 0) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   423
                $endDate = $this->extractDate(trim(substr($elem, 4)));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   424
                if(is_null($endDate)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   425
                    return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   426
                }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   427
                $end = intval($endDate->format("Y"));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   428
                if($end === false) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   429
                    return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   430
                }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   431
            }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   432
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   433
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   434
        if(is_null($start) || is_null($end) || $start>$end ) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   435
            Log::warning("Bad format for $periodStr");
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   436
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   437
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   438
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   439
        return array_map(function($y) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   440
            return \DateTime::createFromFormat("Y", "$y")->format(\DateTime::W3C);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   441
        }, range($start, $end));
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   442
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   443
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   444
    private function processDate($dateStr) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   445
        $date = $this->extractDate($dateStr);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   446
        if(is_null($date))  {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   447
            return null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   448
        } else {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   449
            return $date->format(\DateTime::W3C);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   450
        }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   451
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   452
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   453
    private function getDiscourseTypes($doc) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   454
        return array_reduce($doc->getDiscourseTypes(), function($res, $d) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   455
            $val = null;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   456
            if($d instanceof Resource) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   457
                $val = $d->getUri();
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   458
            } elseif($d instanceof Literal) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   459
                $datatype = $d->getDatatypeURI();
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   460
                $val = (!empty($datatype)?"$datatype#":"").$d->getValue();
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   461
            }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   462
            if(!empty($val)) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   463
                array_push($res,$val);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   464
            }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   465
            return $res;
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   466
        }, []);
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   467
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   468
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   469
    private function getDocBody($doc) {
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   470
        return [
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   471
            'title' => (string)$doc->getTitle(),
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   472
            'date' => (string)$doc->getModified(),
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   473
            'location' => $this->getLocation($doc),
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   474
            'creation_date' => $this->getCreationDate($doc),
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   475
            'language' => $doc->getLanguagesValue(),
325
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   476
            'discourse_types' => $this->getDiscourseTypes($doc),
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   477
            'geonames_hierarchy' => $this->getGeonamesHierarchy($doc),
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   478
            'subject' => $this->getSubjects($doc),
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   479
        ];
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   480
    }
31a4987f6017 Add fields to document index
ymh <ymh.work@gmail.com>
parents: 323
diff changeset
   481
321
aefaad270b9b reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents: 320
diff changeset
   482
    /**
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   483
     * Index one document into Elasticsearch
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   484
     *
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   485
     * @return int (1 if sucess, 0 if error)
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   486
     */
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   487
    private function indexOne($docId, $docBody)
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   488
    {
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   489
        $query_data = [
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   490
            'index' => config('elasticsearch.index'),
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   491
            'type' => 'document',
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   492
            'id' => $docId,
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   493
            'body' => $docBody
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   494
        ];
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   495
        Es::index($query_data);
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   496
    }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   497
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   498
    /**
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   499
     * Index multiple document into Elasticsearch
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   500
     *
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   501
     * @return int (1 if sucess, 0 if error)
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   502
     */
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   503
     private function indexBulk($docBodies)
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   504
     {
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   505
          $query_data = ['body' => []];
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   506
          foreach($docBodies as $docId => $docBody){
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   507
              $query_data['body'][] = [
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   508
                  'index' => [
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents: 25
diff changeset
   509
                      '_index' => config('elasticsearch.index'),
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   510
                      '_type' => 'document',
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   511
                      '_id' => $docId
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   512
                  ]
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   513
              ];
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   514
              $query_data['body'][] = $docBody;
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   515
          }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   516
          Es::bulk($query_data);
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   517
     }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   518
    /**
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   519
     * Execute the console command.
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   520
     *
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   521
     * @return mixed
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   522
     */
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   523
    public function handle()
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   524
    {
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   525
        $this->info('Options:');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   526
        $noBulk = $this->option('no-bulk');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   527
        if ($noBulk)
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   528
        {
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   529
            $this->comment(' - Indexing without bulk insert');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   530
        }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   531
        else
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   532
        {
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   533
            $this->comment(' - Indexing using bulk insert');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   534
        }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   535
        $limit = $this->option('limit');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   536
        if ($limit>0) {
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   537
            $this->comment(' - Indexing only the first '.$limit.' documents');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   538
        }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   539
        $stepSize = $this->option('step-size');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   540
        $this->comment(' - Indexing with step size of '.$stepSize);
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   541
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   542
        if($this->option('reset-geo-cache', false)) {
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   543
            // delete all rows in GeonamesHierarchy
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   544
            GeonamesHierarchy::getQuery()->delete();
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   545
            $this->comment('Geonames cache reset!');
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   546
        }
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   547
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   548
        $this->info('Resetting index...');
323
47f0611cc57d correct theme controller
ymh <ymh.work@gmail.com>
parents: 322
diff changeset
   549
        $success = $this->resetIndex();
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   550
        if($success==1){
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   551
            $this->comment('Index reset!');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   552
        }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   553
        else{
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   554
            $this->error('Error resetting index ' . env('ELASTICSEARCH_INDEX'));
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   555
        }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   556
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   557
        $this->info('Indexing documents...');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   558
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   559
        $limit = (int)$limit;
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   560
        $total = $this->documentRepository->getCount();
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   561
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   562
        if($limit>0) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   563
            $total = min($limit, $total);
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   564
        }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   565
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   566
        $progressBar = $this->output->createProgressBar($total);
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   567
        $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%');
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   568
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   569
        $page = 0;
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   570
        $lastPage = PHP_INT_MAX;
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   571
        $docIds = [];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   572
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   573
        while($page++<$lastPage) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   574
            $docsPaginator = $this->documentRepository->paginate(null, $stepSize, config('corpusparole.pagination_page_param'), $page, "_graph");
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   575
            $lastPage = $docsPaginator->lastPage();
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   576
            $docsBodies = [];
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   577
            foreach($docsPaginator as $docResult) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   578
                $docId = (string)$docResult->getId();
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   579
                $progressBar->setMessage($docId);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   580
                $progressBar->advance();
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   581
                $doc = $this->documentRepository->get($docId);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   582
                $docBody = $this->getDocBody($doc);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   583
                if($noBulk) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   584
                    $this->indexOne($docId, $docBody);
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   585
                } else {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   586
                    $docsBodies[$docId] = $docBody;
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   587
                }
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   588
                $docIds[] = $docId;
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   589
            }
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   590
            if(!$noBulk) {
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   591
                $this->indexBulk($docsBodies);
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   592
            }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   593
        }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   594
        $progressBar->finish();
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   595
        $this->info("\nIndexing completed for " . count(array_unique($docIds))." documents (of ".count($docIds).").");
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 325
diff changeset
   596
24
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   597
    }
de47e8f66e8b Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff changeset
   598
}