validateRstFiles.php 7.25 KB
Newer Older
1
2
#!/usr/bin/env php
<?php
3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

require __DIR__ . '/../../vendor/autoload.php';

if (PHP_SAPI !== 'cli') {
    die('Script must be called from command line.' . chr(10));
}

use Symfony\Component\Finder\Finder;

/**
 * Check ReST files for integrity. If errors are found, they will be
 * output on stdout and the program will exit with exit code 1.
 *
 * Optional arguments: -d <directory>
 *
 * By default, the standard path is used. You can override this for
 * testing purposes.
 */
class validateRstFiles
{
    /**
     * @var array
     */
    protected $messages;

    /**
     * @var bool
     */
    protected $isError;

    /**
     * @var string
     */
    protected $baseDir = 'typo3/sysext/core/Documentation/Changelog';

    public function __construct(string $dir = '')
    {
        if ($dir) {
            $this->baseDir = $dir;
        }
    }

    public function validate()
    {
61
        printf('Searching for rst snippets in ' . $this->baseDir . chr(10));
62
63
64
65
66
67
68
69
70
71
72

        $count = 0;
        $finder = $this->findFiles();
        foreach ($finder as $file) {
            $filename = (string)$file;
            $this->clearMessages();
            $fileContent = $file->getContents();
            $this->validateContent($fileContent);
            $a = explode(chr(10), trim($fileContent));
            $lastLine = array_pop($a);
            $this->validateLastLine($lastLine);
73
            $this->validateLastLineByFilename($filename, $lastLine);
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

            if ($this->isError) {
                $shortPath = substr($filename, strlen($this->baseDir));
                $shortPath = ltrim($shortPath, '/\\');
                $count++;
                printf(
                    '%-10s | %-12s | %-17s | %s ' . chr(10),
                    $this->messages['include']['title'],
                    $this->messages['reference']['title'],
                    $this->messages['index']['title'],
                    $shortPath
                );
                if ($this->messages['include']['message']) {
                    printf($this->messages['include']['message'] . chr(10));
                }
                if ($this->messages['reference']['message']) {
                    printf($this->messages['reference']['message'] . chr(10));
                }
                if ($this->messages['index']['message']) {
                    printf($this->messages['index']['message'] . chr(10));
                }
            }
        }

        if ($count > 0) {
            fwrite(STDERR, 'Found ' . $count . ' rst files with errors, check full log for details.' . chr(10));
            exit(1);
        }
        exit(0);
    }

    public function findFiles(): Finder
    {
        $finder = new Finder();
        $finder
            ->files()
            ->in($this->baseDir)
            ->name('/\.rst$/')
            ->notName('Index.rst')
            ->notName('Howto.rst');

        return $finder;
    }

    protected function clearMessages()
    {
        $this->messages = [
            'include' => [
                'title' => '',
                'message' => '',
            ],
            'reference' => [
                'title' => '',
                'message' => '',
            ],
            'index' => [
                'title' => '',
                'message' => '',
            ],
        ];

        $this->isError = false;
    }

    protected function validateContent(string $fileContent)
    {
        $checkFor = [
            [
                'type' => 'include',
                'regex' => '#^\\.\\. include:: \\.\\./\\.\\./Includes.txt#m',
                'title' => 'no include',
                'message' => 'insert \'.. include:: ../../Includes.txt\' in first line of the file',
            ],
            [
                'type' => 'reference',
                'regex' => '#^See :issue:`[0-9]{4,6}`#m',
                'title' => 'no reference',
                'message' => 'insert \'See :issue:`<issuenumber>`\' after headline',
            ],
        ];

        foreach ($checkFor as $values) {
            if (preg_match($values['regex'], $fileContent) !== 1) {
                $this->messages[$values['type']]['title'] = $values['title'];
                $this->messages[$values['type']]['message'] = $values['message'];
                $this->isError = true;
            }
        }
    }

    protected function validateLastLine(string $line)
    {
        $checkFor = [
            [
                'type' => 'index',
169
                'regex' => '#^\.\. index:: (?:(?:TypoScript|TSConfig|TCA|FlexForm|LocalConfiguration|Fluid|FAL|Database|JavaScript|PHP-API|Frontend|Backend|CLI|RTE|YAML|ext:[a-zA-Z_0-9]+)(?:,\\s|$))+#',
170
                'title' => 'no or wrong index',
171
                'message' => 'insert \'.. index:: <at least one valid keyword>\' at the last line of the file. See Build/Scripts/validateRstFiles.php for allowed keywords',
172
173
174
175
176
177
178
179
180
181
182
183
            ],
        ];

        foreach ($checkFor as $values) {
            if (preg_match($values['regex'], $line) !== 1) {
                $this->messages[$values['type']]['title'] = $values['title'];
                $this->messages[$values['type']]['message'] = $values['message'];
                $this->isError = true;
            }
        }
    }

184
    protected function validateLastLineByFilename(string $path, string $lastLine)
185
186
187
188
    {
        $checkFor = [
            [
                'type' => 'index',
189
190
191
192
193
194
195
196
                'regexIgnoreFilename' => '#'
                    . 'Changelog[\\\\/]'         // Ignore all Changelog files
                    . '(?:'                      // which are either
                    . '.+[\\\\/](?:Feature|Important)' // from any version but of type "Feature" or "Important"
                    . '|'                        // or
                    . '[78]'                     // from 7.x and 8.x (as there was no extension scanner back then)
                    . ')'
                    . '#',
197
                'regex' => '#^\.\. index:: .*[, ](?:Fully|Partially|Not)Scanned([, ]|$).*#',
198
                'title' => 'missing FullyScanned / PartiallyScanned / NotScanned tag',
199
                'message' => 'insert \'.. index:: <at least one valid keyword and either FullyScanned, PartiallyScanned or NotScanned>\' at the last line of the file. See Build/Scripts/validateRstFiles.php for allowed keywords',
200
201
202
203
            ],
        ];

        foreach ($checkFor as $values) {
204
            if (preg_match($values['regexIgnoreFilename'], $path) === 1) {
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
                continue;
            }
            if (preg_match($values['regex'], $lastLine) !== 1) {
                $this->messages[$values['type']]['title'] = $values['title'];
                $this->messages[$values['type']]['message'] = $values['message'];
                $this->isError = true;
            }
        }
    }
}

$dir = '';
$args = getopt('d:');
if (isset($args['d'])) {
    $dir = $args['d'];
}
$validator = new validateRstFiles($dir);
$validator->validate();