server/src/app/Console/Commands/ManageHandle.php
author ymh <ymh.work@gmail.com>
Fri, 22 Apr 2016 11:20:17 +0200
changeset 153 338bcc78d431
permissions -rw-r--r--
add HandleClient implementation + configuration + tests
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
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
use CorpusParole\Libraries\Handle\handleClient;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
class ManageHandle extends Command
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
{
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    /**
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
     * The name and signature of the console command.
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
     * @var string
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
     */
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    protected $signature = 'corpus-parole:manageHandle
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
                                {--r|replace: Replace value for existing handles}
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
                                {--k|key=: Private key}
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
                                {--p|password=: key password}
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
                                {--P|ask-password: ask for private key password}
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
                                {--step-size=100 : number of documents to retrieve from repository at a time before indexing}';
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    /**
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
     * The console command description.
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
     *
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
     * @var string
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
     */
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    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
    29
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
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
     * Create a new command instance.
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
     * @return void
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
    public function __construct(DocumentRepository $documentRepository)
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
        $this->documentRepository = $documentRepository;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        parent::__construct();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    private function registerHandle($doc) {
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
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    /**
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
     * Execute the console command.
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
     * @return mixed
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
     */
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    public function handle()
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
        $stepSize = $this->option('step-size');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        $passwordKey = $this->option('password');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        if($this->option('ask-password')) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            $password = $this->secret('Private key password?');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        }
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
        $privateKey = $this->option('key');
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
        $privateKeyRes = openssl_pkey_get_private($privateKey, $password);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        // create handle client
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        $this->handleClient = new HandleClient($privateKeyRes);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        $this->info('Adding documents handles...');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        $lastPage = $this->documentRepository->paginateAll($stepSize, 'page')->lastPage();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        $total = $this->documentRepository->getCount();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        $progressBar = $this->output->createProgressBar($total);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        for ($page=1;$page<=$lastPage;$page++)
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
            $docs = $this->documentRepository->paginateAll($stepSize, 'page', $page);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            foreach ($docs as $doc){
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
                $this->registerHandle($doc);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
                $progressBar->setMessage($doc->getId());
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                $progressBar->advance();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        $progressBar->finish();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        $this->info('Adding handles completed');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        $this->info('Removing extra handles...');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        $lastPage = -1;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        $page = 0;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        $lastPage = 0;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        while($lastPage<0 || $page <= $lastPage) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
            $handle_res = $this->handleClient->paginateAll($stepSize, 'page', $page++);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
            foreach($handle_res as $handle) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
                if(is_null($this->documentRepository->get($handle))) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
                    $this->handleClient->deleteHandle($handle);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
                }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
                $progressBar->setMessage($handle);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
                $progressBar->advance();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
            }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        }
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
        $this->info('Removing extra handles completed');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        // query ids
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        // loop on ids
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        // PUT handle
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        // query handle
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        // if not in rdf ids delete
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        // delete handle session
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
}