removed output-disable in dbms-output fetching procedure
[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 foreach ( $matches[1] as $file ) {
99 if ( self::isSuitableFile( $file ) && !is_dir( $file ) ) {
100 $this->mFiles[] = $file;
101 }
102 }
103 return;
104 }
105
106 $this->output( "Building file list..." );
107
108 // Only check files in these directories.
109 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
110 $dirs = array(
111 $IP . '/includes',
112 $IP . '/config',
113 $IP . '/languages',
114 $IP . '/maintenance',
115 $IP . '/skins',
116 );
117 if( $this->hasOption( 'with-extensions' ) ) {
118 $dirs[] = $IP . '/extensions';
119 }
120
121 foreach( $dirs as $d ) {
122 $this->addDirectoryContent( $d );
123 }
124
125 // Manually add two user-editable files that are usually sources of problems
126 if ( file_exists( "$IP/LocalSettings.php" ) ) {
127 $this->mFiles[] = "$IP/LocalSettings.php";
128 }
129 if ( file_exists( "$IP/AdminSettings.php" ) ) {
130 $this->mFiles[] = "$IP/AdminSettings.php";
131 }
132
133 $this->output( "done.\n" );
134 }
135
136 /**
137 * Returns true if $file is of a type we can check
138 */
139 private static function isSuitableFile( $file ) {
140 $ext = pathinfo( $file, PATHINFO_EXTENSION );
141 return $ext == 'php' || $ext == 'inc' || $ext == 'php5';
142 }
143
144 /**
145 * Add given path to file list, searching it in include path if needed
146 */
147 private function addPath( $path ) {
148 global $IP;
149 return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" );
150 }
151
152 /**
153 * Add given file to file list, or, if it's a directory, add its content
154 */
155 private function addFileOrDir( $path ) {
156 if ( is_dir( $path ) ) {
157 $this->addDirectoryContent( $path );
158 } elseif ( file_exists( $path ) ) {
159 $this->mFiles[] = $path;
160 } else {
161 return false;
162 }
163 return true;
164 }
165
166 /**
167 * Add all suitable files in given directory or its subdirectories to the file list
168 *
169 * @param $dir String: directory to process
170 */
171 private function addDirectoryContent( $dir ) {
172 $iterator = new RecursiveIteratorIterator(
173 new RecursiveDirectoryIterator( $dir ),
174 RecursiveIteratorIterator::SELF_FIRST
175 );
176 foreach ( $iterator as $file ) {
177 if ( self::isSuitableFile( $file->getRealPath() ) ) {
178 $this->mFiles[] = $file->getRealPath();
179 }
180 }
181 }
182
183 /**
184 * Check a file for syntax errors using Parsekit. Shamelessly stolen
185 * from tools/lint.php by TimStarling
186 * @param $file String Path to a file to check for syntax errors
187 * @return boolean
188 */
189 private function checkFileWithParsekit( $file ) {
190 static $okErrors = array(
191 'Redefining already defined constructor',
192 'Assigning the return value of new by reference is deprecated',
193 );
194 $errors = array();
195 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
196 $ret = true;
197 if ( $errors ) {
198 foreach ( $errors as $error ) {
199 foreach ( $okErrors as $okError ) {
200 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
201 continue 2;
202 }
203 }
204 $ret = false;
205 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
206 $this->mFailures[$file] = $errors;
207 }
208 }
209 return $ret;
210 }
211
212 /**
213 * Check a file for syntax errors using php -l
214 * @param $file String Path to a file to check for syntax errors
215 * @return boolean
216 */
217 private function checkFileWithCli( $file ) {
218 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
219 if( strpos( $res, 'No syntax errors detected' ) === false ) {
220 $this->mFailures[$file] = $res;
221 $this->output( $res . "\n" );
222 return false;
223 }
224 return true;
225 }
226
227 /**
228 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
229 * or pointless ?> closing tags at the end.
230 *
231 * @param $file String String Path to a file to check for errors
232 * @return boolean
233 */
234 private function checkForMistakes( $file ) {
235 $text = file_get_contents( $file );
236
237 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
238 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
239 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
240 }
241
242 private function checkRegex( $file, $text, $regex, $desc ) {
243 if ( !preg_match( $regex, $text ) ) {
244 return;
245 }
246
247 if ( !isset( $this->mWarnings[$file] ) ) {
248 $this->mWarnings[$file] = array();
249 }
250 $this->mWarnings[$file][] = $desc;
251 $this->output( "Warning in file $file: $desc found.\n" );
252 }
253 }
254
255 $maintClass = "SyntaxChecker";
256 require_once( DO_MAINTENANCE );
257