48bbee03bc8648fbea2a0db2adfcb115eb078eca
[lhc/web/wiklou.git] / maintenance / syntaxChecker.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 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class SyntaxChecker extends Maintenance {
26
27 // List of files we're going to check
28 private $mFiles = array(), $mFailures = array(), $mWarnings = array();
29
30 public function __construct() {
31 parent::__construct();
32 $this->mDescription = "Check syntax for all PHP files in MediaWiki";
33 $this->addOption( 'with-extensions', 'Also recurse the extensions folder' );
34 $this->addOption( 'path', 'Specific path (file or directory) to check, either with absolute path or relative to the root of this MediaWiki installation',
35 false, true);
36 $this->addOption( 'list-file', 'Text file containing list of files or directories to check', false, true);
37 $this->addOption( 'modified', 'Check only files that were modified (requires SVN command-line client)' );
38 $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' );
39 }
40
41 protected function getDbType() {
42 return Maintenance::DB_NONE;
43 }
44
45 public function execute() {
46 $this->buildFileList();
47
48 // ParseKit is broken on PHP 5.3+, disabled until this is fixed
49 $useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION, '5.3', '<' );
50
51 $this->output( "Checking syntax (this can take a really long time)...\n\n" );
52 foreach( $this->mFiles as $f ) {
53 if( $useParseKit ) {
54 $this->checkFileWithParsekit( $f );
55 } else {
56 $this->checkFileWithCli( $f );
57 }
58 if( !$this->hasOption( 'syntax-only' ) ) {
59 $this->checkForMistakes( $f );
60 }
61 }
62 $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
63 count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) .
64 " warnings found\n" );
65 }
66
67 /**
68 * Build the list of files we'll check for syntax errors
69 */
70 private function buildFileList() {
71 global $IP;
72
73 if ( $this->hasOption( 'path' ) ) {
74 $path = $this->getOption( 'path' );
75 if ( !$this->addPath( $path ) ) {
76 $this->error( "Error: can't find file or directory $path\n", true );
77 }
78 return; // process only this path
79 } elseif ( $this->hasOption( 'list-file' ) ) {
80 $file = $this->getOption( 'list-file' );
81 $f = @fopen( $file, 'r' );
82 if ( !$f ) {
83 $this->error( "Can't open file $file\n", true );
84 }
85 while( $path = trim( fgets( $f ) ) ) {
86 $this->addPath( $path );
87 }
88 fclose( $f );
89 return;
90 } elseif ( $this->hasOption( 'modified' ) ) {
91 $this->output( "Retrieving list from Subversion... " );
92 $parentDir = wfEscapeShellArg( dirname( __FILE__ ) . '/..' );
93 $output = wfShellExec( "svn status --ignore-externals $parentDir", $retval );
94 if ( $retval ) {
95 $this->error( "Error retrieving list from Subversion!\n", true );
96 } else {
97 $this->output( "done\n" );
98 }
99
100 preg_match_all( '/^\s*[AM]\s+(.*?)\r?$/m', $output, $matches );
101 foreach ( $matches[1] as $file ) {
102 if ( self::isSuitableFile( $file ) && !is_dir( $file ) ) {
103 $this->mFiles[] = $file;
104 }
105 }
106 return;
107 }
108
109 $this->output( "Building file list..." );
110
111 // Only check files in these directories.
112 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
113 $dirs = array(
114 $IP . '/includes',
115 $IP . '/config',
116 $IP . '/languages',
117 $IP . '/maintenance',
118 $IP . '/skins',
119 );
120 if( $this->hasOption( 'with-extensions' ) ) {
121 $dirs[] = $IP . '/extensions';
122 }
123
124 foreach( $dirs as $d ) {
125 $this->addDirectoryContent( $d );
126 }
127
128 // Manually add two user-editable files that are usually sources of problems
129 if ( file_exists( "$IP/LocalSettings.php" ) ) {
130 $this->mFiles[] = "$IP/LocalSettings.php";
131 }
132 if ( file_exists( "$IP/AdminSettings.php" ) ) {
133 $this->mFiles[] = "$IP/AdminSettings.php";
134 }
135
136 $this->output( "done.\n" );
137 }
138
139 /**
140 * Returns true if $file is of a type we can check
141 */
142 private static function isSuitableFile( $file ) {
143 $ext = pathinfo( $file, PATHINFO_EXTENSION );
144 return $ext == 'php' || $ext == 'inc' || $ext == 'php5';
145 }
146
147 /**
148 * Add given path to file list, searching it in include path if needed
149 */
150 private function addPath( $path ) {
151 global $IP;
152 return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" );
153 }
154
155 /**
156 * Add given file to file list, or, if it's a directory, add its content
157 */
158 private function addFileOrDir( $path ) {
159 if ( is_dir( $path ) ) {
160 $this->addDirectoryContent( $path );
161 } elseif ( file_exists( $path ) ) {
162 $this->mFiles[] = $path;
163 } else {
164 return false;
165 }
166 return true;
167 }
168
169 /**
170 * Add all suitable files in given directory or its subdirectories to the file list
171 *
172 * @param $dir String: directory to process
173 */
174 private function addDirectoryContent( $dir ) {
175 $iterator = new RecursiveIteratorIterator(
176 new RecursiveDirectoryIterator( $dir ),
177 RecursiveIteratorIterator::SELF_FIRST
178 );
179 foreach ( $iterator as $file ) {
180 if ( self::isSuitableFile( $file->getRealPath() ) ) {
181 $this->mFiles[] = $file->getRealPath();
182 }
183 }
184 }
185
186 /**
187 * Check a file for syntax errors using Parsekit. Shamelessly stolen
188 * from tools/lint.php by TimStarling
189 * @param $file String Path to a file to check for syntax errors
190 * @return boolean
191 */
192 private function checkFileWithParsekit( $file ) {
193 static $okErrors = array(
194 'Redefining already defined constructor',
195 'Assigning the return value of new by reference is deprecated',
196 );
197 $errors = array();
198 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
199 $ret = true;
200 if ( $errors ) {
201 foreach ( $errors as $error ) {
202 foreach ( $okErrors as $okError ) {
203 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
204 continue 2;
205 }
206 }
207 $ret = false;
208 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
209 $this->mFailures[$file] = $errors;
210 }
211 }
212 return $ret;
213 }
214
215 /**
216 * Check a file for syntax errors using php -l
217 * @param $file String Path to a file to check for syntax errors
218 * @return boolean
219 */
220 private function checkFileWithCli( $file ) {
221 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
222 if( strpos( $res, 'No syntax errors detected' ) === false ) {
223 $this->mFailures[$file] = $res;
224 $this->output( $res . "\n" );
225 return false;
226 }
227 return true;
228 }
229
230 /**
231 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
232 * or pointless ?> closing tags at the end.
233 *
234 * @param $file String String Path to a file to check for errors
235 * @return boolean
236 */
237 private function checkForMistakes( $file ) {
238 $text = file_get_contents( $file );
239
240 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
241 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
242 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
243 }
244
245 private function checkRegex( $file, $text, $regex, $desc ) {
246 if ( !preg_match( $regex, $text ) ) {
247 return;
248 }
249
250 if ( !isset( $this->mWarnings[$file] ) ) {
251 $this->mWarnings[$file] = array();
252 }
253 $this->mWarnings[$file][] = $desc;
254 $this->output( "Warning in file $file: $desc found.\n" );
255 }
256 }
257
258 $maintClass = "SyntaxChecker";
259 require_once( DO_MAINTENANCE );
260