author | ymh <ymh.work@gmail.com> |
Tue, 20 Mar 2018 15:02:40 +0100 | |
changeset 573 | 25f3d28f51b2 |
parent 565 | 5e02aa0899f7 |
permissions | -rw-r--r-- |
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 | 7 |
use GuzzleHttp\Client; |
563
41e4eb4d8b82
Correct Handle creation and correct permalink
ymh <ymh.work@gmail.com>
parents:
552
diff
changeset
|
8 |
use CorpusParole\Libraries\Handle\HandleClient; |
154 | 9 |
use CorpusParole\Libraries\CocoonUtils; |
10 |
use CorpusParole\Repositories\DocumentRepository; |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
|
154 | 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 | 19 |
protected $signature = 'corpus-parole:manageHandles |
20 |
{--r|replace : Replace value for existing handles} |
|
21 |
{--k|key= : Private key} |
|
565 | 22 |
{--p|password= : key password} |
154 | 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}'; |
|
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 | 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 | 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) { |
563
41e4eb4d8b82
Correct Handle creation and correct permalink
ymh <ymh.work@gmail.com>
parents:
552
diff
changeset
|
50 |
$baseUrl = str_replace('corpus-back', 'corpus-app', config('app.url')); |
41e4eb4d8b82
Correct Handle creation and correct permalink
ymh <ymh.work@gmail.com>
parents:
552
diff
changeset
|
51 |
$this->handleClient->createHandleUrlRecord($doc->getId(), "$baseUrl#/doc/".urlencode($doc->getId()), $doc->getTitle()); |
153
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 |
/** |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
* Execute the console command. |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
* |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
* @return mixed |
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 |
public function handle() |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
{ |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
$stepSize = $this->option('step-size'); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
|
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
$passwordKey = $this->option('password'); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
if($this->option('ask-password')) { |
154 | 65 |
$passwordKey = $this->secret('Private key password?'); |
153
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 |
|
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
$privateKey = $this->option('key'); |
154 | 69 |
if(empty($privateKey)) { |
70 |
$privateKey = config('corpusparole.handle_cert_or_pkey'); |
|
71 |
} |
|
72 |
if(empty($privateKey)) { |
|
73 |
throw new Exception("No private key found"); |
|
74 |
} |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
|
154 | 76 |
$adminId = $this->option('admin-id'); |
77 |
if(empty($adminId)) { |
|
78 |
$adminId = config('corpusparole.handle_admin_id'); |
|
79 |
} |
|
80 |
if(empty($adminId)) { |
|
81 |
throw new Exception("No admin id found"); |
|
82 |
} |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
|
154 | 84 |
$handleHost = $this->option('host'); |
85 |
if(!empty($handleHost)) { |
|
86 |
list($handleHost, $handlePort) = array_pad(explode($handleHost, ':'), 2, 8000); |
|
87 |
} |
|
88 |
if(empty($handleHost)) { |
|
89 |
$handleHost = config('corpusparole.handle_host'); |
|
90 |
} |
|
91 |
if(empty($handlePort)) { |
|
92 |
$handlePort = config('corpusparole.handle_port', 8000); |
|
93 |
} |
|
94 |
if(empty($handleHost)) { |
|
95 |
throw new Exception("No handle host found"); |
|
96 |
} |
|
97 |
||
98 |
$this->handlePrefix = $this->option('prefix'); |
|
99 |
if(empty($this->handlePrefix)) { |
|
100 |
$this->handlePrefix = config('corpusparole.handle_prefix'); |
|
101 |
} |
|
102 |
if(empty($this->handlePrefix)) { |
|
103 |
throw new Exception("No prefix found"); |
|
104 |
} |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
105 |
|
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
106 |
// create handle client |
154 | 107 |
$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
|
108 |
|
154 | 109 |
$this->info("Adding documents handles..."); |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
110 |
|
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
111 |
$total = $this->documentRepository->getCount(); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
112 |
|
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
154
diff
changeset
|
113 |
$docs = $this->documentRepository->paginate(null, $stepSize, config('corpusparole.pagination_page_param'), 1, '_graph'); |
154 | 114 |
|
115 |
$progressBar = $this->output->createProgressBar($docs->total()); |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
116 |
$progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%'); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
117 |
|
154 | 118 |
while(!is_null($docs)) { |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
119 |
foreach ($docs as $doc){ |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
120 |
$this->registerHandle($doc); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
121 |
$progressBar->setMessage($doc->getId()); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
122 |
$progressBar->advance(); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
123 |
} |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
154
diff
changeset
|
124 |
$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
|
125 |
} |
154 | 126 |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
127 |
$progressBar->finish(); |
154 | 128 |
$this->info("\nAdding handles completed\n"); |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
129 |
|
154 | 130 |
$this->info("Removing extra handles...\n"); |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
131 |
|
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
154
diff
changeset
|
132 |
$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
|
133 |
|
154 | 134 |
$progressBar = $this->output->createProgressBar($handles->total()); |
135 |
$progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%'); |
|
136 |
||
137 |
while(!is_null($handles)) { |
|
138 |
foreach($handles as $handle) { |
|
139 |
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
|
140 |
$this->handleClient->deleteHandle($handle); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
141 |
} |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
142 |
$progressBar->setMessage($handle); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
143 |
$progressBar->advance(); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
144 |
} |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
154
diff
changeset
|
145 |
$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
|
146 |
} |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
147 |
|
154 | 148 |
$this->info("\nRemoving extra handles completed"); |
153
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 |
} |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
151 |
} |