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