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