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

Standard::createSitemaps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 47
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 47
rs 9.9332

1 Method

Rating   Name   Duplication   Size   Complexity  
A Standard::max() 0 19 1
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\Catalog\Export\Sitemap;
12
13
14
/**
15
 * Job controller for catalog 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/catalog/export/sitemap/name
25
	 * Class name of the used catalog sitemap export 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\Export\Sitemap\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Catalog\Export\Sitemap\Mysitemap
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/catalog/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 2019.02
55
	 */
56
57
	/** controller/jobs/catalog/export/sitemap/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the catalog 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/catalog/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 2019.02
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/catalog/export/sitemap/decorators/global
79
	 * @see controller/jobs/catalog/export/sitemap/decorators/local
80
	 */
81
82
	/** controller/jobs/catalog/export/sitemap/decorators/global
83
	 * Adds a list of globally available decorators only to the catalog 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/catalog/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 2019.02
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/catalog/export/sitemap/decorators/excludes
102
	 * @see controller/jobs/catalog/export/sitemap/decorators/local
103
	 */
104
105
	/** controller/jobs/catalog/export/sitemap/decorators/local
106
	 * Adds a list of local decorators only to the catalog 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\Catalog\Export\Sitemap\Decorator\*") around the job
115
	 * controller.
116
	 *
117
	 *  controller/jobs/catalog/export/sitemap/decorators/local = array( 'decorator2' )
118
	 *
119
	 * This would add the decorator named "decorator2" defined by
120
	 * "\Aimeos\Controller\Jobs\Catalog\Export\Sitemap\Decorator\Decorator2"
121
	 * only to the job controller.
122
	 *
123
	 * @param array List of decorator names
124
	 * @since 2019.02
125
	 * @see controller/jobs/common/decorators/default
126
	 * @see controller/jobs/catalog/export/sitemap/export/sitemap/decorators/excludes
127
	 * @see controller/jobs/catalog/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', 'Catalog 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 catalog 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/catalog/export/sitemap/hidden
164
		 * Export hidden categories in site map
165
		 *
166
		 * The catalog site map contains no hidden categories 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 categories, FALSE if not
170
		 * @since 2022.01
171
		 * @see controller/jobs/catalog/export/sitemap/container/options
172
		 * @see controller/jobs/catalog/export/sitemap/location
173
		 * @see controller/jobs/catalog/export/sitemap/max-items
174
		 * @see controller/jobs/catalog/export/sitemap/max-query
175
		 * @see controller/jobs/catalog/export/sitemap/changefreq
176
		 */
177
		$hidden = $this->context()->config()->get( 'controller/jobs/catalog/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/catalog/export/sitemap/template-index
195
		 * Relative path to the XML site map index template of the catalog 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/catalog/export/sitemap/template-items
212
		 */
213
		$tplconf = 'controller/jobs/catalog/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, 'catalog/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(), 'catalog' );
238
239
		$search = $manager->filter( $default )->order( 'catalog.id' );
240
		$cursor = $manager->cursor( $search->slice( 0, $this->max() ) );
241
242
		$domains = $this->domains();
243
		$max = $this->max();
0 ignored issues
show
Unused Code introduced by
The assignment to $max is dead and can be removed.
Loading history...
244
		$fs = $this->fs();
245
246
		$filenum = 1;
247
		$files = [];
248
249
		while( $items = $manager->iterate( $cursor, $domains ) )
250
		{
251
			$filename = $this->call( 'filename', $filenum++ );
252
			$fs->write( $filename, $this->render( $items ) );
253
			$files[] = $filename;
254
		}
255
256
		return $files;
257
	}
258
259
260
	/**
261
	 * Returns the domain names whose items should be exported too
262
	 *
263
	 * @return array List of domain names
264
	 */
265
	protected function domains() : array
266
	{
267
		/** controller/jobs/catalog/export/sitemap/domains
268
		 * List of associated items from other domains that should be fetched for the sitemap
269
		 *
270
		 * Catalogs consist not only of the base data but also of texts, media and
271
		 * other details. Those information is associated to the catalog via their lists.
272
		 * Using the "domains" option you can make more or less associated items available
273
		 * in the template.
274
		 *
275
		 * @param array List of domain names
276
		 * @since 2019.02
277
		 * @see controller/jobs/catalog/export/sitemap/max-items
278
		 */
279
		return $this->context()->config()->get( 'controller/jobs/catalog/export/sitemap/domains', ['text'] );
280
	}
