Passed
Push — master ( 658422...66e8c5 )
by Aimeos
03:08
created

Standard::backup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 30
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2022
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Catalog\Import\Xml;
12
13
14
/**
15
 * Job controller for XML catalog 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/catalog/import/xml/name
25
	 * Class name of the used catalog 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\Catalog\Import\Xml\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Catalog\Import\Xml\Myxml
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/catalog/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/catalog/import/xml/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the catalog 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/catalog/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/catalog/import/xml/decorators/global
79
	 * @see controller/jobs/catalog/import/xml/decorators/local
80
	 */
81
82
	/** controller/jobs/catalog/import/xml/decorators/global
83
	 * Adds a list of globally available decorators only to the catalog 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/catalog/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/catalog/import/xml/decorators/excludes
102
	 * @see controller/jobs/catalog/import/xml/decorators/local
103
	 */
104
105
	/** controller/jobs/catalog/import/xml/decorators/local
106
	 * Adds a list of local decorators only to the catalog 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\Catalog\Import\Xml\Decorator\*") around the job
115
	 * controller.
116
	 *
117
	 *  controller/jobs/catalog/import/xml/decorators/local = array( 'decorator2' )
118
	 *
119
	 * This would add the decorator named "decorator2" defined by
120
	 * "\Aimeos\Controller\Jobs\Catalog\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/catalog/import/xml/decorators/excludes
127
	 * @see controller/jobs/catalog/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', 'Catalog 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 categories 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
		$location = $this->location();
166
167
		try
168
		{
169
			$logger->info( sprintf( 'Started catalog import from "%1$s"', $location ), 'import/xml/catalog' );
170
171
			if( !file_exists( $location ) )
172
			{
173
				$msg = sprintf( 'File or directory "%1$s" doesn\'t exist', $location );
174
				throw new \Aimeos\Controller\Jobs\Exception( $msg );
175
			}
176
177
			$files = [];
178
179
			if( is_dir( $location ) )
180
			{
181
				foreach( new \DirectoryIterator( $location ) as $entry )
182
				{
183
					if( strncmp( $entry->getFilename(), 'catalog', 7 ) === 0 && $entry->getExtension() === 'xml' ) {
184
						$files[] = $entry->getPathname();
185
					}
186
				}
187
			}
188
			else
189
			{
190
				$files[] = $location;
191
			}
192
193
			sort( $files );
194
			$context->__sleep();
195
196
			$fcn = function( $filepath ) {
197
				$this->import( $filepath );
198
			};
199
200
			foreach( $files as $filepath ) {
201
				$context->process()->start( $fcn, [$filepath] );
202
			}
203
204
			$context->process()->wait();
205
206
			$logger->info( sprintf( 'Finished catalog import from "%1$s"', $location ), 'import/xml/catalog' );
207
		}
208
		catch( \Exception $e )
209
		{
210
			$logger->error( 'Catalog import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/xml/catalog' );
211
			$this->mail( 'Catalog XML import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
212
			throw $e;
213
		}
214
	}
215
216
217
	/**
218
	 * Returns the directory for storing imported files
219
	 *
220
	 * @return string Directory for storing imported files
221
	 */
222
	protected function backup() : string
223
	{
224
		/** controller/jobs/catalog/import/xml/backup
225
		 * Name of the backup for sucessfully imported files
226
		 *
227
		 * After a XML file was imported successfully, you can move it to another
228
		 * location, so it won't be imported again and isn't overwritten by the
229
		 * next file that is stored at the same location in the file system.
230
		 *
231
		 * You should use an absolute path to be sure but can be relative path
232
		 * if you absolutely know from where the job will be executed from. The
233
		 * name of the new backup location can contain placeholders understood
234
		 * by the PHP DateTime::format() method (with percent signs prefix) to
235
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
236
		 * "backup/2000-01-01". For more information about the date() placeholders,
237
		 * please have a look  into the PHP documentation of the
238
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
239
		 *
240
		 * **Note:** If no backup name is configured, the file or directory
241
		 * won't be moved away. Please make also sure that the parent directory
242
		 * and the new directory are writable so the file or directory could be
243
		 * moved.
244
		 *
245
		 * @param integer Name of the backup file, optionally with date/time placeholders
246
		 * @since 2019.04
247
		 * @see controller/jobs/catalog/import/xml/domains
248
		 * @see controller/jobs/catalog/import/xml/location
249
		 * @see controller/jobs/catalog/import/xml/max-query
250
		 */
251
		return (string) $this->context()->config()->get( 'controller/jobs/catalog/import/xml/backup' );
252
	}
