-
Notifications
You must be signed in to change notification settings - Fork 8
/
HarvesterFactory.php
329 lines (308 loc) · 10.4 KB
/
HarvesterFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* Factory for OAI-PMH Harvest Tool
*
* PHP version 7
*
* Copyright (c) Demian Katz 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Harvest_Tools
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/indexing:oai-pmh Wiki
*/
namespace VuFindHarvest\OaiPmh;
use Laminas\Http\Client;
use Symfony\Component\Console\Output\OutputInterface;
use VuFindHarvest\ConsoleOutput\ConsoleWriter;
use VuFindHarvest\RecordWriterStrategy\RecordWriterStrategyFactory;
use VuFindHarvest\RecordWriterStrategy\RecordWriterStrategyInterface;
use VuFindHarvest\ResponseProcessor\ResponseProcessorInterface;
use VuFindHarvest\ResponseProcessor\SimpleXmlResponseProcessor;
/**
* Factory for OAI-PMH Harvest Tool
*
* @category VuFind
* @package Harvest_Tools
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/indexing:oai-pmh Wiki
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class HarvesterFactory
{
/**
* Add SSL options to $options if standard files can be autodetected.
*
* @param array $options Options to modify.
*
* @return void
*/
protected function addAutoSslOptions(& $options)
{
// RedHat/CentOS:
if (file_exists('/etc/pki/tls/cert.pem')) {
$options['sslcafile'] = '/etc/pki/tls/cert.pem';
}
// Debian/Ubuntu:
if (file_exists('/etc/ssl/certs')) {
$options['sslcapath'] = '/etc/ssl/certs';
}
}
/**
* Get HTTP client options from $settings array
*
* @param array $settings Settings
*
* @return array
*/
protected function getClientOptions(array $settings)
{
$options = [
'timeout' => $settings['timeout'] ?? 60,
];
if (isset($settings['autosslca']) && $settings['autosslca']) {
$this->addAutoSslOptions($options);
}
foreach (['sslcafile', 'sslcapath'] as $sslSetting) {
if (isset($settings[$sslSetting])) {
$options[$sslSetting] = $settings[$sslSetting];
}
}
if (isset($settings['sslverifypeer']) && !$settings['sslverifypeer']) {
$options['sslverifypeer'] = false;
}
return $options;
}
/**
* Configure the HTTP client
*
* @param Client $client HTTP client
* @param array $settings Settings
*
* @return Client
*
* @throws Exception
*/
protected function configureClient(Client $client, array $settings)
{
$configuredClient = $client ?: new Client();
// Set authentication, if necessary:
if (!empty($settings['httpUser']) && !empty($settings['httpPass'])) {
$configuredClient->setAuth($settings['httpUser'], $settings['httpPass']);
}
// Set up assorted client options from $settings array:
$configuredClient->setOptions($this->getClientOptions($settings));
return $configuredClient;
}
/**
* Set up directory structure for harvesting.
*
* @param string $harvestRoot Root directory containing harvested data.
* @param string $target The OAI-PMH target directory to create inside
* $harvestRoot.
*
* @return string
*/
protected function getBasePath($harvestRoot, $target)
{
// Build the full harvest path:
$basePath = rtrim($harvestRoot, '/') . '/' . rtrim($target, '/') . '/';
// Create the directory if it does not already exist:
if (!is_dir($basePath)) {
if (!mkdir($basePath)) {
throw new \Exception("Problem creating directory {$basePath}.");
}
}
return $basePath;
}
/**
* Get the communicator.
*
* @param Client $client HTTP client
* @param array $settings Additional settings
* @param ResponseProcessorInterface $processor Response processor
* @param string $target Target being configured (used for
* error messages)
* @param OutputInterface $output Output interface
*
* @return Communicator
*/
protected function getCommunicator(
Client $client,
array $settings,
ResponseProcessorInterface $processor,
$target,
OutputInterface $output = null
) {
if (empty($settings['url'])) {
throw new \Exception("Missing base URL for {$target}.");
}
$comm = new Communicator($settings['url'], $client, $processor);
// We only want the communicator to output messages if we are in verbose
// mode; communicator messages are considered verbose output.
if (($settings['verbose'] ?? false)
&& $writer = $this->getConsoleWriter($output, $settings)
) {
$comm->setOutputWriter($writer);
}
return $comm;
}
/**
* Get the record XML formatter.
*
* @param Communicator $communicator Communicator
* @param array $settings Additional settings
* @param OutputInterface $output Output interface
*
* @return RecordXmlFormatter
*/
protected function getFormatter(
Communicator $communicator,
array $settings,
OutputInterface $output = null
) {
// Build the formatter:
$formatter = new RecordXmlFormatter($settings);
// Load set names if we're going to need them:
if ($formatter->needsSetNames()) {
$loader = $this->getSetLoader($communicator, $settings);
if ($writer = $this->getConsoleWriter($output, $settings)) {
$loader->setOutputWriter($writer);
}
$formatter->setSetNames($loader->getNames());
}
return $formatter;
}
/**
* Get console output writer (if applicable).
*
* @param OutputInterface $output Output interface
* @param array $settings OAI-PMH settings
*
* @return ConsoleWriter
*/
protected function getConsoleWriter(?OutputInterface $output, $settings)
{
// Don't create a writer if we're in silent mode or have no
// available output interface.
return (($settings['silent'] ?? false) || $output === null)
? null : new ConsoleWriter($output);
}
/**
* Get XML response processor.
*
* @param string $basePath Base path for harvest
* @param array $settings OAI-PMH settings
*
* @return SimpleXmlResponseProcessor
*/
protected function getResponseProcessor($basePath, array $settings)
{
return new SimpleXmlResponseProcessor($basePath, $settings);
}
/**
* Get the set loader (used to load set names).
*
* @param Communicator $communicator API communicator
* @param array $settings OAI-PMH settings
*
* @return SetLoader
*/
protected function getSetLoader(Communicator $communicator, array $settings)
{
return new SetLoader($communicator, $settings);
}
/**
* Get state manager
*
* @param string $basePath Base path for harvest
*
* @return StateManager
*/
protected function getStateManager($basePath)
{
return new StateManager($basePath);
}
/**
* Build the writer support object.
*
* @param RecordWriterStrategyInterface $strategy Writing strategy
* @param RecordXmlFormatter $formatter XML record formatter
* @param array $settings Configuration settings
*
* @return RecordWriter
*/
protected function getWriter(
RecordWriterStrategyInterface $strategy,
RecordXmlFormatter $formatter,
array $settings
) {
return new RecordWriter($strategy, $formatter, $settings);
}
/**
* Get the factory for record writer strategies.
*
* @return RecordWriterStrategyFactory
*/
protected function getWriterStrategyFactory()
{
return new RecordWriterStrategyFactory();
}
/**
* Get the harvester
*
* @param string $target Name of source being harvested (used as
* directory name for storing harvested data inside $harvestRoot)
* @param string $harvestRoot Root directory containing harvested data.
* @param Client $client HTTP client
* @param array $settings Additional settings
* @param OutputInterface $output Output interface (optional)
*
* @return Harvester
*
* @throws \Exception
*/
public function getHarvester(
$target,
$harvestRoot,
Client $client = null,
array $settings = [],
OutputInterface $output = null
) {
$basePath = $this->getBasePath($harvestRoot, $target);
$responseProcessor = $this->getResponseProcessor($basePath, $settings);
$communicator = $this->getCommunicator(
$this->configureClient($client, $settings),
$settings,
$responseProcessor,
$target,
$output
);
$formatter = $this->getFormatter($communicator, $settings, $output);
$strategy = $this->getWriterStrategyFactory()
->getStrategy($basePath, $settings);
$writer = $this->getWriter($strategy, $formatter, $settings);
$stateManager = $this->getStateManager($basePath);
$harvester = new Harvester($communicator, $writer, $stateManager, $settings);
if ($writer = $this->getConsoleWriter($output, $settings)) {
$harvester->setOutputWriter($writer);
}
return $harvester;
}
}