summaryrefslogtreecommitdiff
path: root/maintenance/checkSyntax.php
blob: dc8626dfea3b8311ec4b072edb9968d89e4fd422 (plain)
1
2
3
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
61
62
63
64
65
66
67
68
69
70
71
72
73
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
 * Check syntax of all PHP files in MediaWiki
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

require_once __DIR__ . '/Maintenance.php';

/**
 * Maintenance script to check syntax of all PHP files in MediaWiki.
 *
 * @ingroup Maintenance
 */
class CheckSyntax extends Maintenance {

	// List of files we're going to check
	private $mFiles = array(), $mFailures = array(), $mWarnings = array();
	private $mIgnorePaths = array(), $mNoStyleCheckPaths = array();

	public function __construct() {
		parent::__construct();
		$this->mDescription = "Check syntax for all PHP files in MediaWiki";
		$this->addOption( 'with-extensions', 'Also recurse the extensions folder' );
		$this->addOption( 'path', 'Specific path (file or directory) to check, either with absolute path or relative to the root of this MediaWiki installation',
			false, true );
		$this->addOption( 'list-file', 'Text file containing list of files or directories to check', false, true );
		$this->addOption( 'modified', 'Check only files that were modified (requires Git command-line client)' );
		$this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' );
	}

	public function getDbType() {
		return Maintenance::DB_NONE;
	}

	public function execute() {
		$this->buildFileList();

		// ParseKit is broken on PHP 5.3+, disabled until this is fixed
		$useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION, '5.3', '<' );

