[BUGFIX] Hide internal prefix "cat:" in tags of upgrade docs
[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
23 /**
24 * Provide information about documentation files
25 */
26 class DocumentationFile
27 {
28 /**
29 * @var Registry
30 */
31 protected $registry;
32
33 /**
34 * @var array Unified array of used tags
35 */
36 protected $tagsTotal = [];
37
38 /**
39 * all files handled in this Class need to reside inside the changelog dir
40 * this is a security measure to protect system files
41 *
42 * @var string
43 */
44 protected $changelogPath = '';
45
46 /**
47 * DocumentationFile constructor.
48 * @param Registry|null $registry
49 */
50 public function __construct(Registry $registry = null, $changelogDir = '')
51 {
52 $this->registry = $registry;
53 if ($this->registry === null) {
54 $this->registry = new Registry();
55 }
56 $this->changelogPath = $changelogDir !== '' ? $changelogDir : realpath(PATH_site . ExtensionManagementUtility::siteRelPath('core') . 'Documentation/Changelog');
57 $this->changelogPath = strtr($this->changelogPath, '\\', '/');
58 }
59
60 /**
61 * Traverse given directory, select files
62 *
63 * @param string $path
64 * @return array file details of affected documentation files
65 * @throws \InvalidArgumentException
66 */
67 public function findDocumentationFiles(string $path): array
68 {
69 if (strcasecmp($path, $this->changelogPath) < 0 || strpos($path, $this->changelogPath) === false) {
70 throw new \InvalidArgumentException('the given path does not belong to the changelog dir. Aborting', 1485425530);
71 }
72
73 $documentationFiles = [];
74 $versionDirectories = scandir($path);
75
76 $fileInfo = pathinfo($path);
77 $absolutePath = strtr($fileInfo['dirname'], '\\', '/') . '/' . $fileInfo['basename'];
78 foreach ($versionDirectories as $version) {
79 $directory = $absolutePath . '/' . $version;
80 $documentationFiles += $this->getDocumentationFilesForVersion($directory, $version);
81 }
82 $this->tagsTotal = $this->collectTagTotal($documentationFiles);
83
84 return $documentationFiles;
85 }
86
87 /**
88 * Get main information from a .rst file
89 *
90 * @param string $file
91 * @return array
92 */
93 public function getListEntry(string $file): array
94 {
95 if (strcasecmp($file, $this->changelogPath) < 0 || strpos($file, $this->changelogPath) === false) {
96 throw new \InvalidArgumentException('the given file does not belong to the changelog dir. Aborting', 1485425531);
97 }
98 $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
99 $headline = $this->extractHeadline($lines);
100 $entry['headline'] = $headline;
101 $entry['filepath'] = $file;
102 $entry['tags'] = $this->extractTags($lines);
103 $entry['class'] = 'default';
104 foreach ($entry['tags'] as $key => $tag) {
105 if (strpos($tag, 'cat:') === 0) {
106 $substr = substr($tag, 4);
107 $entry['class'] = strtolower($substr);
108 $entry['tags'][$key] = $substr;
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
148 return $tags;
149 }
150
151 /**
152 * Files must contain an index entry, detailing any number of manual tags
153 * each of these tags is extracted and added to the general tag structure for the file
154 *
155 * @param array $file file content, each line is an array item
156 * @return array extracted tags
157 */
158 protected function extractTagsFromFile(array $file): array
159 {
160 foreach ($file as $line) {
161 if (strpos($line, '.. index::') === 0) {
162 $tagString = substr($line, strlen('.. index:: '));
163 return GeneralUtility::trimExplode(',', $tagString, true);
164 }
165 }
166
167 return [];
168 }
169
170 /**
171 * Files contain a headline (provided as input parameter,
172 * it starts with the category string.
173 * This will used as a tag
174 *
175 * @param array $lines
176 * @return string
177 */
178 protected function extractCategoryFromHeadline(array $lines): string
179 {
180 $headline = $this->extractHeadline($lines);
181 if (strpos($headline, ':') !== false) {
182 return 'cat:' . substr($headline, 0, strpos($headline, ':'));
183 }
184
185 return '';
186 }
187
188 /**
189 * Skip include line and markers, use the first line actually containing text
190 *
191 * @param array $lines
192 * @return string
193 */
194 protected function extractHeadline(array $lines): string
195 {
196 $index = 0;
197 while (strpos($lines[$index], '..') === 0 || strpos($lines[$index], '==') === 0) {
198 $index++;
199 }
200 return trim($lines[$index]);
201 }
202
203 /**
204 * True for real directories and a valid version
205 *
206 * @param string $versionDirectory
207 * @param string $version
208 * @return bool
209 */
210 protected function isRelevantDirectory(string $versionDirectory, string $version): bool
211 {
212 return is_dir($versionDirectory) && $version !== '.' && $version !== '..';
213 }
214
215 /**
216 * Handle a single directory
217 *
218 * @param string $docDirectory
219 * @param string $version
220 * @return array
221 */
222 protected function getDocumentationFilesForVersion(
223 string $docDirectory,
224 string $version
225 ): array {
226 $documentationFiles = [];
227 if ($this->isRelevantDirectory($docDirectory, $version)) {
228 $documentationFiles[$version] = [];
229 $absolutePath = strtr(dirname($docDirectory), '\\', '/') . '/' . $version;
230 $rstFiles = scandir($docDirectory);
231 foreach ($rstFiles as $file) {
232 $fileInfo = pathinfo($file);
233 if ($this->isRelevantFile($fileInfo)) {
234 $filePath = $absolutePath . '/' . $fileInfo['basename'];
235 $documentationFiles[$version] += $this->getListEntry($filePath);
236 }
237 }
238 }
239
240 return $documentationFiles;
241 }
242
243 /**
244 * Merge tag list
245 *
246 * @param $documentationFiles
247 * @return array
248 */
249 protected function collectTagTotal($documentationFiles): array
250 {
251 $tags = [];
252 foreach ($documentationFiles as $versionArray) {
253 foreach ($versionArray as $fileArray) {
254 $tags = array_merge(array_unique($tags), $fileArray['tags']);
255 }
256 }
257
258 return array_unique($tags);
259 }
260
261 /**
262 * Return full tag list
263 *
264 * @return array
265 */
266 public function getTagsTotal(): array
267 {
268 return $this->tagsTotal;
269 }
270
271 /**
272 * whether that file has been removed from users view
273 *
274 * @param string $filename
275 * @return bool
276 */
277 protected function isFileIgnoredByUsersChoice(string $filename): bool
278 {
279 $isFileIgnoredByUsersChoice = false;
280
281 $ignoredFiles = $this->registry->get('upgradeAnalysisIgnoreFilter', 'ignoredDocumentationFiles');
282 if (is_array($ignoredFiles)) {
283 foreach ($ignoredFiles as $filePath) {
284 if ($filePath !== null && strlen($filePath) > 0) {
285 if (strpos($filePath, $filename) !== false) {
286 $isFileIgnoredByUsersChoice = true;
287 break;
288 }
289 }
290 }
291 }
292 return $isFileIgnoredByUsersChoice;
293 }
294
295 /**
296 * @param string $rstContent
297 *
298 * @return string
299 * @throws \InvalidArgumentException
300 */
301 protected function parseContent(string $rstContent): string
302 {
303 $content = htmlspecialchars($rstContent);
304 $content = preg_replace('/:issue:`([\d]*)`/', '<a href="https://forge.typo3.org/issues/\\1" target="_blank">\\1</a>', $content);
305 $content = preg_replace('/#([\d]*)/', '#<a href="https://forge.typo3.org/issues/\\1" target="_blank">\\1</a>', $content);
306 $content = preg_replace('/(\n([=]*)\n(.*)\n([=]*)\n)/', '', $content, 1);
307 $content = preg_replace('/.. index::(.*)/', '', $content);
308 $content = preg_replace('/.. include::(.*)/', '', $content);
309 return trim($content);
310 }
311 }