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