		$str = 'Checking syntax (using ' . ( $useParseKit ?
			'parsekit' : ' php -l, this can take a long time' ) . ")\n";
		$this->output( $str );
		foreach ( $this->mFiles as $f ) {
			if ( $useParseKit ) {
				$this->checkFileWithParsekit( $f );
			} else {
				$this->checkFileWithCli( $f );
			}
			if ( !$this->hasOption( 'syntax-only' ) ) {
				$this->checkForMistakes( $f );
			}
		}
		$this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
			count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) .
			" warnings found\n" );
	}

	/**
	 * Build the list of files we'll check for syntax errors
	 */
	private function buildFileList() {
		global $IP;

		$this->mIgnorePaths = array(
			// Compat stuff, explodes on PHP 5.3
			"includes/NamespaceCompat.php$",
			);

		$this->mNoStyleCheckPaths = array(
			// Third-party code we don't care about
			"/activemq_stomp/",
			"EmailPage/PHPMailer",
			"FCKeditor/fckeditor/",
			'\bphplot-',
			"/svggraph/",
			"\bjsmin.php$",
			"PEAR/File_Ogg/",
			"QPoll/Excel/",
			"/geshi/",
			"/smarty/",
			);

		if ( $this->hasOption( 'path' ) ) {
			$path = $this->getOption( 'path' );
			if ( !$this->addPath( $path ) ) {
				$this->error( "Error: can't find file or directory $path\n", true );
			}
			return; // process only this path
		} elseif ( $this->hasOption( 'list-file' ) ) {
			$file = $this->getOption( 'list-file' );
			wfSuppressWarnings();
			$f = fopen( $file, 'r' );
			wfRestoreWarnings();
			if ( !$f ) {
				$this->error( "Can't open file $file\n", true );
			}
			$path = trim( fgets( $f ) );
			while ( $path ) {
				$this->addPath( $path );
			}
			fclose( $f );
			return;
		} elseif ( $this->hasOption( 'modified' ) ) {
			$this->output( "Retrieving list from Git... " );
			$files = $this->getGitModifiedFiles( $IP );
			$this->output( "done\n" );
			foreach ( $files as $file ) {
				if ( $this->isSuitableFile( $file ) && !is_dir( $file ) ) {
					$this->mFiles[] = $file;
				}
			}
			return;
		}

		$this->output( 'Building file list...', 'listfiles' );

		// Only check files in these directories.
		// Don't just put $IP, because the recursive dir thingie goes into all subdirs
		$dirs = array(
			$IP . '/includes',
			$IP . '/mw-config',
			$IP . '/languages',
			$IP . '/maintenance',
			$IP . '/skins',
		);
		if ( $this->hasOption( 'with-extensions' ) ) {
			$dirs[] = $IP . '/extensions';
		}

		foreach ( $dirs as $d ) {
			$this->addDirectoryContent( $d );
		}

		// Manually add two user-editable files that are usually sources of problems
		if ( file_exists( "$IP/LocalSettings.php" ) ) {
			$this->mFiles[] = "$IP/LocalSettings.php";
		}
		if ( file_exists( "$IP/AdminSettings.php" ) ) {
			$this->mFiles[] = "$IP/AdminSettings.php";
		}

		$this->output( 'done.', 'listfiles' );
	}

	/**
	 * Returns a list of tracked files in a Git work tree differing from the master branch.
	 * @param $path string: Path to the repository
	 * @return array: Resulting list of changed files
	 */
	private function getGitModifiedFiles( $path ) {

		global $wgMaxShellMemory;

		if ( !is_dir( "$path/.git" ) ) {
			$this->error( "Error: Not a Git repository!\n", true );
		}

		// git diff eats memory.
		$oldMaxShellMemory = $wgMaxShellMemory;
		if ( $wgMaxShellMemory < 1024000 ) {
			$wgMaxShellMemory = 1024000;
		}

		$ePath = wfEscapeShellArg( $path );

		// Find an ancestor in common with master (rather than just using its HEAD)
		// to prevent files only modified there from showing up in the list.
		$cmd = "cd $ePath && git merge-base master HEAD";
		$retval = 0;
		$output = wfShellExec( $cmd, $retval );
		if ( $retval !== 0 ) {
			$this->error( "Error retrieving base SHA1 from Git!\n", true );
		}

		// Find files in the working tree that changed since then.
		$eBase = wfEscapeShellArg( rtrim( $output, "\n" ) );
		$cmd = "cd $ePath && git diff --name-only --diff-filter AM $eBase";
		$retval = 0;
		$output = wfShellExec( $cmd, $retval );
		if ( $retval !== 0 ) {
			$this->error( "Error retrieving list from Git!\n", true );
		}

		$wgMaxShellMemory = $oldMaxShellMemory;

		$arr = array();
		$filename = strtok( $output, "\n" );
		while ( $filename !== false ) {
			if ( $filename !== '' ) {
				$arr[] = "$path/$filename";
			}
			$filename = strtok( "\n" );
		}

		return $arr;
	}

	/**
	 * Returns true if $file is of a type we can check
	 * @param $file string
	 * @return bool
	 */
	private function isSuitableFile( $file ) {
		$file = str_replace( '\\', '/', $file );
		$ext = pathinfo( $file, PATHINFO_EXTENSION );
		if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' ) {
			return false;
		}
		foreach ( $this->mIgnorePaths as $regex ) {
			$m = array();
			if ( preg_match( "~{$regex}~", $file, $m ) ) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Add given path to file list, searching it in include path if needed
	 * @param $path string
	 * @return bool
	 */
	private function addPath( $path ) {
		global $IP;
		return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" );
	}

	/**
	 * Add given file to file list, or, if it's a directory, add its content
	 * @param $path string
	 * @return bool
	 */
	private function addFileOrDir( $path ) {
		if ( is_dir( $path ) ) {
			$this->addDirectoryContent( $path );
		} elseif ( file_exists( $path ) ) {
			$this->mFiles[] = $path;
		} else {
			return false;
		}
		return true;
	}

	/**
	 * Add all suitable files in given directory or its subdirectories to the file list
	 *
	 * @param $dir String: directory to process
	 */
	private function addDirectoryContent( $dir ) {
		$iterator = new RecursiveIteratorIterator(
			new RecursiveDirectoryIterator( $dir ),
			RecursiveIteratorIterator::SELF_FIRST
		);
		foreach ( $iterator as $file ) {
			if ( $this->isSuitableFile( $file->getRealPath() ) ) {
				$this->mFiles[] = $file->getRealPath();
			}
		}
	}

	/**
	 * Check a file for syntax errors using Parsekit. Shamelessly stolen
	 * from tools/lint.php by TimStarling
	 * @param $file String Path to a file to check for syntax errors
	 * @return boolean
	 */
	private function checkFileWithParsekit( $file ) {
		static $okErrors = array(
			'Redefining already defined constructor',
			'Assigning the return value of new by reference is deprecated',
		);
		$errors = array();
		parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
		$ret = true;
		if ( $errors ) {
			foreach ( $errors as $error ) {
				foreach ( $okErrors as $okError ) {
					if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
						continue 2;
					}
				}
				$ret = false;
				$this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
				$this->mFailures[$file] = $errors;
			}
		}
		return $ret;
	}

	/**
	 * Check a file for syntax errors using php -l
	 * @param $file String Path to a file to check for syntax errors
	 * @return boolean
	 */
	private function checkFileWithCli( $file ) {
		$res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
		if ( strpos( $res, 'No syntax errors detected' ) === false ) {
			$this->mFailures[$file] = $res;
			$this->output( $res . "\n" );
			return false;
		}
		return true;
	}

	/**
	 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
	 * or pointless ?> closing tags at the end.
	 *
	 * @param $file String String Path to a file to check for errors
	 * @return boolean
	 */
	private function checkForMistakes( $file ) {
		foreach ( $this->mNoStyleCheckPaths as $regex ) {
			$m = array();
			if ( preg_match( "~{$regex}~", $file, $m ) ) {
				return;
			}
		}

		$text = file_get_contents( $file );
		$tokens = token_get_all( $text );

		$this->checkEvilToken( $file, $tokens, '@', 'Error supression operator (@)' );
		$this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
		$this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
		$this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
	}

	private function checkRegex( $file, $text, $regex, $desc ) {
		if ( !preg_match( $regex, $text ) ) {
			return;
		}

		if ( !isset( $this->mWarnings[$file] ) ) {
			$this->mWarnings[$file] = array();
		}
		$this->mWarnings[$file][] = $desc;
		$this->output( "Warning in file $file: $desc found.\n" );
	}

	private function checkEvilToken( $file, $tokens, $evilToken, $desc ) {
		if ( !in_array( $evilToken, $tokens ) ) {
			return;
		}

		if ( !isset( $this->mWarnings[$file] ) ) {
			$this->mWarnings[$file] = array();
		}
		$this->mWarnings[$file][] = $desc;
		$this->output( "Warning in file $file: $desc found.\n" );
	}
}

$maintClass = "CheckSyntax";
require_once RUN_MAINTENANCE_IF_MAIN;