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