Passed
Push — master ( 2923cf...904cad )
by Aimeos
03:02
created

Standard::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 43
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 43
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
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
	 * @category Developer
56
	 */
57
58
	/** controller/jobs/catalog/export/sitemap/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the catalog export sitemap job controller
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "controller/jobs/common/decorators/default" before they are wrapped
68
	 * around the job controller.
69
	 *
70
	 *  controller/jobs/catalog/export/sitemap/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
74
	 * "controller/jobs/common/decorators/default" to the job controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2019.02
78
	 * @category Developer
79
	 * @see controller/jobs/common/decorators/default
80
	 * @see controller/jobs/catalog/export/sitemap/decorators/global
81
	 * @see controller/jobs/catalog/export/sitemap/decorators/local
82
	 */
83
84
	/** controller/jobs/catalog/export/sitemap/decorators/global
85
	 * Adds a list of globally available decorators only to the catalog export sitemap job controller
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
94
	 *
95
	 *  controller/jobs/catalog/export/sitemap/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2019.02
102
	 * @category Developer
103
	 * @see controller/jobs/common/decorators/default
104
	 * @see controller/jobs/catalog/export/sitemap/decorators/excludes
105
	 * @see controller/jobs/catalog/export/sitemap/decorators/local
106
	 */
107
108
	/** controller/jobs/catalog/export/sitemap/decorators/local
109
	 * Adds a list of local decorators only to the catalog export sitemap job controller
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Controller\Jobs\Catalog\Export\Sitemap\Decorator\*") around the job
118
	 * controller.
119
	 *
120
	 *  controller/jobs/catalog/export/sitemap/decorators/local = array( 'decorator2' )
121
	 *
122
	 * This would add the decorator named "decorator2" defined by
123
	 * "\Aimeos\Controller\Jobs\Catalog\Export\Sitemap\Decorator\Decorator2"
124
	 * only to the job controller.
125
	 *
126
	 * @param array List of decorator names
127
	 * @since 2019.02
128
	 * @category Developer
129
	 * @see controller/jobs/common/decorators/default
130
	 * @see controller/jobs/catalog/export/sitemap/export/sitemap/decorators/excludes
131
	 * @see controller/jobs/catalog/export/sitemap/export/sitemap/decorators/global
132
	 */
133
134
135
	private $locales;
136
137
138
	/**
139
	 * Returns the localized name of the job.
140
	 *
141
	 * @return string Name of the job
142
	 */
143
	public function getName() : string
144
	{
145
		return $this->context()->translate( 'controller/jobs', 'Catalog site map' );
146
	}
147
148
149
	/**
150
	 * Returns the localized description of the job.
151
	 *
152
	 * @return string Description of the job
153
	 */
154
	public function getDescription() : string
155
	{
156
		return $this->context()->translate( 'controller/jobs', 'Creates a catalog site map for search engines' );
157
	}
158
159
160
	/**
161
	 * Executes the job.
162
	 *
163
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
164
	 */
165
	public function run()
166
	{
167
		$context = $this->context();
168
169
		/** controller/jobs/catalog/export/sitemap/hidden
170
		 * Export hidden categories in site map
171
		 *
172
		 * The catalog site map contains no hidden categories by default. If they
173
		 * should be part of the export, set this configuration option to TRUE.
174
		 *
175
		 * @param bool TRUE to export hidden categories, FALSE if not
176
		 * @since 2022.01
177
		 * @see controller/jobs/catalog/export/sitemap/container/options
178
		 * @see controller/jobs/catalog/export/sitemap/location
179
		 * @see controller/jobs/catalog/export/sitemap/max-items
180
		 * @see controller/jobs/catalog/export/sitemap/max-query
181
		 * @see controller/jobs/catalog/export/sitemap/changefreq
182
		 */
183
		$hidden = $context->config()->get( 'controller/jobs/catalog/export/sitemap/hidden', false );
184
185
		$names = [];
186
		$fs = $context->fs();
187
188
		foreach( $this->createSitemaps( $hidden ? null : true ) as $idx => $file )
189
		{
190
			$name = $this->call( 'sitemapFilename', $idx + 1 );
191
			$fs->writes( $name, $file );
192
			$names[] = $name;
193
			fclose( $file );
194
		}
195
196
		$this->createSitemapIndex( $names );
197
	}