253
254
255
	/**
256
	 * Returns the list of domain names that should be retrieved along with the catalog items
257
	 *
258
	 * @return array List of domain names
259
	 */
260
	protected function domains() : array
261
	{
262
		/** controller/jobs/catalog/import/xml/domains
263
		 * List of item domain names that should be retrieved along with the catalog items
264
		 *
265
		 * For efficient processing, the items associated to the products can be
266
		 * fetched to, minimizing the number of database queries required. To be
267
		 * most effective, the list of item domain names should be used in the
268
		 * mapping configuration too, so the retrieved items will be used during
269
		 * the import.
270
		 *
271
		 * @param array Associative list of MShop item domain names
272
		 * @since 2019.04
273
		 * @see controller/jobs/catalog/import/xml/backup
274
		 * @see controller/jobs/catalog/import/xml/location
275
		 * @see controller/jobs/catalog/import/xml/max-query
276
		 */
277
		return $this->context()->config()->get( 'controller/jobs/catalog/import/xml/domains', [] );
278
	}
279
280
281
	/**
282
	 * Imports the XML file given by its path
283
	 *
284
	 * @param string $filename Absolute or relative path to the XML file
285
	 */
286
	protected function import( string $filename )
287
	{
288
		$logger = $this->context()->logger();
289
		$xml = new \XMLReader();
290
291
		if( $xml->open( $filename, LIBXML_COMPACT | LIBXML_PARSEHUGE ) === false ) {
292
			throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No XML file "%1$s" found', $filename ) );
293
		}
294
295
		$logger->info( sprintf( 'Started catalog import from file "%1$s"', $filename ), 'import/xml/catalog' );
296
297
		$this->importTree( $xml, $this->domains() );
298
299
		foreach( $this->getProcessors() as $proc ) {
300
			$proc->finish();
301
		}
302
303
		$logger->info( sprintf( 'Finished catalog import from file "%1$s"', $filename ), 'import/xml/catalog' );
304
305
		if( !empty( $backup = $this->backup() ) && @rename( $filename, $backup = \Aimeos\Base\Str::strtime( $backup ) ) === false )
306
		{
307
			$msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $filename, $backup );
308
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
309
		}
310
	}
311
312
313
	/**
314
	 * Imports a single category node
315
	 *
316
	 * @param \DomElement $node DOM node of "catalogitem" element
317
	 * @param string[] $domains List of domain names whose referenced items will be updated in the catalog items
318
	 * @param string|null $parentid ID of the parent catalog node
319
	 * @param array &$map Will contain the associative list of code/ID pairs of the child categories
320
	 * @return string Catalog ID of the imported category
321
	 */
322
	protected function importNode( \DomElement $node, array $domains, string $parentid = null, array &$map ) : string
323
	{
324
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
325
326
		if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null )
0 ignored issues
show
Bug introduced by
The method getNamedItem() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

326
		if( ( $attr = $node->attributes->/** @scrutinizer ignore-call */ getNamedItem( 'ref' ) ) !== null )

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
327
		{
328
			try
329
			{
330
				$item = $manager->find( $attr->nodeValue, $domains );
331
				$manager->move( $item->getId(), $item->getParentId(), $parentid );
332
333
				$item = $this->process( $item, $node );
334
				$currentid = $manager->save( $item )->getId();
335
				unset( $item );
336
337
				$tree = $manager->getTree( $currentid, [], \Aimeos\MW\Tree\Manager\Base::LEVEL_LIST );
338
339
				foreach( $tree->getChildren() as $child ) {
340
					$map[$child->getCode()] = $child->getId();
341
				}
342
343
				return $currentid;
344
			}
345
			catch( \Aimeos\MShop\Exception $e ) {} // not found, create new
346
		}
347
348
		$item = $this->process( $manager->create(), $node );
349
		return $manager->insert( $item, $parentid )->getId();
