Passed
Push — master ( 219e21...0cbac8 )
by Aimeos
03:14
created

Standard::export()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 23
rs 9.7998
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Product\Export\Sitemap;
12
13
14
/**
15
 * Job controller for product sitemap.
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/product/export/sitemap/name
25
	 * Class name of the used product 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\Product\Export\Sitemap\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Product\Export\Sitemap\Mysitemap
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/product/export/sitemap/name = Mysitemap
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 "MySitemap"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2015.01
55
	 */
56
57
	/** controller/jobs/product/export/sitemap/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the product export sitemap 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/product/export/sitemap/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 2015.01
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/product/export/sitemap/decorators/global
79
	 * @see controller/jobs/product/export/sitemap/decorators/local
80
	 */
81
82
	/** controller/jobs/product/export/sitemap/decorators/global
83
	 * Adds a list of globally available decorators only to the product export sitemap 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/product/export/sitemap/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 2015.01
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/product/export/sitemap/decorators/excludes
102
	 * @see controller/jobs/product/export/sitemap/decorators/local
103
	 */
104
105
	/** controller/jobs/product/export/sitemap/decorators/local
106
	 * Adds a list of local decorators only to the product export sitemap 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\Product\Export\Sitemap\Decorator\*") around the job
115
	 * controller.
116
	 *
117
	 *  controller/jobs/product/export/sitemap/decorators/local = array( 'decorator2' )
118
	 *
119
	 * This would add the decorator named "decorator2" defined by
120
	 * "\Aimeos\Controller\Jobs\Product\Export\Sitemap\Decorator\Decorator2"
121
	 * only to the job controller.
122
	 *
123
	 * @param array List of decorator names
124
	 * @since 2015.01
125
	 * @see controller/jobs/common/decorators/default
126
	 * @see controller/jobs/product/export/sitemap/export/sitemap/decorators/excludes
127
	 * @see controller/jobs/product/export/sitemap/export/sitemap/decorators/global
128
	 */
129
130
131
	private ?\Aimeos\Map $locales = null;
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', 'Product site map' );
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', 'Creates a product site map for search engines' );
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
		/** controller/jobs/product/export/sitemap/hidden
164
		 * Export hidden products in site map
165
		 *
166
		 * The product site map contains no hidden products by default. If they
167
		 * should be part of the export, set this configuration option to TRUE.
168
		 *
169
		 * @param bool TRUE to export hidden products, FALSE if not
170
		 * @since 2022.01
171
		 * @see controller/jobs/product/export/sitemap/container/options
172
		 * @see controller/jobs/product/export/sitemap/location
173
		 * @see controller/jobs/product/export/sitemap/max-items
174
		 * @see controller/jobs/product/export/sitemap/max-query
175
		 * @see controller/jobs/product/export/sitemap/changefreq
176
		 */
177
		$hidden = $this->context()->config()->get( 'controller/jobs/product/export/sitemap/hidden', false );
178
179
		$this->createIndex( $this->export( $hidden ? null : true ) );
180
	}
181
182
183
	/**
184
	 * Adds the content for the site map index file
185
	 *
186
	 * @param array $files List of generated site map file names
187
	 */
188
	protected function createIndex( array $files )
189
	{
190
		$context = $this->context();
191
		$config = $context->config();
192
		$view = $context->view();
193
194
		/** controller/jobs/product/export/sitemap/template-index
195
		 * Relative path to the XML site map index template of the product site map job controller.
196
		 *
197
		 * The template file contains the XML code and processing instructions
198
		 * to generate the site map index files. The configuration string is the path
199
		 * to the template file relative to the templates directory (usually in
200
		 * templates/controller/jobs).
201
		 *
202
		 * You can overwrite the template file configuration in extensions and
203
		 * provide alternative templates. These alternative templates should be
204
		 * named like the default one but with the string "standard" replaced by
205
		 * an unique name. You may use the name of your project for this. If
206
		 * you've implemented an alternative client class as well, "standard"
207
		 * should be replaced by the name of the new class.
208
		 *
209
		 * @param string Relative path to the template creating XML code for the site map index
210
		 * @since 2015.01
211
		 * @see controller/jobs/product/export/sitemap/template-items
212
		 */
213
		$tplconf = 'controller/jobs/product/export/sitemap/template-index';
214
215
		if( empty( $baseUrl = rtrim( $config->get( 'resource/fs/baseurl', '' ), '/' ) ) )
216
		{
217
			$msg = sprintf( 'Required configuration for "%1$s" is missing', 'resource/fs/baseurl' );
218
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
219
		}
220
221
		$view->siteFiles = $files;
222
		$view->baseUrl = $baseUrl . '/';
223
224
		$content = $view->render( $config->get( $tplconf, 'product/export/sitemap-index' ) );
225
		$context->fs()->write( $this->call( 'indexFilename' ), $content );
226
	}
227
228
229
	/**
230
	 * Exports the sitemap files
231
	 *
232
	 * @param bool|null $default TRUE to use default criteria, NULL for relaxed criteria
233
	 * @return array List of temporary files
234
	 */
235
	protected function export( ?bool $default = true ) : array
236
	{
237
		$manager = \Aimeos\MShop::create( $this->context(), 'index' );
238
239
		$search = $manager->filter( $default )->order( 'product.id' );
240
		$search->add( $search->make( 'product:has', ['catalog'] ), '!=', null );
241
		$cursor = $manager->cursor( $search->slice( 0, $this->max() ) );
242
243
		$domains = $this->domains();
244
		$max = $this->max();
0 ignored issues
show
Unused Code introduced by
The assignment to $max is dead and can be removed.
Loading history...
245
		$fs = $this->fs();
246
247
		$filenum = 1;
248
		$files = [];
249
250
		while( $items = $manager->iterate( $cursor, $domains ) )
251
		{
252
			$filename = $this->call( 'filename', $filenum++ );
253
			$fs->write( $filename, $this->render( $items ) );
254
			$files[] = $filename;
255
		}
256
257
		return $files;
258
	}
259
260
261
	/**
262
	 * Returns the domain names whose items should be exported too
263
	 *
264
	 * @return array List of domain names
265
	 */
266
	protected function domains() : array
267
	{
268
		/** controller/jobs/product/export/sitemap/domains
269
		 * List of associated items from other domains that should be fetched for the sitemap
270
		 *
271
		 * Catalogs consist not only of the base data but also of texts, media and
272
		 * other details. Those information is associated to the product via their lists.
273
		 * Using the "domains" option you can make more or less associated items available
274
		 * in the template.
275
		 *
276
		 * @param array List of domain names
277
		 * @since 2019.02
278
		 * @see controller/jobs/product/export/sitemap/max-items
279
		 */
280
		return $this->context()->config()->get( 'controller/jobs/product/export/sitemap/domains', ['text'] );
281
	}
282
283
284
	/**
285
	 * Returns the sitemap file name
286
	 *
287
	 * @param int $number Current file number
288
	 * @return string File name
289
	 */
290
	protected function filename( int $number ) : string
291
	{
292
		return sprintf( 'aimeos-sitemap-%d.xml', $number );
293
	}
294
295
296
	/**
297
	 * Returns the file system for storing the exported files
298
	 *
299
	 * @return \Aimeos\Base\Filesystem\Iface File system to store files to
300
	 */
301
	protected function fs() : \Aimeos\Base\Filesystem\Iface
302
	{
303
		return $this->context()->fs( 'fs' );
304
	}
305
306
307
	/**
308
	 * Returns the file name of the sitemap index file
309
	 *
310
	 * @return string File name
311
	 */
312
	protected function indexFilename() : string
313
	{
314
		return 'aimeos-sitemap-index.xml';
315
	}
316
317
318
	/**
319
	 * Returns the available locale items for the current site
320
	 *
321
	 * @return \Aimeos\Map List of locale items
322
	 */
323
	protected function locales() : \Aimeos\Map
324
	{
325
		if( !isset( $this->locales ) )
326
		{
327
			$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
328
			$filter = $manager->filter( true )->add( ['locale.siteid' => $this->context()->locale()->getSiteId()] );
329
330
			$this->locales = $manager->search( $filter->order( 'locale.position' )->slice( 0, 10000 ) );
331
		}
332
333
		return $this->locales;
334
	}
335
336
337
	/**
338
	 * Returns the maximum number of exported products per file
339
	 *
340
	 * @return int Maximum number of exported products per file
341
	 */
342
	protected function max() : int
343
	{
344
		/** controller/jobs/product/export/sitemap/max-items
345
		 * Maximum number of categories per site map
346
		 *
347
		 * Each site map file must not contain more than 50,000 links and it's
348
		 * size must be less than 10MB. If your product URLs are rather long
349
		 * and one of your site map files is bigger than 10MB, you should set
350
		 * the number of categories per file to a smaller value until each file
351
		 * is less than 10MB.
352
		 *
353
		 * More details about site maps can be found at
354
		 * {@link http://www.sitemaps.org/protocol.html sitemaps.org}
355
		 *
356
		 * @param integer Number of categories per file
357
		 * @since 2019.02
358
		 * @see controller/jobs/product/export/sitemap/domains
359
		 */
360
		return $this->context()->config()->get( 'controller/jobs/product/export/sitemap/max-items', 10000 );
361
	}
362
363
364
	/**
365
	 * Creates sitemap with the given products
366
	 *
367
	 * @param \Aimeos\Map $items List of product items implementing \Aimeos\MShop\Product\Item\Iface
368
	 * @return string Rendered content
369
	 */
370
	protected function render( \Aimeos\Map $items ) : string
371
	{
372
		/** controller/jobs/product/export/sitemap/template
373
		 * Relative path to the XML template of the product site map job controller.
374
		 *
375
		 * The template file contains the XML code and processing instructions
376
		 * to generate the site map files. The configuration string is the path
377
		 * to the template file relative to the templates directory (usually in
378
		 * templates/controller/jobs).
379
		 *
380
		 * You can overwrite the template file configuration in extensions and
381
		 * provide alternative templates. These alternative templates should be
382
		 * named like the default one but with the string "standard" replaced by
383
		 * an unique name. You may use the name of your project for this. If
384
		 * you've implemented an alternative client class as well, "standard"
385
		 * should be replaced by the name of the new class.
386
		 *
387
		 * @param string Relative path to the template creating XML code for the site map
388
		 * @since 2022.10
389
		 */
390
		$tplconf = 'controller/jobs/product/export/sitemap/template';
391
392
		$context = $this->context();
393
		$view = $context->view();
394
395
		$view->siteItems = $items;
396
		$view->siteLocales = $this->locales();
397
398
		return $view->render( $context->config()->get( $tplconf, 'product/export/sitemap-items' ) );
399
	}
400
}
401