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