Merge "Pass phpcs-strict on maintenance/ (8/8)"
[lhc/web/wiklou.git] / maintenance / checkSyntax.php
1 <?php
2 /**
3 * Check syntax of all PHP files in MediaWiki
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script to check syntax of all PHP files in MediaWiki.
28 *
29 * @ingroup Maintenance
30 */
31 class CheckSyntax extends Maintenance {
32
33 // List of files we're going to check
34 private $mFiles = array(), $mFailures = array(), $mWarnings = array();
35 private $mIgnorePaths = array(), $mNoStyleCheckPaths = array();
36
37 public function __construct() {
38 parent::__construct();
39 $this->mDescription = "Check syntax for all PHP files in MediaWiki";
40 $this->addOption( 'with-extensions', 'Also recurse the extensions folder' );
41 $this->addOption(
42 'path',
43 'Specific path (file or directory) to check, either with absolute path or '
44 . 'relative to the root of this MediaWiki installation',
45 false,
46 true
47 );
48 $this->addOption(
49 'list-file',
50 'Text file containing list of files or directories to check',
51 false,
52 true
53 );
54 $this->addOption(
55 'modified',
56 'Check only files that were modified (requires Git command-line client)'
57 );
58 $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' );
59 }
60
61 public function getDbType() {
62 return Maintenance::DB_NONE;
63 }
64
65 public function execute() {
66 $this->buildFileList();
67
68 // ParseKit is broken on PHP 5.3+, disabled until this is fixed
69 $useParseKit = function_exists( 'parsekit_compile_file' )
70 && version_compare( PHP_VERSION, '5.3', '<' );
71
72 $str = 'Checking syntax (using ' . ( $useParseKit ?
73 'parsekit' : ' php -l, this can take a long time' ) . ")\n";
74 $this->output( $str );
75 foreach ( $this->mFiles as $f ) {
76 if ( $useParseKit ) {
77 $this->checkFileWithParsekit( $f );
78 } else {
79 $this->checkFileWithCli( $f );
80 }
81 if ( !$this->hasOption( 'syntax-only' ) ) {
82 $this->checkForMistakes( $f );
83 }
84 }
85 $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
86 count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) .
87 " warnings found\n" );
88 }
89
90 /**
91 * Build the list of files we'll check for syntax errors
92 */
93 private function buildFileList() {
94 global $IP;
95
96 $this->mIgnorePaths = array(
97 // Compat stuff, explodes on PHP 5.3
98 "includes/NamespaceCompat.php$",
99 );
100
101 $this->mNoStyleCheckPaths = array(
102 // Third-party code we don't care about
103 "/activemq_stomp/",
104 "EmailPage/PHPMailer",
105 "FCKeditor/fckeditor/",
106 '\bphplot-',
107 "/svggraph/",
108 "\bjsmin.php$",
109 "PEAR/File_Ogg/",
110 "QPoll/Excel/",
111 "/geshi/",
112 "/smarty/",
113 );
114
115 if ( $this->hasOption( 'path' ) ) {
116 $path = $this->getOption( 'path' );
117 if ( !$this->addPath( $path ) ) {
118 $this->error( "Error: can't find file or directory $path\n", true );
119 }
120 return; // process only this path
121 } elseif ( $this->hasOption( 'list-file' ) ) {
122 $file = $this->getOption( 'list-file' );
123 wfSuppressWarnings();
124 $f = fopen( $file, 'r' );
125 wfRestoreWarnings();
126 if ( !$f ) {
127 $this->error( "Can't open file $file\n", true );
128 }
129 $path = trim( fgets( $f ) );
130 while ( $path ) {
131 $this->addPath( $path );
132 }
133 fclose( $f );
134 return;
135 } elseif ( $this->hasOption( 'modified' ) ) {
136 $this->output( "Retrieving list from Git... " );
137 $files = $this->getGitModifiedFiles( $IP );
138 $this->output( "done\n" );
139 foreach ( $files as $file ) {
140 if ( $this->isSuitableFile( $file ) && !is_dir( $file ) ) {
141 $this->mFiles[] = $file;
142 }
143 }
144 return;
145 }
146
147 $this->output( 'Building file list...', 'listfiles' );
148
149 // Only check files in these directories.
150 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
151 $dirs = array(
152 $IP . '/includes',
153 $IP . '/mw-config',
154 $IP . '/languages',
155 $IP . '/maintenance',
156 $IP . '/skins',
157 );
158 if ( $this->hasOption( 'with-extensions' ) ) {
159 $dirs[] = $IP . '/extensions';
160 }
161
162 foreach ( $dirs as $d ) {
163 $this->addDirectoryContent( $d );
164 }
165
166 // Manually add two user-editable files that are usually sources of problems
167 if ( file_exists( "$IP/LocalSettings.php" ) ) {
168 $this->mFiles[] = "$IP/LocalSettings.php";
169 }
170
171 $this->output( 'done.', 'listfiles' );
172 }
173
174 /**
175 * Returns a list of tracked files in a Git work tree differing from the master branch.
176 * @param string $path Path to the repository
177 * @return array Resulting list of changed files
178 */
179 private function getGitModifiedFiles( $path ) {
180
181 global $wgMaxShellMemory;
182
183 if ( !is_dir( "$path/.git" ) ) {
184 $this->error( "Error: Not a Git repository!\n", true );
185 }
186
187 // git diff eats memory.
188 $oldMaxShellMemory = $wgMaxShellMemory;
189 if ( $wgMaxShellMemory < 1024000 ) {
190 $wgMaxShellMemory = 1024000;
191 }
192
193 $ePath = wfEscapeShellArg( $path );
194
195 // Find an ancestor in common with master (rather than just using its HEAD)
196 // to prevent files only modified there from showing up in the list.
197 $cmd = "cd $ePath && git merge-base master HEAD";
198 $retval = 0;
199 $output = wfShellExec( $cmd, $retval );
200 if ( $retval !== 0 ) {
201 $this->error( "Error retrieving base SHA1 from Git!\n", true );
202 }
203
204 // Find files in the working tree that changed since then.
205 $eBase = wfEscapeShellArg( rtrim( $output, "\n" ) );
206 $cmd = "cd $ePath && git diff --name-only --diff-filter AM $eBase";
207 $retval = 0;
208 $output = wfShellExec( $cmd, $retval );
209 if ( $retval !== 0 ) {
210 $this->error( "Error retrieving list from Git!\n", true );
211 }
212
213 $wgMaxShellMemory = $oldMaxShellMemory;
214
215 $arr = array();
216 $filename = strtok( $output, "\n" );
217 while ( $filename !== false ) {
218 if ( $filename !== '' ) {
219 $arr[] = "$path/$filename";
220 }
221 $filename = strtok( "\n" );
222 }
223
224 return $arr;
225 }
226
227 /**
228 * Returns true if $file is of a type we can check
229 * @param string $file
230 * @return bool
231 */
232 private function isSuitableFile( $file ) {
233 $file = str_replace( '\\', '/', $file );
234 $ext = pathinfo( $file, PATHINFO_EXTENSION );
235 if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' ) {
236 return false;
237 }
238 foreach ( $this->mIgnorePaths as $regex ) {
239 $m = array();
240 if ( preg_match( "~{$regex}~", $file, $m ) ) {
241 return false;
242 }
243 }
244 return true;
245 }
246
247 /**
248 * Add given path to file list, searching it in include path if needed
249 * @param string $path
250 * @return bool
251 */
252 private function addPath( $path ) {
253 global $IP;
254 return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" );
255 }
256
257 /**
258 * Add given file to file list, or, if it's a directory, add its content
259 * @param string $path
260 * @return bool
261 */
262 private function addFileOrDir( $path ) {
263 if ( is_dir( $path ) ) {
264 $this->addDirectoryContent( $path );
265 } elseif ( file_exists( $path ) ) {
266 $this->mFiles[] = $path;
267 } else {
268 return false;
269 }
270 return true;
271 }
272
273 /**
274 * Add all suitable files in given directory or its subdirectories to the file list
275 *
276 * @param string $dir Directory to process
277 */
278 private function addDirectoryContent( $dir ) {
279 $iterator = new RecursiveIteratorIterator(
280 new RecursiveDirectoryIterator( $dir ),
281 RecursiveIteratorIterator::SELF_FIRST
282 );
283 foreach ( $iterator as $file ) {
284 if ( $this->isSuitableFile( $file->getRealPath() ) ) {
285 $this->mFiles[] = $file->getRealPath();
286 }
287 }
288 }
289
290 /**
291 * Check a file for syntax errors using Parsekit. Shamelessly stolen
292 * from tools/lint.php by TimStarling
293 * @param string $file Path to a file to check for syntax errors
294 * @return bool
295 */
296 private function checkFileWithParsekit( $file ) {
297 static $okErrors = array(
298 'Redefining already defined constructor',
299 'Assigning the return value of new by reference is deprecated',
300 );
301 $errors = array();
302 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
303 $ret = true;
304 if ( $errors ) {
305 foreach ( $errors as $error ) {
306 foreach ( $okErrors as $okError ) {
307 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
308 continue 2;
309 }
310 }
311 $ret = false;
312 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
313 $this->mFailures[$file] = $errors;
314 }
315 }
316 return $ret;
317 }
318
319 /**
320 * Check a file for syntax errors using php -l
321 * @param string $file Path to a file to check for syntax errors
322 * @return bool
323 */
324 private function checkFileWithCli( $file ) {
325 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
326 if ( strpos( $res, 'No syntax errors detected' ) === false ) {
327 $this->mFailures[$file] = $res;
328 $this->output( $res . "\n" );
329 return false;
330 }
331 return true;
332 }
333
334 /**
335 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
336 * or pointless ?> closing tags at the end.
337 *
338 * @param string $file String Path to a file to check for errors
339 * @return bool
340 */
341 private function checkForMistakes( $file ) {
342 foreach ( $this->mNoStyleCheckPaths as $regex ) {
343 $m = array();
344 if ( preg_match( "~{$regex}~", $file, $m ) ) {
345 return;
346 }
347 }
348
349 $text = file_get_contents( $file );
350 $tokens = token_get_all( $text );
351
352 $this->checkEvilToken( $file, $tokens, '@', 'Error supression operator (@)' );
353 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
354 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
355 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
356 }
357
358 private function checkRegex( $file, $text, $regex, $desc ) {
359 if ( !preg_match( $regex, $text ) ) {
360 return;
361 }
362
363 if ( !isset( $this->mWarnings[$file] ) ) {
364 $this->mWarnings[$file] = array();
365 }
366 $this->mWarnings[$file][] = $desc;
367 $this->output( "Warning in file $file: $desc found.\n" );
368 }
369
370 private function checkEvilToken( $file, $tokens, $evilToken, $desc ) {
371 if ( !in_array( $evilToken, $tokens ) ) {
372 return;
373 }
374
375 if ( !isset( $this->mWarnings[$file] ) ) {
376 $this->mWarnings[$file] = array();
377 }
378 $this->mWarnings[$file][] = $desc;
379 $this->output( "Warning in file $file: $desc found.\n" );
380 }
381 }
382
383 $maintClass = "CheckSyntax";
384 require_once RUN_MAINTENANCE_IF_MAIN;