198
199
200
	/**
201
	 * Creates a temporary sitemap file with the given categories
202
	 *
203
	 * @param \Aimeos\Map $items List of catalog items implementing \Aimeos\MShop\Catalog\Item\Iface
204
	 * @param resource File handle
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Jobs\Catalog\Export\Sitemap\File was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
205
	 */
206
	protected function create( \Aimeos\Map $items )
207
	{
208
		/** controller/jobs/catalog/export/sitemap/template
209
		 * Relative path to the XML template of the catalog site map job controller.
210
		 *
211
		 * The template file contains the XML code and processing instructions
212
		 * to generate the site map files. The configuration string is the path
213
		 * to the template file relative to the templates directory (usually in
214
		 * controller/jobs/templates).
215
		 *
216
		 * You can overwrite the template file configuration in extensions and
217
		 * provide alternative templates. These alternative templates should be
218
		 * named like the default one but with the string "standard" replaced by
219
		 * an unique name. You may use the name of your project for this. If
220
		 * you've implemented an alternative client class as well, "standard"
221
		 * should be replaced by the name of the new class.
222
		 *
223
		 * @param string Relative path to the template creating XML code for the site map
224
		 * @since 2022.10
225
		 */
226
		$tplconf = 'controller/jobs/catalog/export/sitemap/template';
227
228
		$context = $this->context();
229
		$view = $context->view();
230
231
		$view->siteItems = $items;
232
		$view->siteLocales = $this->locales();
233
234
		$content = $view->render( $context->config()->get( $tplconf, 'catalog/export/sitemap-items' ) );
235
236
		if( ( $file = tmpfile() ) === false ) {
237
			throw new \Aimeos\Controller\Jobs\Exception( 'Unable to create temporary sitemap file' );
238
		}
239
240
		if( fwrite( $file, $content ) === false ) {
241
			throw new \Aimeos\Controller\Jobs\Exception( 'Unable to write to temporary sitemap file' );
242
		}
243
244
		if( rewind( $file ) === false ) {
245
			throw new \Aimeos\Controller\Jobs\Exception( 'Unable to rewind temporary sitemap file' );
246
		}
247
248
		return $file;
249
	}
250
251
252
	/**
253
	 * Adds the content for the site map index file
254
	 *
255
	 * @param array $files List of generated site map file names
256
	 */
257
	protected function createSitemapIndex( array $files )
258
	{
259
		$context = $this->context();
260
		$config = $context->config();
261
		$view = $context->view();
262
263
		/** controller/jobs/catalog/export/sitemap/template-index
264
		 * Relative path to the XML site map index template of the catalog site map job controller.
265
		 *
266
		 * The template file contains the XML code and processing instructions
267
		 * to generate the site map index files. The configuration string is the path
268
		 * to the template file relative to the templates directory (usually in
269
		 * controller/jobs/templates).
270
		 *
271
		 * You can overwrite the template file configuration in extensions and
272
		 * provide alternative templates. These alternative templates should be
273
		 * named like the default one but with the string "standard" replaced by
274
		 * an unique name. You may use the name of your project for this. If
275
		 * you've implemented an alternative client class as well, "standard"
276
		 * should be replaced by the name of the new class.
277
		 *
278
		 * @param string Relative path to the template creating XML code for the site map index
279
		 * @since 2019.02
280
		 * @category Developer
281
		 * @see controller/jobs/catalog/export/sitemap/template-header
282
		 * @see controller/jobs/catalog/export/sitemap/template-items
283
		 * @see controller/jobs/catalog/export/sitemap/template-footer
284
		 */
285
		$tplconf = 'controller/jobs/catalog/export/sitemap/template-index';
286
287
		/** controller/jobs/catalog/export/sitemap/baseurl
288
		 * URL to the folder where the site maps can be accessed, without the filenames.
289
		 *
290
		 * The site maps must be publically available for download by the search
291
		 * engines. Individual site map files need a fully qualified URL in the index file.
292
		 *
293
		 * https://www.yourshop.com/your/sitemap/path/
294
		 *
295
		 * The location of the site map index file should then be
296
		 * added to the robots.txt in the document root of your domain:
297
		 *
298
		 * Sitemap: https://www.yourshop.com/your/sitemap/path/aimeos-catalog-sitemap-index.xml
299
		 *
300
		 * More details about site maps can be found at
301
		 * {@link http://www.sitemaps.org/protocol.html sitemaps.org}
302
		 *
303
		 * @param string Absolute URL
304
		 * @since 2019.06
305
		 * @category Developer
306
		 * @category User
307
		 * @see controller/jobs/catalog/export/sitemap/container/options
308
		 * @see controller/jobs/catalog/export/sitemap/max-items
309
		 * @see controller/jobs/catalog/export/sitemap/max-query
310
		 * @see controller/jobs/catalog/export/sitemap/changefreq
311
		 * @see controller/jobs/catalog/export/sitemap/location
312
		 */
313
		$baseUrl = $config->get( 'resource/fs/baseurl' );
314
315
		if( empty( $baseUrl ) )
316
		{
317
			$msg = sprintf( 'Required configuration for "%1$s" is missing', 'resource/fs/baseurl' );
318
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
319
		}
320
321
		$view->siteFiles = $files;
322
		$view->baseUrl = rtrim( $baseUrl, '/' ) . '/';
323
324
		$content = $view->render( $config->get( $tplconf, 'catalog/export/sitemap-index' ) );
325
		$context->fs()->write( $this->call( 'sitemapIndexFilename' ), $content );
326
	}
327
328
329
	/**
330
	 * Creates the sitemap files
331
	 *
332
	 * @param bool|null $default TRUE to use default criteria, NULL for relaxed criteria
333
	 * @return array List of temporary files
334
	 */
335
	protected function createSitemaps( ?bool $default = true ) : array
336
	{
337
		$config = $this->context()->config();
338
339
		/** controller/jobs/catalog/export/sitemap/domains
340
		 * List of associated items from other domains that should be fetched for the sitemap
341
		 *
342
		 * Catalogs consist not only of the base data but also of texts, media and
343
		 * other details. Those information is associated to the catalog via their lists.
344
		 * Using the "domains" option you can make more or less associated items available
345
		 * in the template.
346
		 *
347
		 * @param array List of domain names
348
		 * @since 2019.02
349
		 * @see controller/jobs/catalog/export/sitemap/max-items
350
		 */
351
		$domains = $config->get( 'controller/jobs/catalog/export/sitemap/domains', ['text'] );
352
353
		/** controller/jobs/catalog/export/sitemap/max-items
354
		 * Maximum number of categories per site map
355
		 *
356
		 * Each site map file must not contain more than 50,000 links and it's
357
		 * size must be less than 10MB. If your catalog URLs are rather long
358
		 * and one of your site map files is bigger than 10MB, you should set
359
		 * the number of categories per file to a smaller value until each file
360
		 * is less than 10MB.
361
		 *
362
		 * More details about site maps can be found at
363
		 * {@link http://www.sitemaps.org/protocol.html sitemaps.org}
364
		 *
365
		 * @param integer Number of categories per file
366
		 * @since 2019.02
367
		 * @see controller/jobs/catalog/export/sitemap/domains
368
		 */
369
		$maxItems = $config->get( 'controller/jobs/catalog/export/sitemap/max-items', 10000 );
370
371
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
372
373
		$search = $manager->filter( $default )->slice( 0, $maxItems );
374
		$cursor = $manager->cursor( $search );
375
376
		while( $items = $manager->iterate( $cursor, $domains ) ) {
377
			$files[] = $this->create( $items );
378
		}
379
380
		return $files;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $files does not seem to be defined for all execution paths leading up to this point.
Loading history...
381
	}
382
383
384
	/**
385
	 * Returns the available locale items for the current site
386
	 *
387
	 * @return \Aimeos\Map List of locale items
388
	 */
389
	protected function locales() : \Aimeos\Map
390
	{
391
		if( !isset( $this->locales ) )
392
		{
393
			$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
394
			$filter = $manager->filter()->add( ['locale.siteid' => $this->context()->locale()->getSiteId()] );
395
396
			$this->locales = $manager->search( $filter->order( 'locale.position' )->slice( 0, 10000 ) );
397
		}
398
399
		return $this->locales;
400
	}
401
402
403
	/**
404
	 * Returns the sitemap file name
405
	 *
406
	 * @param int $number Current file number
407
	 * @return string File name
408
	 */
409
	protected function sitemapFilename( int $number ) : string
410
	{
411
		return sprintf( 'aimeos-catalog-sitemap-%d.xml', $number );
412
	}
413
414
415
	/**
416
	 * Returns the file name of the sitemap index file
417
	 *
418
	 * @return string File name
419
	 */
420
	protected function sitemapIndexFilename() : string
421
	{
422
		return 'aimeos-catalog-sitemap-index.xml';
423
	}
424
}
425