server/src/app/Console/Commands/ManageHandles.php
author ymh <ymh.work@gmail.com>
Wed, 09 Nov 2016 15:05:41 +0100
changeset 406 cf0f23803a53
parent 326 226d5b17a119
child 552 2c579ea45608
permissions -rw-r--r--
upgrade elasticsearch to 5.0, upgrade ember
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
namespace CorpusParole\Console\Commands;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
use Illuminate\Console\Command;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
     7
use GuzzleHttp\Client;
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
use CorpusParole\Libraries\Handle\handleClient;
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
     9
use CorpusParole\Libraries\CocoonUtils;
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    10
use CorpusParole\Repositories\DocumentRepository;
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    12
class ManageHandles extends Command
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
{
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    /**
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
     * The name and signature of the console command.
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
     *
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
     * @var string
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
     */
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    19
    protected $signature = 'corpus-parole:manageHandles
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    20
                                {--r|replace : Replace value for existing handles}
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    21
                                {--k|key= : Private key}
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
                                {--p|password=: key password}
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    23
                                {--P|ask-password : ask for private key password}
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    24
                                {--a|admin-id= : the admin index + handle - default to config value corpusparole.handle_admin_id, env var HANDLE_ADMIN_ID}
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    25
                                {--H|host= : the handle host format <ip[:port]> - default to corpusparole.handle_host:corpusparole.handle_port (env HANDLE_HOST:HANDLE_PORT)}
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    26
                                {--x|prefix= : handle prefix (default to corpusparole.handle_prefix, env HANDLE_PREFIX)}
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    27
                                {--s|step-size=100 : number of documents to retrieve from repository at a time before indexing}';
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    /**
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
     * The console command description.
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
     *
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
     * @var string
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
     */
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    protected $description = 'Synchronize the handle registry with the content of the rdf repository.';
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    /**
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
     * Create a new command instance.
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
     *
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
     * @return void
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
     */
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    42
    public function __construct(DocumentRepository $documentRepository, Client $httpClient)
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        $this->documentRepository = $documentRepository;
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    45
        $this->httpClient = $httpClient;
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        parent::__construct();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    private function registerHandle($doc) {
406
cf0f23803a53 upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents: 326
diff changeset
    50
        $this->handleClient->createHandleUrlRecord($doc->getId(), config('app.url')."/docs/".$doc->getId());
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    /**
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
     * Execute the console command.
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
     *
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
     * @return mixed
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
     */
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    public function handle()
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        $stepSize = $this->option('step-size');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        $passwordKey = $this->option('password');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        if($this->option('ask-password')) {
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    64
            $passwordKey = $this->secret('Private key password?');
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        $privateKey = $this->option('key');
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    68
        if(empty($privateKey)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    69
            $privateKey = config('corpusparole.handle_cert_or_pkey');
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    70
        }
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    71
        if(empty($privateKey)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    72
            throw new Exception("No private key found");
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    73
        }
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    75
        $adminId = $this->option('admin-id');
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    76
        if(empty($adminId)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    77
            $adminId = config('corpusparole.handle_admin_id');
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    78
        }
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    79
        if(empty($adminId)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    80
            throw new Exception("No admin id found");
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    81
        }
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    83
        $handleHost = $this->option('host');
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    84
        if(!empty($handleHost)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    85
            list($handleHost, $handlePort) = array_pad(explode($handleHost, ':'), 2, 8000);
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    86
        }
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    87
        if(empty($handleHost)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    88
            $handleHost = config('corpusparole.handle_host');
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    89
        }
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    90
        if(empty($handlePort)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    91
            $handlePort = config('corpusparole.handle_port', 8000);
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    92
        }
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    93
        if(empty($handleHost)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    94
            throw new Exception("No handle host found");
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    95
        }
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    96
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    97
        $this->handlePrefix = $this->option('prefix');
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    98
        if(empty($this->handlePrefix)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    99
            $this->handlePrefix = config('corpusparole.handle_prefix');
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   100
        }
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   101
        if(empty($this->handlePrefix)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   102
            throw new Exception("No prefix found");
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   103
        }
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        // create handle client
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   106
        $this->handleClient = new HandleClient($privateKey, $passwordKey, $adminId, $handleHost, $handlePort, $this->httpClient);
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   108
        $this->info("Adding documents handles...");
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        $total = $this->documentRepository->getCount();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   112
        $docs = $this->documentRepository->paginate(null, $stepSize, config('corpusparole.pagination_page_param'), 1, '_graph');
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   113
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   114
        $progressBar = $this->output->createProgressBar($docs->total());
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
        $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   117
        while(!is_null($docs)) {
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
            foreach ($docs as $doc){
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
                $this->registerHandle($doc);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
                $progressBar->setMessage($doc->getId());
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
                $progressBar->advance();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
            }
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   123
            $docs = ($docs->hasMorePages()? $this->documentRepository->paginate(null, $stepSize, config('corpusparole.pagination_page_param'), $docs->currentPage()+1, '_graph'):null);
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        }
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   125
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
        $progressBar->finish();
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   127
        $this->info("\nAdding handles completed\n");
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   129
        $this->info("Removing extra handles...\n");
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   131
        $handles = $this->handleClient->paginateAll($this->handlePrefix, $stepSize, config('corpusparole.pagination_page_param'));
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   133
        $progressBar = $this->output->createProgressBar($handles->total());
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   134
        $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%');
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   135
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   136
        while(!is_null($handles)) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   137
            foreach($handles as $handle) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   138
                if(strpos($handle, config('corpusparole.corpus_id_prefix')) === 0 && is_null($this->documentRepository->get($handle))) {
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
                    $this->handleClient->deleteHandle($handle);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
                }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
                $progressBar->setMessage($handle);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
                $progressBar->advance();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
            }
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   144
            $handles = $handles->hasMorePages()?$this->handleClient->paginateAll($this->handlePrefix, $stepSize, config('corpusparole.pagination_page_param'), $handles->currentPage()+1):null;
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   147
        $this->info("\nRemoving extra handles completed");
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
}