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