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

Standard::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\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\Product\Export\Standard
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
	 * @category Developer
56
	 */
57
58
	/** controller/jobs/product/export/sitemap/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the product 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/product/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 2015.01
78
	 * @category Developer
79
	 * @see controller/jobs/common/decorators/default
80
	 * @see controller/jobs/product/export/sitemap/decorators/global
81
	 * @see controller/jobs/product/export/sitemap/decorators/local
82
	 */
83
84
	/** controller/jobs/product/export/sitemap/decorators/global
85
	 * Adds a list of globally available decorators only to the product 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/product/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 2015.01
102
	 * @category Developer
103
	 * @see controller/jobs/common/decorators/default
104
	 * @see controller/jobs/product/export/sitemap/decorators/excludes
105
	 * @see controller/jobs/product/export/sitemap/decorators/local
106
	 */
107
108
	/** controller/jobs/product/export/sitemap/decorators/local
109
	 * Adds a list of local decorators only to the product 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\Product\Export\Sitemap\Decorator\*") around the job
118
	 * controller.
119
	 *
120
	 *  controller/jobs/product/export/sitemap/decorators/local = array( 'decorator2' )
121
	 *
122
	 * This would add the decorator named "decorator2" defined by
123
	 * "\Aimeos\Controller\Jobs\Product\Export\Sitemap\Decorator\Decorator2"
124
	 * only to the job controller.
125
	 *
126
	 * @param array List of decorator names
127
	 * @since 2015.01
128
	 * @category Developer
129
	 * @see controller/jobs/common/decorators/default
130
	 * @see controller/jobs/product/export/sitemap/export/sitemap/decorators/excludes
131
	 * @see controller/jobs/product/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', 'Product 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 product 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/product/export/sitemap/hidden
170
		 * Export hidden products in site map
171
		 *
172
		 * The product site map contains no hidden products 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 products, FALSE if not
176
		 * @since 2022.01
177
		 * @see controller/jobs/product/export/sitemap/container/options
178
		 * @see controller/jobs/product/export/sitemap/location
179
		 * @see controller/jobs/product/export/sitemap/max-items
180
		 * @see controller/jobs/product/export/sitemap/max-query
181
		 * @see controller/jobs/product/export/sitemap/changefreq
182
		 */
183
		$hidden = $context->config()->get( 'controller/jobs/product/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 products
202
	 *
203
	 * @param \Aimeos\Map $items List of product items implementing \Aimeos\MShop\Product\Item\Iface
204
	 * @param resource File handle
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Jobs\Product\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/product/export/sitemap/template
209
		 * Relative path to the XML template of the product 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/product/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, 'product/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/product/export/sitemap/template-index
264
		 * Relative path to the XML site map index template of the product 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 2015.01
280
		 * @category Developer
281
		 * @see controller/jobs/product/export/sitemap/template-header
282
		 * @see controller/jobs/product/export/sitemap/template-items
283
		 * @see controller/jobs/product/export/sitemap/template-footer
284
		 */
285
		$tplconf = 'controller/jobs/product/export/sitemap/template-index';
286
		$default = 'product/export/sitemap-index-standard';
0 ignored issues
show
Unused Code introduced by
The assignment to $default is dead and can be removed.
Loading history...
287
288
		/** controller/jobs/product/export/sitemap/baseurl
289
		 * URL to the folder where the site maps can be accessed, without the filenames.
290
		 *
291
		 * The site maps must be publically available for download by the search
292
		 * engines. Individual site map files need a fully qualified URL in the index file.
293
		 *
294
		 * https://www.yourshop.com/your/sitemap/path/
295
		 *
296
		 * The location of the site map index file should then be
297
		 * added to the robots.txt in the document root of your domain:
298
		 *
299
		 * Sitemap: https://www.yourshop.com/your/sitemap/path/aimeos-sitemap-index.xml
300
		 *
301
		 * More details about site maps can be found at
302
		 * {@link http://www.sitemaps.org/protocol.html sitemaps.org}
303
		 *
304
		 * @param string Absolute URL
305
		 * @since 2019.06
306
		 * @category Developer
307
		 * @category User
308
		 * @see controller/jobs/product/export/sitemap/container/options
309
		 * @see controller/jobs/product/export/sitemap/max-items
310
		 * @see controller/jobs/product/export/sitemap/max-query
311
		 * @see controller/jobs/product/export/sitemap/changefreq
312
		 * @see controller/jobs/product/export/sitemap/location
313
		 */
314
		$baseUrl = $config->get( 'resource/fs/baseurl' );
315
316
		if( empty( $baseUrl ) )
317
		{
318
			$msg = sprintf( 'Required configuration for "%1$s" is missing', 'resource/fs/baseurl' );
319
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
320
		}
321
322
		$view->siteFiles = $files;
323
		$view->baseUrl = rtrim( $baseUrl, '/' ) . '/';
324
325
		$content = $view->render( $config->get( $tplconf, 'product/export/sitemap-index' ) );
326
		$context->fs()->write( $this->call( 'sitemapIndexFilename' ), $content );
327
	}
328
329
330
	/**
331
	 * Creates the sitemap files
332
	 *
333
	 * @param bool|null $default TRUE to use default criteria, NULL for relaxed criteria
334
	 * @return array List of temporary files
335
	 */
336
	protected function createSitemaps( ?bool $default = true ) : array
337
	{
338
		$config = $this->context()->config();
339
340
		/** controller/jobs/product/export/sitemap/domains
341
		 * List of associated items from other domains that should be fetched for the sitemap
342
		 *
343
		 * Catalogs consist not only of the base data but also of texts, media and
344
		 * other details. Those information is associated to the product via their lists.
345
		 * Using the "domains" option you can make more or less associated items available
346
		 * in the template.
347
		 *
348
		 * @param array List of domain names
349
		 * @since 2019.02
350
		 * @see controller/jobs/product/export/sitemap/max-items
351
		 */
352
		$domains = $config->get( 'controller/jobs/product/export/sitemap/domains', ['text'] );
353
354
		/** controller/jobs/product/export/sitemap/max-items
355
		 * Maximum number of categories per site map
356
		 *
357
		 * Each site map file must not contain more than 50,000 links and it's
358
		 * size must be less than 10MB. If your product URLs are rather long
359
		 * and one of your site map files is bigger than 10MB, you should set
360
		 * the number of categories per file to a smaller value until each file
361
		 * is less than 10MB.
362
		 *
363
		 * More details about site maps can be found at
364
		 * {@link http://www.sitemaps.org/protocol.html sitemaps.org}
365
		 *
366
		 * @param integer Number of categories per file
367
		 * @since 2019.02
368
		 * @see controller/jobs/product/export/sitemap/domains
369
		 */
370
		$maxItems = $config->get( 'controller/jobs/product/export/sitemap/max-items', 10000 );
371
372
		$manager = \Aimeos\MShop::create( $this->context(), 'index' );
373
374
		$search = $manager->filter( $default )->slice( 0, $maxItems );
375
		$search->add( $search->make( 'product:has', ['catalog'] ), '!=', null );
376
		$cursor = $manager->cursor( $search );
377
378
		while( $items = $manager->iterate( $cursor, $domains ) ) {
379
			$files[] = $this->create( $items );
380
		}
381
382
		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...
383
	}
384
385
386
	/**
387
	 * Returns the available locale items for the current site
388
	 *
389
	 * @return \Aimeos\Map List of locale items
390
	 */
391
	protected function locales() : \Aimeos\Map
392
	{
393
		if( !isset( $this->locales ) )
394
		{
395
			$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
396
			$filter = $manager->filter()->add( ['locale.siteid' => $this->context()->locale()->getSiteId()] );
397
398
			$this->locales = $manager->search( $filter->order( 'locale.position' )->slice( 0, 10000 ) );
399
		}
400
401
		return $this->locales;
402
	}
403
404
405
	/**
406
	 * Returns the sitemap file name
407
	 *
408
	 * @param int $number Current file number
409
	 * @return string File name
410
	 */
411
	protected function sitemapFilename( int $number ) : string
412
	{
413
		return sprintf( 'aimeos-sitemap-%d.xml', $number );
414
	}
415
416
417
	/**
418
	 * Returns the file name of the sitemap index file
419
	 *
420
	 * @return string File name
421
	 */
422
	protected function sitemapIndexFilename() : string
423
	{
424
		return 'aimeos-sitemap-index.xml';
425
	}
426
}
427