Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

modules/FeedGenerator/FeedGenerator.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\FeedGenerator;
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
use function strip_tags;
11
12
/**
13
 * generate atom feeds from content
14
 *
15
 * @since 2.3.0
16
 *
17
 * @package Redaxscript
18
 * @category Modules
19
 * @author Henry Ruhs
20
 */
21
22
class FeedGenerator extends Module\Module
23
{
24
	/**
25
	 * array of the module
26
	 *
27
	 * @var array
28
	 */
29
30
	protected static $_moduleArray =
31
	[
32
		'name' => 'Feed Generator',
33
		'alias' => 'FeedGenerator',
34
		'author' => 'Redaxmedia',
35
		'description' => 'Generate Atom feeds from content',
36
		'version' => '4.0.0'
37
	];
38
39
	/**
40
	 * renderStart
41
	 *
42
	 * @since 2.3.0
43
	 */
44
45
	public function renderStart() : void
46
	{
47
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'feed-generator')
48
		{
49
			$this->_registry->set('renderBreak', true);
50
			if ($this->_registry->get('thirdParameter') === 'articles')
51
			{
52
				echo $this->render('articles');
53
			}
54
			if ($this->_registry->get('thirdParameter') === 'comments')
55
			{
56
				echo $this->render('comments');
57
			}
58
		}
59
	}
60
61
	/**
62
	 * render
63
	 *
64
	 * @since 2.3.0
65
	 *
66
	 * @param string $table name of the table
67
	 *
68
	 * @return string
69
	 */
70
71
	public function render(string $table = null) : string
72
	{
73
		/* query result */
74
75
		$resultArray[$table] = Db::forTablePrefix($table)
0 ignored issues
show
Coding Style Comprehensibility introduced by
$resultArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $resultArray = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
76
			->whereLanguageIs($this->_registry->get('language'))
77
			->whereNull('access')
78
			->where('status', 1)
79
			->orderBySetting('rank')
80
			->findMany();
81
82
		/* write xml */
83
84
		Header::contentType('application/atom+xml');
85
		return $this->_writeXML($resultArray);
86
	}
87
88
	/**
89
	 * @param array $resultArray
90
	 *
91
	 * @return string
92
	 */
93
94
	protected function _writeXML(array $resultArray = []) : string
95
	{
96
		$writer = new XMLWriter();
97
		$contentModel = new Model\Content();
98
		$settingModel = new Model\Setting();
99
		$dater = new Dater();
100
		$dater->init($this->_registry->get('now'));
101
		$root = $this->_registry->get('root');
102
		$parameterRoute = $this->_registry->get('parameterRoute');
103
104
		/* prepare href */
105
106
		$href = $root . '/' . $parameterRoute . $this->_registry->get('fullRoute');
107
		if ($this->_request->getQuery('l'))
108
		{
109
			$href .= $this->_registry->get('languageRoute') . $this->_registry->get('language');
110
		}
111
112
		/* write xml */
113
114
		$writer->openMemory();
115
		$writer->setIndent(true);
116
		$writer->setIndentString('	');
117
		$writer->startDocument('1.0', $settingModel->get('charset'));
118
		$writer->startElement('feed');
119
		$writer->writeAttribute('xmlns', 'http://www.w3.org/2005/Atom');
120
		$writer->startElement('link');
121
		$writer->writeAttribute('type', 'application/atom+xml');
122
		$writer->writeAttribute('href', $href);
123
		$writer->writeAttribute('rel', 'self');
124
		$writer->endElement();
125
		$writer->writeElement('id', $href);
126
		$writer->writeElement('title', $settingModel->get('title'));
127
		$writer->writeElement('updated', $dater->getDateTime()->format('c'));
128
		$writer->startElement('author');
129
		$writer->writeElement('name', $settingModel->get('author'));
130
		$writer->endElement();
131
132
		/* process result */
133
134
		foreach ($resultArray as $table => $result)
135
		{
136
			foreach ($result as $value)
137
			{
138
				$dater->init($value->date);
139
				$writer->startElement('entry');
140
				$writer->writeElement('id', $root . '/' . $parameterRoute . $contentModel->getRouteByTableAndId($table, $value->id));
141
				$writer->writeElement('title', $value->title);
142
				$writer->writeElement('updated', $dater->getDateTime()->format('c'));
143
				$writer->writeElement('content', strip_tags($value->text));
144
				$writer->endElement();
145
			}
146
		}
147
		$writer->endElement();
148
		$writer->endDocument();
149
		return $writer->outputMemory(true);
150
	}
151
}
152