1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2019 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Jobs |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Jobs\Customer\Import\Xml; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Job controller for XML customer 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
|
|
|
use \Aimeos\Controller\Common\Common\Import\Xml\Traits; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns the localized name of the job. |
29
|
|
|
* |
30
|
|
|
* @return string Name of the job |
31
|
|
|
*/ |
32
|
|
|
public function getName() |
33
|
|
|
{ |
34
|
|
|
return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Customer import XML' ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the localized description of the job. |
40
|
|
|
* |
41
|
|
|
* @return string Description of the job |
42
|
|
|
*/ |
43
|
|
|
public function getDescription() |
44
|
|
|
{ |
45
|
|
|
return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Imports new and updates existing customers from XML files' ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Executes the job. |
51
|
|
|
* |
52
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
53
|
|
|
*/ |
54
|
|
|
public function run() |
55
|
|
|
{ |
56
|
|
|
$context = $this->getContext(); |
57
|
|
|
$config = $context->getConfig(); |
58
|
|
|
$logger = $context->getLogger(); |
59
|
|
|
|
60
|
|
|
/** controller/jobs/customer/import/xml/location |
61
|
|
|
* File or directory where the content is stored which should be imported |
62
|
|
|
* |
63
|
|
|
* You need to configure the XML file or directory with the XML files that |
64
|
|
|
* should be imported. It should be an absolute path to be sure but can be |
65
|
|
|
* relative path if you absolutely know from where the job will be executed |
66
|
|
|
* from. |
67
|
|
|
* |
68
|
|
|
* @param string Absolute file or directory path |
69
|
|
|
* @since 2019.04 |
70
|
|
|
* @category Developer |
71
|
|
|
* @category User |
72
|
|
|
* @see controller/jobs/customer/import/xml/container/type |
73
|
|
|
* @see controller/jobs/customer/import/xml/container/content |
74
|
|
|
* @see controller/jobs/customer/import/xml/container/options |
75
|
|
|
*/ |
76
|
|
|
$location = $config->get( 'controller/jobs/customer/import/xml/location' ); |
77
|
|
|
|
78
|
|
|
try |
79
|
|
|
{ |
80
|
|
|
$msg = sprintf( 'Started customer import from "%1$s" (%2$s)', $location, __CLASS__ ); |
81
|
|
|
$logger->log( $msg, \Aimeos\MW\Logger\Base::INFO ); |
82
|
|
|
|
83
|
|
|
if( !file_exists( $location ) ) |
84
|
|
|
{ |
85
|
|
|
$msg = sprintf( 'File or directory "%1$s" doesn\'t exist', $location ); |
86
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$files = []; |
90
|
|
|
|
91
|
|
|
if( is_dir( $location ) ) |
92
|
|
|
{ |
93
|
|
|
foreach( new \DirectoryIterator( $location ) as $entry ) |
94
|
|
|
{ |
95
|
|
|
if( strncmp( $entry->getFilename(), 'customer', 8 ) === 0 && $entry->getExtension() === 'xml' ) { |
96
|
|
|
$files[] = $entry->getPathname(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
else |
101
|
|
|
{ |
102
|
|
|
$files[] = $location; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
sort( $files ); |
106
|
|
|
$total = 0; |
107
|
|
|
|
108
|
|
|
foreach( $files as $filepath ) { |
109
|
|
|
$total += $this->import( $filepath ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$msg = 'Finished customer import from "%1$s": %2$s total (%3$s MB)'; |
113
|
|
|
$mem = number_format( memory_get_peak_usage() / 1024 / 1024, 2 ); |
114
|
|
|
|
115
|
|
|
$logger->log( sprintf( $msg, $location, $total, $mem ), \Aimeos\MW\Logger\Base::INFO ); |
116
|
|
|
} |
117
|
|
|
catch( \Exception $e ) |
118
|
|
|
{ |
119
|
|
|
$logger->log( 'Customer import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString() ); |
120
|
|
|
throw $e; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Imports the XML file given by its path |
127
|
|
|
* |
128
|
|
|
* @param $filename Absolute or relative path to the XML file |
|
|
|
|
129
|
|
|
* @return integer Total number of imported customers |
130
|
|
|
*/ |
131
|
|
|
protected function import( $filename ) |
132
|
|
|
{ |
133
|
|
|
$context = $this->getContext(); |
134
|
|
|
$config = $context->getConfig(); |
135
|
|
|
$domains = ['customer', 'customer/address', 'customer/property', 'media', 'product', 'text']; |
136
|
|
|
|
137
|
|
|
/** controller/jobs/customer/import/xml/domains |
138
|
|
|
* List of item domain names that should be retrieved along with the customer items |
139
|
|
|
* |
140
|
|
|
* This configuration setting overwrites the shared option |
141
|
|
|
* "controller/common/customer/import/xml/domains" if you need a |
142
|
|
|
* specific setting for the job controller. Otherwise, you should |
143
|
|
|
* use the shared option for consistency. |
144
|
|
|
* |
145
|
|
|
* @param array Associative list of MShop item domain names |
146
|
|
|
* @since 2019.04 |
147
|
|
|
* @category Developer |
148
|
|
|
* @see controller/jobs/customer/import/xml/backup |
149
|
|
|
* @see controller/jobs/customer/import/xml/max-query |
150
|
|
|
*/ |
151
|
|
|
$domains = $config->get( 'controller/jobs/customer/import/xml/domains', $domains ); |
152
|
|
|
|
153
|
|
|
/** controller/jobs/customer/import/xml/backup |
154
|
|
|
* Name of the backup for sucessfully imported files |
155
|
|
|
* |
156
|
|
|
* After a XML file was imported successfully, you can move it to another |
157
|
|
|
* location, so it won't be imported again and isn't overwritten by the |
158
|
|
|
* next file that is stored at the same location in the file system. |
159
|
|
|
* |
160
|
|
|
* You should use an absolute path to be sure but can be relative path |
161
|
|
|
* if you absolutely know from where the job will be executed from. The |
162
|
|
|
* name of the new backup location can contain placeholders understood |
163
|
|
|
* by the PHP strftime() function to create dynamic paths, e.g. "backup/%Y-%m-%d" |
164
|
|
|
* which would create "backup/2000-01-01". For more information about the |
165
|
|
|
* strftime() placeholders, please have a look into the PHP documentation of |
166
|
|
|
* the {@link http://php.net/manual/en/function.strftime.php strftime() function}. |
167
|
|
|
* |
168
|
|
|
* '''Note:''' If no backup name is configured, the file or directory |
169
|
|
|
* won't be moved away. Please make also sure that the parent directory |
170
|
|
|
* and the new directory are writable so the file or directory could be |
171
|
|
|
* moved. |
172
|
|
|
* |
173
|
|
|
* @param integer Name of the backup file, optionally with date/time placeholders |
174
|
|
|
* @since 2019.04 |
175
|
|
|
* @category Developer |
176
|
|
|
* @see controller/jobs/customer/import/xml/domains |
177
|
|
|
* @see controller/jobs/customer/import/xml/max-query |
178
|
|
|
*/ |
179
|
|
|
$backup = $config->get( 'controller/jobs/customer/import/xml/backup' ); |
180
|
|
|
|
181
|
|
|
/** controller/jobs/customer/import/xml/max-query |
182
|
|
|
* Maximum number of XML nodes processed at once |
183
|
|
|
* |
184
|
|
|
* Processing and fetching several customer items at once speeds up importing |
185
|
|
|
* the XML files. The more items can be processed at once, the faster the |
186
|
|
|
* import. More items also increases the memory usage of the importer and |
187
|
|
|
* thus, this parameter should be low enough to avoid reaching the memory |
188
|
|
|
* limit of the PHP process. |
189
|
|
|
* |
190
|
|
|
* @param integer Number of XML nodes |
191
|
|
|
* @since 2019.04 |
192
|
|
|
* @category Developer |
193
|
|
|
* @category User |
194
|
|
|
* @see controller/jobs/customer/import/xml/domains |
195
|
|
|
* @see controller/jobs/customer/import/xml/backup |
196
|
|
|
*/ |
197
|
|
|
$maxquery = $config->get( 'controller/jobs/customer/import/xml/max-query', 1000 ); |
198
|
|
|
|
199
|
|
|
$nodes = []; |
200
|
|
|
$total = $slice = 0; |
201
|
|
|
$xml = new \XMLReader(); |
202
|
|
|
|
203
|
|
|
if( $xml->open( $filename, LIBXML_COMPACT | LIBXML_PARSEHUGE ) === false ) { |
204
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No XML file "%1$s" found', $filename ) ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
while( $xml->read() === true ) |
208
|
|
|
{ |
209
|
|
|
if( $xml->depth === 1 && $xml->nodeType === \XMLReader::ELEMENT && $xml->name === 'customeritem' ) |
210
|
|
|
{ |
211
|
|
|
if( ( $dom = $xml->expand() ) === false ) |
212
|
|
|
{ |
213
|
|
|
$msg = sprintf( 'Expanding "%1$s" node failed', 'customeritem' ); |
214
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$nodes[] = $dom; |
218
|
|
|
|
219
|
|
|
if( $slice++ >= $maxquery ) |
220
|
|
|
{ |
221
|
|
|
$this->importNodes( $nodes, $domains ); |
222
|
|
|
unset( $nodes ); |
223
|
|
|
$nodes = []; |
224
|
|
|
$slice = 0; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$total++; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$this->importNodes( $nodes, $domains ); |
232
|
|
|
unset( $nodes ); |
233
|
|
|
|
234
|
|
|
if( !empty( $backup ) && @rename( $filename, strftime( $backup ) ) === false ) |
235
|
|
|
{ |
236
|
|
|
$msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $filename, $backup ); |
237
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $total; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Imports the given DOM nodes |
246
|
|
|
* |
247
|
|
|
* @param \DomElement[] $nodes List of nodes to import |
248
|
|
|
* @param string[] $ref List of domain names whose referenced items will be updated in the customer items |
249
|
|
|
*/ |
250
|
|
|
protected function importNodes( array $nodes, array $ref ) |
251
|
|
|
{ |
252
|
|
|
$codes = $map = []; |
253
|
|
|
|
254
|
|
|
foreach( $nodes as $node ) |
255
|
|
|
{ |
256
|
|
|
if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) { |
257
|
|
|
$codes[$attr->nodeValue] = null; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
262
|
|
|
$search = $manager->createSearch()->setSlice( 0, count( $codes ) ); |
263
|
|
|
$search->setConditions( $search->compare( '==', 'customer.code', array_keys( $codes ) ) ); |
264
|
|
|
|
265
|
|
|
foreach( $manager->searchItems( $search, $ref ) as $item ) { |
266
|
|
|
$map[$item->getCode()] = $item; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
foreach( $nodes as $node ) |
270
|
|
|
{ |
271
|
|
|
if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null && isset( $map[$attr->nodeValue] ) ) { |
272
|
|
|
$item = $this->process( $map[$attr->nodeValue], $node ); |
273
|
|
|
} else { |
274
|
|
|
$item = $this->process( $manager->createItem(), $node ); |
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$manager->saveItem( $item ); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Updates the customer item and its referenced items using the given DOM node |
284
|
|
|
* |
285
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object to update |
286
|
|
|
* @param \DomElement $node DOM node used for updateding the customer item |
287
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface $item Updated customer item object |
288
|
|
|
*/ |
289
|
|
|
protected function process( \Aimeos\MShop\Customer\Item\Iface $item, \DomElement $node ) |
290
|
|
|
{ |
291
|
|
|
$list = []; |
292
|
|
|
|
293
|
|
|
foreach( $node->attributes as $attr ) { |
294
|
|
|
$list[$attr->nodeName] = $attr->nodeValue; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
foreach( $node->childNodes as $tag ) |
298
|
|
|
{ |
299
|
|
|
if( in_array( $tag->nodeName, ['address', 'lists', 'property'] ) ) { |
300
|
|
|
$item = $this->getProcessor( $tag->nodeName )->process( $item, $tag ); |
301
|
|
|
} else { |
302
|
|
|
$list[$tag->nodeName] = $tag->nodeValue; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $item->fromArray( $list, true ); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths