Completed
Push — master ( e9b1ca...cbd317 )
by Henry
07:52
created

modules/SitemapXml/SitemapXml.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Modules\SitemapXml;
3
4
use Redaxscript\Dater;
5
use Redaxscript\Db;
6
use Redaxscript\Header;
7
use Redaxscript\Model;
8
use Redaxscript\Module;
9
use XMLWriter;
10
11
/**
12
 * submit xml sitemap to search engines
13
 *
14
 * @since 2.2.0
15
 *
16
 * @package Redaxscript
17
 * @category Modules
18
 * @author Henry Ruhs
19
 */
20
21
class SitemapXml extends Module\Module
22
{
23
	/**
24
	 * array of the module
25
	 *
26
	 * @var array
27
	 */
28
29
	protected static $_moduleArray =
30
	[
31
		'name' => 'Sitemap XML',
32
		'alias' => 'SitemapXml',
33
		'author' => 'Redaxmedia',
34
		'description' => 'Submit XML sitemap to search engines',
35
		'version' => '4.0.0'
36
	];
37
38
	/**
39
	 * renderStart
40
	 *
41
	 * @since 2.2.0
42
	 */
43
44
	public function renderStart()
45
	{
46
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'sitemap-xml')
47
		{
48
			$this->_registry->set('renderBreak', true);
0 ignored issues
show
true is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
			echo $this->render();
50
		}
51
	}
52
53
	/**
54
	 * render
55
	 *
56
	 * @since 2.5.0
57
	 *
58
	 * @return string
59
	 */
60
61
	public function render() : string
62
	{
63
		/* query categories */
64
65
		$categories = Db::forTablePrefix('categories')
66
			->whereNull('access')
67
			->where('status', 1)
68
			->orderByAsc('rank')
69
			->findMany();
70
71
		/* query articles */
72
73
		$articles = Db::forTablePrefix('articles')
74
			->whereNull('access')
75
			->where('status', 1)
76
			->orderByAsc('rank')
77
			->findMany();
78
79
		/* write xml */
80
81
		Header::contentType('application/xml');
82
		return $this->_writeXML($categories, $articles);
83
	}
84
85
	/**
86
	 * write xml
87
	 *
88
	 * @since 2.5.0
89
	 *
90
	 * @param object $categories
91
	 * @param object $articles
92
	 *
93
	 * @return string
94
	 */
95
96
	protected function _writeXML(object $categories = null, object $articles = null) : string
97
	{
98
		$writer = new XMLWriter();
99
		$categoryModel = new Model\Category();
100
		$articleModel = new Model\Article();
101
		$settingModel = new Model\Setting();
102
		$dater = new Dater();
103
		$root = $this->_registry->get('root');
104
		$parameterRoute = $this->_registry->get('parameterRoute');
105
106
		/* write xml */
107
108
		$writer->openMemory();
109
		$writer->setIndent(true);
110
		$writer->setIndentString('	');
111
		$writer->startDocument('1.0', $settingModel->get('charset'));
112
		$writer->startElement('urlset');
113
		$writer->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
114
		$writer->startElement('url');
115
		$writer->writeElement('loc', $root);
116
		$writer->endElement();
117
118
		/* process categories */
119
120
		foreach ($categories as $value)
121
		{
122
			$dater->init($value->date);
123
			$writer->startElement('url');
124
			$writer->writeElement('loc', $root . '/' . $parameterRoute . $categoryModel->getRouteById($value->id));
125
			$writer->writeElement('lastmod', $dater->getDateTime()->format('c'));
126
			$writer->endElement();
127
		}
128
129
		/* process articles */
130
131
		foreach ($articles as $value)
132
		{
133
			$dater->init($value->date);
134
			$writer->startElement('url');
135
			$writer->writeElement('loc', $root . '/' . $parameterRoute . $articleModel->getRouteById($value->id));
136
			$writer->writeElement('lastmod', $dater->getDateTime()->format('c'));
137
			$writer->endElement();
138
		}
139
		$writer->endElement();
140
		$writer->endDocument();
141
		return $writer->outputMemory(true);
142
	}
143
}
144