281
282
283
	/**
284
	 * Returns the sitemap file name
285
	 *
286
	 * @param int $number Current file number
287
	 * @return string File name
288
	 */
289
	protected function filename( int $number ) : string
290
	{
291
		return sprintf( 'aimeos-catalog-sitemap-%d.xml', $number );
292
	}
293
294
295
	/**
296
	 * Returns the file system for storing the exported files
297
	 *
298
	 * @return \Aimeos\Base\Filesystem\Iface File system to store files to
299
	 */
300
	protected function fs() : \Aimeos\Base\Filesystem\Iface
301
	{
302
		return $this->context()->fs( 'fs' );
303
	}
304
305
306
	/**
307
	 * Returns the file name of the sitemap index file
308
	 *
309
	 * @return string File name
310
	 */
311
	protected function indexFilename() : string
312
	{
313
		return 'aimeos-catalog-sitemap-index.xml';
314
	}
315
316
317
	/**
318
	 * Returns the available locale items for the current site
319
	 *
320
	 * @return \Aimeos\Map List of locale items
321
	 */
322
	protected function locales() : \Aimeos\Map
323
	{
324
		if( !isset( $this->locales ) )
325
		{
326
			$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
327
			$filter = $manager->filter( true )->add( ['locale.siteid' => $this->context()->locale()->getSiteId()] );
328
329
			$this->locales = $manager->search( $filter->order( 'locale.position' )->slice( 0, 10000 ) );
330
		}
331
332
		return $this->locales;
333
	}
334
335
336
	/**
337
	 * Returns the maximum number of exported categories per file
338
	 *
339
	 * @return int Maximum number of exported categories per file
340
	 */
341
	protected function max() : int
342
	{
343
		/** controller/jobs/catalog/export/sitemap/max-items
344
		 * Maximum number of categories per site map
345
		 *
346
		 * Each site map file must not contain more than 50,000 links and it's
347
		 * size must be less than 10MB. If your catalog URLs are rather long
348
		 * and one of your site map files is bigger than 10MB, you should set
349
		 * the number of categories per file to a smaller value until each file
350
		 * is less than 10MB.
351
		 *
352
		 * More details about site maps can be found at
353
		 * {@link http://www.sitemaps.org/protocol.html sitemaps.org}
354
		 *
355
		 * @param integer Number of categories per file
356
		 * @since 2019.02
357
		 * @see controller/jobs/catalog/export/sitemap/domains
358
		 */
359
		return $this->context()->config()->get( 'controller/jobs/catalog/export/sitemap/max-items', 10000 );
360
	}
361
362
363
	/**
364
	 * Creates sitemap with the given categories
365
	 *
366
	 * @param \Aimeos\Map $items List of catalog items implementing \Aimeos\MShop\Product\Item\Iface
367
	 * @return string Rendered content
368
	 */
369
	protected function render( \Aimeos\Map $items ) : string
370
	{
371
		/** controller/jobs/catalog/export/sitemap/template
372
		 * Relative path to the XML template of the catalog site map job controller.
373
		 *
374
		 * The template file contains the XML code and processing instructions
375
		 * to generate the site map files. The configuration string is the path
376
		 * to the template file relative to the templates directory (usually in
377
		 * templates/controller/jobs).
378
		 *
379
		 * You can overwrite the template file configuration in extensions and
380
		 * provide alternative templates. These alternative templates should be
381
		 * named like the default one but with the string "standard" replaced by
382
		 * an unique name. You may use the name of your project for this. If
383
		 * you've implemented an alternative client class as well, "standard"
384
		 * should be replaced by the name of the new class.
385
		 *
386
		 * @param string Relative path to the template creating XML code for the site map
387
		 * @since 2022.10
388
		 */
389
		$tplconf = 'controller/jobs/catalog/export/sitemap/template';
390
391
		$context = $this->context();
392
		$view = $context->view();
393
394
		$view->siteItems = $items;
395
		$view->siteLocales = $this->locales();
396
397
		return $view->render( $context->config()->get( $tplconf, 'catalog/export/sitemap-items' ) );
398
	}
399
}
400