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, $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( 'file', 'Specific file to check, either with absolute path or relative to the root of this MediaWiki installation',
35 false, true);
36 }
37
38 protected function getDbType() {
39 return Maintenance::DB_NONE;
40 }
41
42 public function execute() {
43 $this->buildFileList();
44
45 // ParseKit is broken on PHP 5.3+, disabled until this is fixed
46 $useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION, '5.3', '<' );
47
48 $this->output( "Checking syntax (this can take a really long time)...\n\n" );
49 foreach( $this->mFiles as $f ) {
50 if( $useParseKit ) {
51 $this->checkFileWithParsekit( $f );
52 } else {
53 $this->checkFileWithCli( $f );
54 }
55 $this->checkForMistakes( $f );
56 }
57 $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
58 count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) .
59 " warnings found\n" );
60 }
61
62 /**
63 * Build the list of files we'll check for syntax errors
64 */
65 private function buildFileList() {
66 global $IP;
67
68 if ( $this->hasOption( 'file' ) ) {
69 $file = $this->getOption( 'file' );
70 if ( !file_exists( $file ) ) {
71 if ( file_exists( "$IP/$file" ) ) {
72 $file = "$IP/$file";
73 } else {
74 $this->error( "Error: couldn't open file $file.\n", true );
75 }
76 }
77 $this->mFiles[] = $file;
78 $this->output( "Checking file $file.\n" );
79 return; // process only this file
80 }
81
82 $this->output( "Building file list..." );
83
84 // Only check files in these directories.
85 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
86 $dirs = array(
87 $IP . '/includes',
88 $IP . '/config',
89 $IP . '/languages',
90 $IP . '/maintenance',
91 $IP . '/skins',
92 );
93 if( $this->hasOption( 'with-extensions' ) ) {
94 $dirs[] = $IP . '/extensions';
95 }
96
97 foreach( $dirs as $d ) {
98 $iterator = new RecursiveIteratorIterator(
99 new RecursiveDirectoryIterator( $d ),
100 RecursiveIteratorIterator::SELF_FIRST
101 );
102 foreach ( $iterator as $file ) {
103 $ext = pathinfo( $file->getFilename(), PATHINFO_EXTENSION );
104 if ( $ext == 'php' || $ext == 'inc' || $ext == 'php5' ) {
105 $this->mFiles[] = $file->getRealPath();
106 }
107 }
108 }
109
110 // Manually add two user-editable files that are usually sources of problems
111 if ( file_exists( "$IP/LocalSettings.php" ) ) {
112 $this->mFiles[] = "$IP/LocalSettings.php";
113 }
114 if ( file_exists( "$IP/AdminSettings.php" ) ) {
115 $this->mFiles[] = "$IP/AdminSettings.php";
116 }
117
118 $this->output( "done.\n" );
119 }
120
121 /**
122 * Check a file for syntax errors using Parsekit. Shamelessly stolen
123 * from tools/lint.php by TimStarling
124 * @param $file String Path to a file to check for syntax errors
125 * @return boolean
126 */
127 private function checkFileWithParsekit( $file ) {
128 static $okErrors = array(
129 'Redefining already defined constructor',
130 'Assigning the return value of new by reference is deprecated',
131 );
132 $errors = array();
133 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
134 $ret = true;
135 if ( $errors ) {
136 foreach ( $errors as $error ) {
137 foreach ( $okErrors as $okError ) {
138 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
139 continue 2;
140 }
141 }
142 $ret = false;
143 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
144 $this->mFailures[$file] = $errors;
145 }
146 }
147 return $ret;
148 }
149
150 /**
151 * Check a file for syntax errors using php -l
152 * @param $file String Path to a file to check for syntax errors
153 * @return boolean
154 */
155 private function checkFileWithCli( $file ) {
156 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
157 if( strpos( $res, 'No syntax errors detected' ) === false ) {
158 $this->mFailures[$file] = $res;
159 $this->output( $res . "\n" );
160 return false;
161 }
162 return true;
163 }
164
165 /**
166 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
167 * or pointless ?> closing tags at the end.
168 *
169 * @param $file String String Path to a file to check for errors
170 * @return boolean
171 */
172 private function checkForMistakes( $file ) {
173 $text = file_get_contents( $file );
174
175 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
176 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
177 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
178 }
179
180 private function checkRegex( $file, $text, $regex, $desc ) {
181 if ( !preg_match( $regex, $text ) ) {
182 return;
183 }
184
185 if ( !isset( $this->mWarnings[$file] ) ) {
186 $this->mWarnings[$file] = array();
187 }
188 $this->mWarnings[$file][] = $desc;
189 $this->output( "Warning in file $file: $desc found.\n" );
190 }
191 }
192
193 $maintClass = "SyntaxChecker";
194 require_once( DO_MAINTENANCE );
195