-
Notifications
You must be signed in to change notification settings - Fork 171
/
ImportService.php
248 lines (218 loc) · 6.94 KB
/
ImportService.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
<?php
namespace Drupal\datastore\Service;
use CsvParser\Parser\Csv;
use Drupal\common\DataResource;
use Drupal\common\EventDispatcherTrait;
use Drupal\datastore\Events\DatastoreImportedEvent;
use Drupal\datastore\Plugin\QueueWorker\ImportJob;
use Drupal\datastore\Storage\DatabaseTable;
use Drupal\datastore\Storage\DatabaseTableFactory;
use Drupal\datastore\Storage\ImportJobStoreFactory;
use Procrastinator\Result;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Datastore importer.
*
* @todo This class has state and is not actually a service because it holds
* state. Have import() take an argument of a resource, instead of storing it
* as a property.
*/
class ImportService {
use EventDispatcherTrait;
/**
* Event name used when configuring the parser during import.
*
* @var string
*/
public const EVENT_CONFIGURE_PARSER = 'dkan_datastore_import_configure_parser';
/**
* Event name for when the datastore has been successfully imported.
*/
public const EVENT_DATASTORE_IMPORTED = 'dkan_datastore_imported';
/**
* Time-limit used for standard import service.
*
* @var int
*/
protected const DEFAULT_TIMELIMIT = 50;
/**
* The qualified class name of the importer to use.
*
* @var \Procrastinator\Job\AbstractPersistentJob
*/
private $importerClass = ImportJob::class;
/**
* The DKAN Resource to import.
*
* @var \Drupal\common\DataResource|null
*/
private ?DataResource $resource;
/**
* The jobstore factory service.
*
* @var \Drupal\datastore\Storage\ImportJobStoreFactory
*/
private ImportJobStoreFactory $importJobStoreFactory;
/**
* Database table factory service.
*
* @var \Drupal\datastore\Storage\DatabaseTableFactory
*/
private DatabaseTableFactory $databaseTableFactory;
/**
* Import job for the current import.
*
* Access using self::getImporter().
*
* @var \Drupal\datastore\Plugin\QueueWorker\ImportJob|null
*
* @see self::getImporter()
*/
private ?ImportJob $importJob = NULL;
/**
* Logger channel service.
*
* @var \Psr\Log\LoggerInterface
*/
private LoggerInterface $logger;
/**
* Event dispatcher service.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
private EventDispatcherInterface $eventDispatcher;
/**
* Create a resource service instance.
*
* @param \Drupal\common\DataResource $resource
* DKAN Resource.
* @param \Drupal\datastore\Storage\ImportJobStoreFactory $importJobStoreFactory
* Import jobstore factory.
* @param \Drupal\datastore\Storage\DatabaseTableFactory $databaseTableFactory
* Database Table factory.
* @param \Psr\Log\LoggerInterface $loggerChannel
* DKAN logger channel service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* Event dispatcher service.
*/
public function __construct(
DataResource $resource,
ImportJobStoreFactory $importJobStoreFactory,
DatabaseTableFactory $databaseTableFactory,
LoggerInterface $loggerChannel,
EventDispatcherInterface $eventDispatcher,
) {
$this->resource = $resource;
$this->importJobStoreFactory = $importJobStoreFactory;
$this->databaseTableFactory = $databaseTableFactory;
$this->logger = $loggerChannel;
$this->eventDispatcher = $eventDispatcher;
}
/**
* Setter.
*/
public function setImporterClass($className) {
$this->importerClass = $className;
}
/**
* Get DKAN resource.
*
* @return \Drupal\common\DataResource
* DKAN Resource.
*/
protected function getResource(): DataResource {
return $this->resource;
}
/**
* Import.
*/
public function import() {
$result = $this->getImporter()->run();
$data_resource = $this->getResource();
if ($result->getStatus() === Result::ERROR) {
$datastore_resource = $data_resource->getDatastoreResource();
$this->logger->error('Error importing resource id:%id path:%path message:%message', [
'%id' => $datastore_resource->getId(),
'%path' => $datastore_resource->getFilePath(),
'%message' => $result->getError(),
]);
}
// If the import job finished successfully...
elseif ($result->getStatus() === Result::DONE) {
// Dispatch the import event.
$this->eventDispatcher->dispatch(
new DatastoreImportedEvent($data_resource),
self::EVENT_DATASTORE_IMPORTED
);
// Queue the imported resource for post-import processing.
$post_import_queue = \Drupal::service('queue')->get('post_import');
$post_import_queue->createItem($data_resource);
}
}
/**
* Build an Importer.
*
* @return \Drupal\datastore\Plugin\QueueWorker\ImportJob
* Importer.
*
* @throws \Exception
* Throws exception if we cannot create a valid importer object.
*/
public function getImporter(): ImportJob {
if ($this->importJob ?? FALSE) {
return $this->importJob;
}
$datastore_resource = $this->getResource()->getDatastoreResource();
$delimiter = ",";
if ($datastore_resource->getMimeType() == 'text/tab-separated-values') {
$delimiter = "\t";
}
$this->importJob = call_user_func([$this->importerClass, 'get'],
$datastore_resource->getId(),
$this->importJobStoreFactory->getInstance(),
[
"storage" => $this->getStorage(),
"parser" => $this->getNonRecordingParser($delimiter),
"resource" => $datastore_resource,
]
);
$this->importJob->setTimeLimit(self::DEFAULT_TIMELIMIT);
return $this->importJob;
}
/**
* Create a non-recording parser.
*
* When processing chunk size was increased to boost performance, the state
* machine's default behavior to record every execution steps caused out of
* memory errors. Stopping the machine's recording addresses this.
*
* @param string $delimiter
* Delimiter character.
*
* @return \CsvParser\Parser\Csv
* A parser which does not keep track of every execution steps.
*/
private function getNonRecordingParser(string $delimiter) : Csv {
$parserConfiguration = [
'delimiter' => $delimiter,
'quote' => '"',
'escape' => "\\",
'record_end' => ["\n", "\r"],
];
$parserConfiguration = $this->dispatchEvent(self::EVENT_CONFIGURE_PARSER, $parserConfiguration);
$parser = Csv::getParser($parserConfiguration['delimiter'], $parserConfiguration['quote'], $parserConfiguration['escape'], $parserConfiguration['record_end']);
$parser->machine->stopRecording();
return $parser;
}
/**
* Build a database table storage object.
*
* @return \Drupal\datastore\Storage\DatabaseTable
* DatabaseTable storage object.
*/
public function getStorage(): DatabaseTable {
$datastore_resource = $this->getResource()->getDatastoreResource();
return $this->databaseTableFactory->getInstance($datastore_resource->getId(), ['resource' => $datastore_resource]);
}
}