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