author | ymh <ymh.work@gmail.com> |
Wed, 09 Nov 2016 15:05:41 +0100 | |
changeset 406 | cf0f23803a53 |
parent 326 | 226d5b17a119 |
child 552 | 2c579ea45608 |
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; |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
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} |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
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) { |
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
326
diff
changeset
|
50 |
$this->handleClient->createHandleUrlRecord($doc->getId(), config('app.url')."/docs/".$doc->getId()); |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
51 |
} |
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 |
* Execute the console command. |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
* |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
* @return mixed |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
*/ |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
public function handle() |
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 |
$stepSize = $this->option('step-size'); |
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 |
$passwordKey = $this->option('password'); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
if($this->option('ask-password')) { |
154 | 64 |
$passwordKey = $this->secret('Private key password?'); |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
} |
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 |
$privateKey = $this->option('key'); |
154 | 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 |
} |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
|
154 | 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 |
} |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
|
154 | 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 |
} |
|
153
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 |
// create handle client |
154 | 106 |
$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
|
107 |
|
154 | 108 |
$this->info("Adding documents handles..."); |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
109 |
|
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
110 |
$total = $this->documentRepository->getCount(); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
111 |
|
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
154
diff
changeset
|
112 |
$docs = $this->documentRepository->paginate(null, $stepSize, config('corpusparole.pagination_page_param'), 1, '_graph'); |
154 | 113 |
|
114 |
$progressBar = $this->output->createProgressBar($docs->total()); |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
115 |
$progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%'); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
116 |
|
154 | 117 |
while(!is_null($docs)) { |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
118 |
foreach ($docs as $doc){ |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
119 |
$this->registerHandle($doc); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
120 |
$progressBar->setMessage($doc->getId()); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
121 |
$progressBar->advance(); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
122 |
} |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
154
diff
changeset
|
123 |
$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
|
124 |
} |
154 | 125 |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
126 |
$progressBar->finish(); |
154 | 127 |
$this->info("\nAdding handles completed\n"); |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
128 |
|
154 | 129 |
$this->info("Removing extra handles...\n"); |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
130 |
|
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
154
diff
changeset
|
131 |
$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
|
132 |
|
154 | 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))) { |
|
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
139 |
$this->handleClient->deleteHandle($handle); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
140 |
} |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
141 |
$progressBar->setMessage($handle); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
142 |
$progressBar->advance(); |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
143 |
} |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
154
diff
changeset
|
144 |
$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
|
145 |
} |
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
146 |
|
154 | 147 |
$this->info("\nRemoving extra handles completed"); |
153
338bcc78d431
add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
148 |
|
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 |
} |