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