|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2019-2023 |
|
6
|
|
|
* @package Controller |
|
7
|
|
|
* @subpackage Jobs |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Jobs\Group\Import\Xml; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Job controller for XML group imports |
|
16
|
|
|
* |
|
17
|
|
|
* @package Controller |
|
18
|
|
|
* @subpackage Jobs |
|
19
|
|
|
*/ |
|
20
|
|
|
class Standard |
|
21
|
|
|
extends \Aimeos\Controller\Jobs\Base |
|
22
|
|
|
implements \Aimeos\Controller\Jobs\Iface |
|
23
|
|
|
{ |
|
24
|
|
|
/** controller/jobs/group/import/xml/name |
|
25
|
|
|
* Class name of the used group suggestions scheduler controller implementation |
|
26
|
|
|
* |
|
27
|
|
|
* Each default job controller can be replace by an alternative imlementation. |
|
28
|
|
|
* To use this implementation, you have to set the last part of the class |
|
29
|
|
|
* name as configuration value so the controller factory knows which class it |
|
30
|
|
|
* has to instantiate. |
|
31
|
|
|
* |
|
32
|
|
|
* For example, if the name of the default class is |
|
33
|
|
|
* |
|
34
|
|
|
* \Aimeos\Controller\Jobs\Group\Import\Xml\Standard |
|
35
|
|
|
* |
|
36
|
|
|
* and you want to replace it with your own version named |
|
37
|
|
|
* |
|
38
|
|
|
* \Aimeos\Controller\Jobs\Group\Import\Xml\Myxml |
|
39
|
|
|
* |
|
40
|
|
|
* then you have to set the this configuration option: |
|
41
|
|
|
* |
|
42
|
|
|
* controller/jobs/group/import/xml/name = Myxml |
|
43
|
|
|
* |
|
44
|
|
|
* The value is the last part of your own class name and it's case sensitive, |
|
45
|
|
|
* so take care that the configuration value is exactly named like the last |
|
46
|
|
|
* part of the class name. |
|
47
|
|
|
* |
|
48
|
|
|
* The allowed characters of the class name are A-Z, a-z and 0-9. No other |
|
49
|
|
|
* characters are possible! You should always start the last part of the class |
|
50
|
|
|
* name with an upper case character and continue only with lower case characters |
|
51
|
|
|
* or numbers. Avoid chamel case names like "MyXml"! |
|
52
|
|
|
* |
|
53
|
|
|
* @param string Last part of the class name |
|
54
|
|
|
* @since 2019.04 |
|
55
|
|
|
*/ |
|
56
|
|
|
|
|
57
|
|
|
/** controller/jobs/group/import/xml/decorators/excludes |
|
58
|
|
|
* Excludes decorators added by the "common" option from the group import CSV job controller |
|
59
|
|
|
* |
|
60
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
61
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
62
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
63
|
|
|
* modify what is returned to the caller. |
|
64
|
|
|
* |
|
65
|
|
|
* This option allows you to remove a decorator added via |
|
66
|
|
|
* "controller/jobs/common/decorators/default" before they are wrapped |
|
67
|
|
|
* around the job controller. |
|
68
|
|
|
* |
|
69
|
|
|
* controller/jobs/group/import/xml/decorators/excludes = array( 'decorator1' ) |
|
70
|
|
|
* |
|
71
|
|
|
* This would remove the decorator named "decorator1" from the list of |
|
72
|
|
|
* common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via |
|
73
|
|
|
* "controller/jobs/common/decorators/default" to the job controller. |
|
74
|
|
|
* |
|
75
|
|
|
* @param array List of decorator names |
|
76
|
|
|
* @since 2019.04 |
|
77
|
|
|
* @see controller/jobs/common/decorators/default |
|
78
|
|
|
* @see controller/jobs/group/import/xml/decorators/global |
|
79
|
|
|
* @see controller/jobs/group/import/xml/decorators/local |
|
80
|
|
|
*/ |
|
81
|
|
|
|
|
82
|
|
|
/** controller/jobs/group/import/xml/decorators/global |
|
83
|
|
|
* Adds a list of globally available decorators only to the group import CSV job controller |
|
84
|
|
|
* |
|
85
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
86
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
87
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
88
|
|
|
* modify what is returned to the caller. |
|
89
|
|
|
* |
|
90
|
|
|
* This option allows you to wrap global decorators |
|
91
|
|
|
* ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller. |
|
92
|
|
|
* |
|
93
|
|
|
* controller/jobs/group/import/xml/decorators/global = array( 'decorator1' ) |
|
94
|
|
|
* |
|
95
|
|
|
* This would add the decorator named "decorator1" defined by |
|
96
|
|
|
* "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array List of decorator names |
|
99
|
|
|
* @since 2019.04 |
|
100
|
|
|
* @see controller/jobs/common/decorators/default |
|
101
|
|
|
* @see controller/jobs/group/import/xml/decorators/excludes |
|
102
|
|
|
* @see controller/jobs/group/import/xml/decorators/local |
|
103
|
|
|
*/ |
|
104
|
|
|
|
|
105
|
|
|
/** controller/jobs/group/import/xml/decorators/local |
|
106
|
|
|
* Adds a list of local decorators only to the group import CSV job controller |
|
107
|
|
|
* |
|
108
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
109
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
110
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
111
|
|
|
* modify what is returned to the caller. |
|
112
|
|
|
* |
|
113
|
|
|
* This option allows you to wrap local decorators |
|
114
|
|
|
* ("\Aimeos\Controller\Jobs\Group\Import\Xml\Decorator\*") around the job |
|
115
|
|
|
* controller. |
|
116
|
|
|
* |
|
117
|
|
|
* controller/jobs/group/import/xml/decorators/local = array( 'decorator2' ) |
|
118
|
|
|
* |
|
119
|
|
|
* This would add the decorator named "decorator2" defined by |
|
120
|
|
|
* "\Aimeos\Controller\Jobs\Group\Import\Xml\Decorator\Decorator2" |
|
121
|
|
|
* only to the job controller. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array List of decorator names |
|
124
|
|
|
* @since 2019.04 |
|
125
|
|
|
* @see controller/jobs/common/decorators/default |
|
126
|
|
|
* @see controller/jobs/group/import/xml/decorators/excludes |
|
127
|
|
|
* @see controller/jobs/group/import/xml/decorators/global |
|
128
|
|
|
*/ |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
use \Aimeos\Controller\Common\Common\Import\Xml\Traits; |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns the localized name of the job. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string Name of the job |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getName() : string |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->context()->translate( 'controller/jobs', 'Groups import XML' ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns the localized description of the job. |
|
147
|
|
|
* |
|
148
|
|
|
* @return string Description of the job |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getDescription() : string |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->context()->translate( 'controller/jobs', 'Imports new and updates existing groups from XML files' ); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Executes the job. |
|
158
|
|
|
* |
|
159
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
|
160
|
|
|
*/ |
|
161
|
|
|
public function run() |
|
162
|
|
|
{ |
|
163
|
|
|
$context = $this->context(); |
|
164
|
|
|
$logger = $context->logger(); |
|
165
|
|
|
$process = $context->process(); |
|
166
|
|
|
|
|
167
|
|
|
$location = $this->location(); |
|
168
|
|
|
$fs = $context->fs( 'fs-import' ); |
|
169
|
|
|
|
|
170
|
|
|
if( $fs->isDir( $location ) === false ) { |
|
171
|
|
|
return; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
try |
|
175
|
|
|
{ |
|
176
|
|
|
$logger->info( sprintf( 'Started group import from "%1$s"', $location ), 'import/xml/group' ); |
|
177
|
|
|
|
|
178
|
|
|
$fcn = function( \Aimeos\MShop\ContextIface $context, string $path ) { |
|
179
|
|
|
$this->import( $context, $path ); |
|
180
|
|
|
}; |
|
181
|
|
|
|
|
182
|
|
|
foreach( map( $fs->scan( $location ) )->sort() as $filename ) |
|
183
|
|
|
{ |
|
184
|
|
|
$path = $location . '/' . $filename; |
|
185
|
|
|
|
|
186
|
|
|
if( $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $path ) ) { |
|
187
|
|
|
continue; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$process->start( $fcn, [$context, $path] ); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$process->wait(); |
|
194
|
|
|
|
|
195
|
|
|
$logger->info( sprintf( 'Finished group import from "%1$s"', $location ), 'import/xml/group' ); |
|
196
|
|
|
} |
|
197
|
|
|
catch( \Exception $e ) |
|
198
|
|
|
{ |
|
199
|
|
|
$logger->error( 'Customer group import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/xml/group' ); |
|
200
|
|
|
$this->mail( 'Customer group XML import error', $e->getMessage() . "\n" . $e->getTraceAsString() ); |
|
201
|
|
|
throw $e; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Returns the directory for storing imported files |
|
208
|
|
|
* |
|
209
|
|
|
* @return string Directory for storing imported files |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function backup() : string |
|
212
|
|
|
{ |
|
213
|
|
|
/** controller/jobs/group/import/xml/backup |
|
214
|
|
|
* Name of the backup for sucessfully imported files |
|
215
|
|
|
* |
|
216
|
|
|
* After a XML file was imported successfully, you can move it to another |
|
217
|
|
|
* location, so it won't be imported again and isn't overwritten by the |
|
218
|
|
|
* next file that is stored at the same location in the file system. |
|
219
|
|
|
* |
|
220
|
|
|
* You should use an absolute path to be sure but can be relative path |
|
221
|
|
|
* if you absolutely know from where the job will be executed from. The |
|
222
|
|
|
* name of the new backup location can contain placeholders understood |
|
223
|
|
|
* by the PHP DateTime::format() method (with percent signs prefix) to |
|
224
|
|
|
* create dynamic paths, e.g. "backup/%Y-%m-%d" which would create |
|
225
|
|
|
* "backup/2000-01-01". For more information about the date() placeholders, |
|
226
|
|
|
* please have a look into the PHP documentation of the |
|
227
|
|
|
* {@link https://www.php.net/manual/en/datetime.format.php format() method}. |
|
228
|
|
|
* |
|
229
|
|
|
* **Note:** If no backup name is configured, the file will be removed! |
|
230
|
|
|
* |
|
231
|
|
|
* @param integer Name of the backup file, optionally with date/time placeholders |
|
232
|
|
|
* @since 2019.04 |
|
233
|
|
|
* @see controller/jobs/group/import/xml/domains |
|
234
|
|
|
* @see controller/jobs/group/import/xml/location |
|
235
|
|
|
* @see controller/jobs/group/import/xml/max-query |
|
236
|
|
|
*/ |
|
237
|
|
|
$backup = $this->context()->config()->get( 'controller/jobs/group/import/xml/backup' ); |
|
238
|
|
|
return \Aimeos\Base\Str::strtime( (string) $backup ); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Imports the XML file given by its path |
|
244
|
|
|
* |
|
245
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
|
246
|
|
|
* @param string $path Relative path to the XML file in the file system |
|
247
|
|
|
*/ |
|
248
|
|
|
protected function import( \Aimeos\MShop\ContextIface $context, string $path ) |
|
249
|
|
|
{ |
|
250
|
|
|
$slice = 0; |
|
251
|
|
|
$nodes = []; |
|
252
|
|
|
|
|
253
|
|
|
$xml = new \XMLReader(); |
|
254
|
|
|
$maxquery = $this->max(); |
|
255
|
|
|
|
|
256
|
|
|
$logger = $context->logger(); |
|
257
|
|
|
$fs = $context->fs( 'fs-import' ); |
|
258
|
|
|
$tmpfile = $fs->readf( $path ); |
|
259
|
|
|
|
|
260
|
|
|
if( $xml->open( $tmpfile, LIBXML_COMPACT | LIBXML_PARSEHUGE ) === false ) { |
|
261
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No XML file "%1$s" found', $tmpfile ) ); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$logger->info( sprintf( 'Started group import from file "%1$s"', $path ), 'import/xml/group' ); |
|
265
|
|
|
|
|
266
|
|
|
while( $xml->read() === true ) |
|
267
|
|
|
{ |
|
268
|
|
|
if( $xml->depth === 1 && $xml->nodeType === \XMLReader::ELEMENT && $xml->name === 'groupitem' ) |
|
269
|
|
|
{ |
|
270
|
|
|
if( ( $dom = $xml->expand() ) === false ) |
|
271
|
|
|
{ |
|
272
|
|
|
$msg = sprintf( 'Expanding "%1$s" node failed', 'groupitem' ); |
|
273
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$nodes[] = $dom; |
|
277
|
|
|
|
|
278
|
|
|
if( $slice++ >= $maxquery ) |
|
279
|
|
|
{ |
|
280
|
|
|
$this->importNodes( $nodes ); |
|
281
|
|
|
unset( $nodes ); |
|
282
|
|
|
$nodes = []; |
|
283
|
|
|
$slice = 0; |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
$this->importNodes( $nodes ); |
|
289
|
|
|
unset( $nodes ); |
|
290
|
|
|
|
|
291
|
|
|
foreach( $this->getProcessors() as $proc ) { |
|
292
|
|
|
$proc->finish(); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
unlink( $tmpfile ); |
|
296
|
|
|
|
|
297
|
|
|
if( !empty( $backup = $this->backup() ) ) { |
|
298
|
|
|
$fs->move( $path, $backup ); |
|
299
|
|
|
} else { |
|
300
|
|
|
$fs->rm( $path ); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
$logger->info( sprintf( 'Finished group import from file "%1$s"', $path ), 'import/xml/group' ); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Imports the given DOM nodes |
|
309
|
|
|
* |
|
310
|
|
|
* @param string[] $ref List of domain names whose referenced items will be updated in the group items |
|
311
|
|
|
*/ |
|
312
|
|
|
protected function importNodes( array $nodes ) |
|
313
|
|
|
{ |
|
314
|
|
|
$codes = $map = []; |
|
315
|
|
|
|
|
316
|
|
|
foreach( $nodes as $node ) |
|
317
|
|
|
{ |
|
318
|
|
|
if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) { |
|
319
|
|
|
$codes[$attr->nodeValue] = null; |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'group' ); |
|
324
|
|
|
$search = $manager->filter()->slice( 0, count( $codes ) ); |
|
325
|
|
|
$search->setConditions( $search->compare( '==', 'group.code', array_keys( $codes ) ) ); |
|
326
|
|
|
|
|
327
|
|
|
foreach( $manager->search( $search ) as $item ) { |
|
328
|
|
|
$map[$item->getCode()] = $item; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
foreach( $nodes as $node ) |
|
332
|
|
|
{ |
|
333
|
|
|
if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null && isset( $map[$attr->nodeValue] ) ) { |
|
334
|
|
|
$item = $this->process( $map[$attr->nodeValue], $node ); |
|
335
|
|
|
} else { |
|
336
|
|
|
$item = $this->process( $manager->create(), $node ); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
$manager->save( $item ); |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* Returns the path to the directory with the XML file |
|
346
|
|
|
* |
|
347
|
|
|
* @return string Path to the directory with the XML file |
|
348
|
|
|
*/ |
|
349
|
|
|
protected function location() : string |
|
350
|
|
|
{ |
|
351
|
|
|
/** controller/jobs/group/import/xml/location |
|
352
|
|
|
* Directory where the CSV files are stored which should be imported |
|
353
|
|
|
* |
|
354
|
|
|
* It's the relative path inside the "fs-import" virtual file system |
|
355
|
|
|
* configuration. The default location of the "fs-import" file system is: |
|
356
|
|
|
* |
|
357
|
|
|
* * Laravel: ./storage/import/ |
|
358
|
|
|
* * TYPO3: /uploads/tx_aimeos/.secure/import/ |
|
359
|
|
|
* |
|
360
|
|
|
* @param string Relative path to the XML files |
|
361
|
|
|
* @since 2019.04 |
|
362
|
|
|
* @see controller/jobs/group/import/xml/backup |
|
363
|
|
|
* @see controller/jobs/group/import/xml/domains |
|
364
|
|
|
* @see controller/jobs/group/import/xml/max-query |
|
365
|
|
|
*/ |
|
366
|
|
|
return (string) $this->context()->config()->get( 'controller/jobs/group/import/xml/location', 'group' ); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* Returns the maximum number of XML nodes processed at once |
|
372
|
|
|
* |
|
373
|
|
|
* @return int Maximum number of XML nodes |
|
374
|
|
|
*/ |
|
375
|
|
|
protected function max() : int |
|
376
|
|
|
{ |
|
377
|
|
|
/** controller/jobs/group/import/xml/max-query |
|
378
|
|
|
* Maximum number of XML nodes processed at once |
|
379
|
|
|
* |
|
380
|
|
|
* Processing and fetching several attribute items at once speeds up importing |
|
381
|
|
|
* the XML files. The more items can be processed at once, the faster the |
|
382
|
|
|
* import. More items also increases the memory usage of the importer and |
|
383
|
|
|
* thus, this parameter should be low enough to avoid reaching the memory |
|
384
|
|
|
* limit of the PHP process. |
|
385
|
|
|
* |
|
386
|
|
|
* @param integer Number of XML nodes |
|
387
|
|
|
* @since 2019.04 |
|
388
|
|
|
* @see controller/jobs/group/import/xml/domains |
|
389
|
|
|
* @see controller/jobs/group/import/xml/location |
|
390
|
|
|
* @see controller/jobs/group/import/xml/backup |
|
391
|
|
|
*/ |
|
392
|
|
|
return $this->context()->config()->get( 'controller/jobs/group/import/xml/max-query', 100 ); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Updates the group item and its referenced items using the given DOM node |
|
398
|
|
|
* |
|
399
|
|
|
* @param \Aimeos\MShop\Group\Item\Iface $item Customer group item object to update |
|
400
|
|
|
* @param \DomElement $node DOM node used for updating the group item |
|
401
|
|
|
* @return \Aimeos\MShop\Group\Item\Iface $item Updated group item object |
|
402
|
|
|
*/ |
|
403
|
|
|
protected function process( \Aimeos\MShop\Group\Item\Iface $item, \DomElement $node ) : \Aimeos\MShop\Group\Item\Iface |
|
404
|
|
|
{ |
|
405
|
|
|
$list = []; |
|
406
|
|
|
|
|
407
|
|
|
foreach( $node->attributes as $attr ) { |
|
408
|
|
|
$list[$attr->nodeName] = $attr->nodeValue; |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
foreach( $node->childNodes as $tag ) { |
|
412
|
|
|
$list[$tag->nodeName] = $tag->nodeValue; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
return $item->fromArray( $list, true ); |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
|