[TASK] Streamline PHPDoc comment matches function/method signature
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / UpgradeAnalysis / DocumentationFile.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Install\UpgradeAnalysis;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\Registry;
20 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\PathUtility;
23
24 /**
25 * Provide information about documentation files
26 */
27 class DocumentationFile
28 {
29 /**
30 * @var Registry
31 */
32 protected $registry;
33
34 /**
35 * @var array Unified array of used tags
36 */
37 protected $tagsTotal = [];
38
39 /**
40 * all files handled in this Class need to reside inside the changelog dir
41 * this is a security measure to protect system files
42 *
43 * @var string
44 */
45 protected $changelogPath = '';
46
47 /**
48 * DocumentationFile constructor.
49 * @param Registry|null $registry
50 * @param string $changelogDir
51 */
52 public function __construct(Registry $registry = null, $changelogDir = '')
53 {
54 $this->registry = $registry;
55 if ($this->registry === null) {
56 $this->registry = new Registry();
57 }
58 $this->changelogPath = $changelogDir !== '' ? $changelogDir : realpath(ExtensionManagementUtility::extPath('core') . 'Documentation/Changelog');
59 $this->changelogPath = str_replace('\\', '/', $this->changelogPath);
60 }
61
62 /**
63 * Traverse given directory, select files
64 *
65 * @param string $path
66 * @return array file details of affected documentation files
67 * @throws \InvalidArgumentException
68 */
69 public function findDocumentationFiles(string $path): array
70 {
71 if (strcasecmp($path, $this->changelogPath) < 0 || strpos($path, $this->changelogPath) === false) {
72 throw new \InvalidArgumentException('the given path does not belong to the changelog dir. Aborting', 1485425530);
73 }
74
75 $documentationFiles = [];
76 $versionDirectories = scandir($path);
77
78 $fileInfo = pathinfo($path);
79 $absolutePath = str_replace('\\', '/', $fileInfo['dirname']) . '/' . $fileInfo['basename'];
80 foreach ($versionDirectories as $version) {
81 $directory = $absolutePath . '/' . $version;
82 $documentationFiles += $this->getDocumentationFilesForVersion($directory, $version);
83 }
84 $this->tagsTotal = $this->collectTagTotal($documentationFiles);
85
86 return $documentationFiles;
87 }
88
89 /**
90 * Get main information from a .rst file
91 *
92 * @param string $file Absolute path to documentation file
93 * @return array
94 */
95 public function getListEntry(string $file): array
96 {
97 if (strcasecmp($file, $this->changelogPath) < 0 || strpos($file, $this->changelogPath) === false) {
98 throw new \InvalidArgumentException('the given file does not belong to the changelog dir. Aborting', 1485425531);
99 }
100 $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
101 $headline = $this->extractHeadline($lines);
102 $entry['headline'] = $headline;
103 $entry['filepath'] = $file;
104 $entry['tags'] = $this->extractTags($lines);
105 $entry['class'] = 'default';
106 foreach ($entry['tags'] as $tag) {
107 if (strpos($tag, 'cat:') !== false) {
108 $entry['class'] = strtolower(substr($tag, 4));
109 }
110 }
111 $entry['tagList'] = implode(',', $entry['tags']);
112 $entry['content'] = file_get_contents($file);
113 $entry['parsedContent'] = $this->parseContent($entry['content']);
114 $entry['file_hash'] = md5($entry['content']);
115
116 return [md5($file) => $entry];
117 }
118
119 /**
120 * True if file should be considered
121 *
122 * @param array $fileInfo
123 * @return bool
124 */
125 protected function isRelevantFile(array $fileInfo): bool
126 {
127 $isRelevantFile = $fileInfo['extension'] === 'rst' && $fileInfo['filename'] !== 'Index';
128 // file might be ignored by users choice
129 if ($isRelevantFile && $this->isFileIgnoredByUsersChoice($fileInfo['basename'])) {
130 $isRelevantFile = false;
131 }
132
133 return $isRelevantFile;
134 }
135
136 /**
137 * Add tags from file
138 *
139 * @param array $file file content, each line is an array item
140 * @return array
141 */
142 protected function extractTags(array $file): array
143 {
144 $tags = $this->extractTagsFromFile($file);
145 // Headline starting with the category like Breaking, Important or Feature
146 $tags[] = $this->extractCategoryFromHeadline($file);
147 natcasesort($tags);
148
149 return $tags;
150 }
151
152 /**
153 * Files must contain an index entry, detailing any number of manual tags
154 * each of these tags is extracted and added to the general tag structure for the file
155 *
156 * @param array $file file content, each line is an array item
157 * @return array extracted tags
158 */
159 protected function extractTagsFromFile(array $file): array
160 {
161 foreach ($file as $line) {
162 if (strpos($line, '.. index::') === 0) {
163 $tagString = substr($line, strlen('.. index:: '));
164 return GeneralUtility::trimExplode(',', $tagString, true);
165 }
166 }
167
168 return [];
169 }
170
171 /**
172 * Files contain a headline (provided as input parameter,
173 * it starts with the category string.
174 * This will used as a tag
175 *
176 * @param array $lines
177 * @return string
178 */
179 protected function extractCategoryFromHeadline(array $lines): string
180 {
181 $headline = $this->extractHeadline($lines);
182 if (strpos($headline, ':') !== false) {
183 return 'cat:' . substr($headline, 0, strpos($headline, ':'));
184 }
185
186 return '';
187 }
188
189 /**
190 * Skip include line and markers, use the first line actually containing text
191 *
192 * @param array $lines
193 * @return string
194 */
195 protected function extractHeadline(array $lines): string
196 {
197 $index = 0;
198 while (strpos($lines[$index], '..') === 0 || strpos($lines[$index], '==') === 0) {
199 $index++;
200 }
201 return trim($lines[$index]);
202 }
203
204 /**
205 * True for real directories and a valid version
206 *
207 * @param string $versionDirectory
208 * @param string $version
209 * @return bool
210 */
211 protected function isRelevantDirectory(string $versionDirectory, string $version): bool
212 {
213 return is_dir($versionDirectory) && $version !== '.' && $version !== '..';
214 }
215
216 /**
217 * Handle a single directory
218 *
219 * @param string $docDirectory
220 * @param string $version
221 * @return array
222 */
223 protected function getDocumentationFilesForVersion(
224 string $docDirectory,
225 string $version
226 ): array {
227 $documentationFiles = [];
228 if ($this->isRelevantDirectory($docDirectory, $version)) {
229 $documentationFiles[$version] = [];
230 $absolutePath = str_replace('\\', '/', PathUtility::dirname($docDirectory)) . '/' . $version;
231 $rstFiles = scandir($docDirectory);
232 foreach ($rstFiles as $file) {
233 $fileInfo = pathinfo($file);
234 if ($this->isRelevantFile($fileInfo)) {
235 $filePath = $absolutePath . '/' . $fileInfo['basename'];
236 $documentationFiles[$version] += $this->getListEntry($filePath);
237 }
238 }
239 }
240
241 return $documentationFiles;
242 }
243
244 /**
245 * Merge tag list
246 *
247 * @param $documentationFiles
248 * @return array
249 */
250 protected function collectTagTotal($documentationFiles): array
251 {
252 $tags = [];
253 foreach ($documentationFiles as $versionArray) {
254 foreach ($versionArray as $fileArray) {
255 $tags = array_merge(array_unique($tags), $fileArray['tags']);
256 }
257 }
258
259 return array_unique($tags);
260 }
261
262 /**
263 * Return full tag list
264 *
265 * @return array
266 */
267 public function getTagsTotal(): array
268 {
269 return $this->tagsTotal;
270 }
271
272 /**
273 * whether that file has been removed from users view
274 *
275 * @param string $filename
276 * @return bool
277 */
278 protected function isFileIgnoredByUsersChoice(string $filename): bool
279 {
280 $isFileIgnoredByUsersChoice = false;
281
282 $ignoredFiles = $this->registry->get('upgradeAnalysisIgnoreFilter', 'ignoredDocumentationFiles');
283 if (is_array($ignoredFiles)) {
284 foreach ($ignoredFiles as $filePath) {
285 if ($filePath !== null && strlen($filePath) > 0) {
286 if (strpos($filePath, $filename) !== false) {
287 $isFileIgnoredByUsersChoice = true;
288 break;
289 }
290 }
291 }
292 }
293 return $isFileIgnoredByUsersChoice;
294 }
295
296 /**
297 * @param string $rstContent
298 *
299 * @return string
300 * @throws \InvalidArgumentException
301 */
302 protected function parseContent(string $rstContent): string
303 {
304 $content = htmlspecialchars($rstContent);
305 $content = preg_replace('/:issue:`([\d]*)`/', '<a href="https://forge.typo3.org/issues/\\1" target="_blank">\\1</a>', $content);
306 $content = preg_replace('/#([\d]*)/', '#<a href="https://forge.typo3.org/issues/\\1" target="_blank">\\1</a>', $content);
307 $content = preg_replace('/(\n([=]*)\n(.*)\n([=]*)\n)/', '', $content, 1);
308 $content = preg_replace('/.. index::(.*)/', '', $content);
309 $content = preg_replace('/.. include::(.*)/', '', $content);
310 return trim($content);
311 }
312 }