correct handle managing
authorymh <ymh.work@gmail.com>
Sun, 24 Apr 2016 22:38:10 +0200
changeset 154 ded3cf22eef8
parent 153 338bcc78d431
child 155 76ae75975ee2
correct handle managing
server/src/app/Console/Commands/ManageHandle.php
server/src/app/Console/Commands/ManageHandles.php
server/src/app/Console/Kernel.php
server/src/app/Libraries/Handle/HandleClient.php
server/src/config/corpusparole.php
--- a/server/src/app/Console/Commands/ManageHandle.php	Fri Apr 22 11:20:17 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,116 +0,0 @@
-<?php
-
-namespace CorpusParole\Console\Commands;
-
-use Illuminate\Console\Command;
-
-use CorpusParole\Libraries\Handle\handleClient;
-
-class ManageHandle extends Command
-{
-    /**
-     * The name and signature of the console command.
-     *
-     * @var string
-     */
-    protected $signature = 'corpus-parole:manageHandle
-                                {--r|replace: Replace value for existing handles}
-                                {--k|key=: Private key}
-                                {--p|password=: key password}
-                                {--P|ask-password: ask for private key password}
-                                {--step-size=100 : number of documents to retrieve from repository at a time before indexing}';
-
-    /**
-     * The console command description.
-     *
-     * @var string
-     */
-    protected $description = 'Synchronize the handle registry with the content of the rdf repository.';
-
-
-    /**
-     * Create a new command instance.
-     *
-     * @return void
-     */
-    public function __construct(DocumentRepository $documentRepository)
-    {
-        $this->documentRepository = $documentRepository;
-        parent::__construct();
-    }
-
-    private function registerHandle($doc) {
-
-    }
-
-    /**
-     * Execute the console command.
-     *
-     * @return mixed
-     */
-    public function handle()
-    {
-        $stepSize = $this->option('step-size');
-
-        $passwordKey = $this->option('password');
-        if($this->option('ask-password')) {
-            $password = $this->secret('Private key password?');
-        }
-
-        $privateKey = $this->option('key');
-
-        $privateKeyRes = openssl_pkey_get_private($privateKey, $password);
-
-
-        // create handle client
-        $this->handleClient = new HandleClient($privateKeyRes);
-
-        $this->info('Adding documents handles...');
-
-        $lastPage = $this->documentRepository->paginateAll($stepSize, 'page')->lastPage();
-        $total = $this->documentRepository->getCount();
-
-        $progressBar = $this->output->createProgressBar($total);
-        $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%');
-
-        for ($page=1;$page<=$lastPage;$page++)
-        {
-            $docs = $this->documentRepository->paginateAll($stepSize, 'page', $page);
-            foreach ($docs as $doc){
-                $this->registerHandle($doc);
-                $progressBar->setMessage($doc->getId());
-                $progressBar->advance();
-            }
-        }
-        $progressBar->finish();
-        $this->info('Adding handles completed');
-
-        $this->info('Removing extra handles...');
-
-        $lastPage = -1;
-        $page = 0;
-        $lastPage = 0;
-
-        while($lastPage<0 || $page <= $lastPage) {
-            $handle_res = $this->handleClient->paginateAll($stepSize, 'page', $page++);
-            foreach($handle_res as $handle) {
-                if(is_null($this->documentRepository->get($handle))) {
-                    $this->handleClient->deleteHandle($handle);
-                }
-                $progressBar->setMessage($handle);
-                $progressBar->advance();
-            }
-        }
-
-        $this->info('Removing extra handles completed');
-
-
-        // query ids
-        // loop on ids
-        // PUT handle
-        // query handle
-        // if not in rdf ids delete
-
-        // delete handle session
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/app/Console/Commands/ManageHandles.php	Sun Apr 24 22:38:10 2016 +0200
@@ -0,0 +1,150 @@
+<?php
+
+namespace CorpusParole\Console\Commands;
+
+use Illuminate\Console\Command;
+
+use GuzzleHttp\Client;
+use CorpusParole\Libraries\Handle\handleClient;
+use CorpusParole\Libraries\CocoonUtils;
+use CorpusParole\Repositories\DocumentRepository;
+
+class ManageHandles extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'corpus-parole:manageHandles
+                                {--r|replace : Replace value for existing handles}
+                                {--k|key= : Private key}
+                                {--p|password=: key password}
+                                {--P|ask-password : ask for private key password}
+                                {--a|admin-id= : the admin index + handle - default to config value corpusparole.handle_admin_id, env var HANDLE_ADMIN_ID}
+                                {--H|host= : the handle host format <ip[:port]> - default to corpusparole.handle_host:corpusparole.handle_port (env HANDLE_HOST:HANDLE_PORT)}
+                                {--x|prefix= : handle prefix (default to corpusparole.handle_prefix, env HANDLE_PREFIX)}
+                                {--s|step-size=100 : number of documents to retrieve from repository at a time before indexing}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Synchronize the handle registry with the content of the rdf repository.';
+
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(DocumentRepository $documentRepository, Client $httpClient)
+    {
+        $this->documentRepository = $documentRepository;
+        $this->httpClient = $httpClient;
+        parent::__construct();
+    }
+
+    private function registerHandle($doc) {
+        $this->handleClient->createHandleUrlRecord($doc->getId(), env('APP_URL')."/docs/".$doc->getId());
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $stepSize = $this->option('step-size');
+
+        $passwordKey = $this->option('password');
+        if($this->option('ask-password')) {
+            $passwordKey = $this->secret('Private key password?');
+        }
+
+        $privateKey = $this->option('key');
+        if(empty($privateKey)) {
+            $privateKey = config('corpusparole.handle_cert_or_pkey');
+        }
+        if(empty($privateKey)) {
+            throw new Exception("No private key found");
+        }
+
+        $adminId = $this->option('admin-id');
+        if(empty($adminId)) {
+            $adminId = config('corpusparole.handle_admin_id');
+        }
+        if(empty($adminId)) {
+            throw new Exception("No admin id found");
+        }
+
+        $handleHost = $this->option('host');
+        if(!empty($handleHost)) {
+            list($handleHost, $handlePort) = array_pad(explode($handleHost, ':'), 2, 8000);
+        }
+        if(empty($handleHost)) {
+            $handleHost = config('corpusparole.handle_host');
+        }
+        if(empty($handlePort)) {
+            $handlePort = config('corpusparole.handle_port', 8000);
+        }
+        if(empty($handleHost)) {
+            throw new Exception("No handle host found");
+        }
+
+        $this->handlePrefix = $this->option('prefix');
+        if(empty($this->handlePrefix)) {
+            $this->handlePrefix = config('corpusparole.handle_prefix');
+        }
+        if(empty($this->handlePrefix)) {
+            throw new Exception("No prefix found");
+        }
+
+        // create handle client
+        $this->handleClient = new HandleClient($privateKey, $passwordKey, $adminId, $handleHost, $handlePort, $this->httpClient);
+
+        $this->info("Adding documents handles...");
+
+        $total = $this->documentRepository->getCount();
+
+        $docs = $this->documentRepository->paginateAll($stepSize, 'page');
+
+        $progressBar = $this->output->createProgressBar($docs->total());
+        $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%');
+
+        while(!is_null($docs)) {
+            foreach ($docs as $doc){
+                $this->registerHandle($doc);
+                $progressBar->setMessage($doc->getId());
+                $progressBar->advance();
+            }
+            $docs = ($docs->hasMorePages()? $this->documentRepository->paginateAll($stepSize, 'page', $docs->currentPage()+1):null);
+        }
+
+        $progressBar->finish();
+        $this->info("\nAdding handles completed\n");
+
+        $this->info("Removing extra handles...\n");
+
+        $handles = $this->handleClient->paginateAll($this->handlePrefix, $stepSize, 'page');
+
+        $progressBar = $this->output->createProgressBar($handles->total());
+        $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%');
+
+        while(!is_null($handles)) {
+            foreach($handles as $handle) {
+                if(strpos($handle, config('corpusparole.corpus_id_prefix')) === 0 && is_null($this->documentRepository->get($handle))) {
+                    $this->handleClient->deleteHandle($handle);
+                }
+                $progressBar->setMessage($handle);
+                $progressBar->advance();
+            }
+            $handles = $handles->hasMorePages()?$this->handleClient->paginateAll($this->handlePrefix, $stepSize, 'page', $handles->currentPage()+1):null;
+        }
+
+        $this->info("\nRemoving extra handles completed");
+
+    }
+}
--- a/server/src/app/Console/Kernel.php	Fri Apr 22 11:20:17 2016 +0200
+++ b/server/src/app/Console/Kernel.php	Sun Apr 24 22:38:10 2016 +0200
@@ -16,6 +16,7 @@
         'CorpusParole\Console\Commands\Inspire',
         'CorpusParole\Console\Commands\ImportCocoonRDF',
         'CorpusParole\Console\Commands\IndexDocuments',
+        'CorpusParole\Console\Commands\ManageHandles',
     ];
 
     /**
--- a/server/src/app/Libraries/Handle/HandleClient.php	Fri Apr 22 11:20:17 2016 +0200
+++ b/server/src/app/Libraries/Handle/HandleClient.php	Sun Apr 24 22:38:10 2016 +0200
@@ -42,7 +42,7 @@
 
     public function close() {
         $this->deleteSession();
-        $this->freeResources();        
+        $this->freeResources();
     }
 
     private function getBaseUrl() {
@@ -60,7 +60,7 @@
     private function getPrivateKeyRes() {
         if(is_null($this->privateKeyRes)) {
             $this->privateKeyRes = openssl_pkey_get_private($this->privateKeyOrCert, $this->pkpass);
-        }       
+        }
         return $this->privateKeyRes;
     }
 
@@ -93,7 +93,7 @@
         }
         $rsa->loadKey($keyContent);
         $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1);
-        
+
         return $rsa->sign($str);
 
     }
@@ -236,17 +236,20 @@
         $total = (int)$paginateJson['totalCount'];
 
         $results = $paginateJson['handles'];
-        
+
         return new LengthAwarePaginator($results, $total, $perPage, $page, [
             'path' => Paginator::resolveCurrentPath(),
             'pageName' => $pageName,
         ]);
-   
+
     }
 
     public function deleteHandle($handle) {
         $this->initSession();
-        
+
+        if($handle === $this->adminId) {
+            throw new \Exception("HandleClient: can not delete admin handle");
+        }
         $delUrl = $this->getBaseUrl()."handles/$handle";
         $delRes = $this->httpClient->delete($delUrl, $this->guzzleOptions);
 
--- a/server/src/config/corpusparole.php	Fri Apr 22 11:20:17 2016 +0200
+++ b/server/src/config/corpusparole.php	Sun Apr 24 22:38:10 2016 +0200
@@ -21,9 +21,10 @@
     'cocoon_doc_pub_base_uri' => 'http://corpusdelaparole.huma-num.fr/corpus-app#/detail/',
     'cocoon_doc_id_base' => 'oai:crdo.vjf.cnrs.fr:',
 
-    'corpus_id_scheme' => '11280.100/',
+    'corpus_id_scheme' => env('HANDLE_PREFIX').'/',
+    'corpus_id_prefix' => env('HANDLE_PREFIX').'/crdo-',
 
-    'corpus_doc_id_base_uri' => 'https://hdl.handle.net/11280.100/',
+    'corpus_doc_id_base_uri' => 'https://hdl.handle.net/'.env('HANDLE_PREFIX').'/',
     'corpus_doc_default_cc_rights' => 'http://creativecommons.org/licenses/by/4.0/',
 
     'edm_provider' => 'Corpus de la Parole',
@@ -139,6 +140,11 @@
         "exportApplicationGlobal" => true
     ],
 
-    'handle_host' => env('HANDLE_HOST'),
-    'handle_port' => env('HANDLE_PORT'),
+    'handle_host'         => env('HANDLE_HOST'),
+    'handle_port'         => env('HANDLE_PORT', 8000),
+    'handle_admin_id'     => env('HANDLE_ADMIN_ID'),
+    'handle_prefix'       => env('HANDLE_PREFIX'),
+    'handle_cert_or_pkey' => env('HANDLE_CERT_OR_PKEY'),
+    'handle_password'     => env('HANDLE_PASSWORD')
+
 ];