350
	}
351
352
353
	/**
354
	 * Imports the catalog document
355
	 *
356
	 * @param \XMLReader $xml Catalog document to import
357
	 * @param string[] $domains List of domain names whose referenced items will be updated in the catalog items
358
	 * @param string|null $parentid ID of the parent catalog node
359
	 * @param array $map Associative list of catalog code as keys and category ID as values
360
	 */
361
	protected function importTree( \XMLReader $xml, array $domains, string $parentid = null, array $map = [] )
362
	{
363
		$total = 0;
364
		$childMap = [];
365
		$currentid = $parentid;
366
367
		while( $xml->read() === true )
368
		{
369
			if( $xml->nodeType === \XMLReader::ELEMENT && $xml->name === 'catalogitem' )
370
			{
371
				if( ( $node = $xml->expand() ) === false )
372
				{
373
					$msg = sprintf( 'Expanding "%1$s" node failed', 'catalogitem' );
374
					throw new \Aimeos\Controller\Jobs\Exception( $msg );
375
				}
376
377
				if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
378
					unset( $map[$attr->nodeValue] );
379
				}
380
381
				$currentid = $this->importNode( $node, $domains, $parentid, $childMap );
382
				$total++;
383
			}
384
			elseif( $xml->nodeType === \XMLReader::ELEMENT && $xml->name === 'catalog' )
385
			{
386
				$this->importTree( $xml, $domains, $currentid, $childMap );
387
				$childMap = [];
388
			}
389
			elseif( $xml->nodeType === \XMLReader::END_ELEMENT && $xml->name === 'catalog' )
390
			{
391
				\Aimeos\MShop::create( $this->context(), 'catalog' )->delete( $map );
392
				break;
393
			}
394
		}
395
	}
396
397
398
	/**
399
	 * Returns the path to the directory with the XML file
400
	 *
401
	 * @return string Path to the directory with the XML file
402
	 */
403
	protected function location() : string
404
	{
405
		/** controller/jobs/catalog/import/xml/location
406
		 * File or directory where the content is stored which should be imported
407
		 *
408
		 * You need to configure the XML file or directory with the XML files that
409
		 * should be imported. It should be an absolute path to be sure but can be
410
		 * relative path if you absolutely know from where the job will be executed
411
		 * from.
412
		 *
413
		 * @param string Relative path to the XML files
414
		 * @since 2019.04
415
		 * @see controller/jobs/catalog/import/xml/backup
416
		 * @see controller/jobs/catalog/import/xml/domains
417
		 * @see controller/jobs/catalog/import/xml/max-query
418
		 */
419
		return (string) $this->context()->config()->get( 'controller/jobs/catalog/import/xml/location', 'catalog' );
420
	}
421
422
423
	/**
424
	 * Updates the catalog item and its referenced items using the given DOM node
425
	 *
426
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object to update
427
	 * @param \DomElement $node DOM node used for updateding the catalog item
428
	 * @return \Aimeos\MShop\Catalog\Item\Iface $item Updated catalog item object
429
	 */
430
	protected function process( \Aimeos\MShop\Catalog\Item\Iface $item, \DomElement $node ) : \Aimeos\MShop\Catalog\Item\Iface
431
	{
432
		try
433
		{
434
			$list = [];
435
436
			foreach( $node->attributes as $attr ) {
437
				$list[$attr->nodeName] = $attr->nodeValue;
438
			}
439
440
			foreach( $node->childNodes as $tag )
441
			{
442
				if( $tag->nodeName === 'lists' ) {
443
					$item = $this->getProcessor( $tag->nodeName )->process( $item, $tag );
444
				} elseif( $tag->nodeName[0] !== '#' ) {
445
					$list[$tag->nodeName] = $tag->nodeValue;
446
				}
447
			}
448
449
			$list['catalog.config'] = isset( $list['catalog.config'] ) ? json_decode( $list['catalog.config'], true ) : [];
450
			$item->fromArray( $list, true );
451
		}
452
		catch( \Exception $e )
453
		{
454
			$msg = 'Catalog import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString();
455
			$this->context()->logger()->error( $msg, 'import/xml/catalog' );
456
		}
457
458
		return $item;
459
	}
460
}
461