New hook getOtherBlockLogLink, called in Special:IPBlockList to show links to block...
[mediawiki.git] / maintenance / syntaxChecker.php
blob90d1f878dcc9071435a71c3afbff45e6bc9c50d3
1 <?php
2 /**
3 * Check syntax of all PHP files in MediaWiki
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.
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.
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
20 * @ingroup Maintenance
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25 class SyntaxChecker extends Maintenance {
27 // List of files we're going to check
28 private $mFiles, $mFailures = array();
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' );
36 protected function getDbType() {
37 return Maintenance::DB_NONE;
40 public function execute() {
41 $this->output( "Building file list..." );
42 $this->buildFileList();
43 $this->output( "done.\n" );
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', '<' );
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 );
56 $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
57 count( $this->mFailures ) . " failures found\n" );
60 /**
61 * Build the list of files we'll check for syntax errors
63 private function buildFileList() {
64 global $IP;
66 // Only check files in these directories.
67 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
68 $dirs = array(
69 $IP . '/includes',
70 $IP . '/config',
71 $IP . '/languages',
72 $IP . '/maintenance',
73 $IP . '/skins',
75 if( $this->hasOption( 'with-extensions' ) ) {
76 $dirs[] = $IP . '/extensions';
79 foreach( $dirs as $d ) {
80 $iterator = new RecursiveIteratorIterator(
81 new RecursiveDirectoryIterator( $d ),
82 RecursiveIteratorIterator::SELF_FIRST
84 foreach ( $iterator as $file ) {
85 $ext = pathinfo( $file->getFilename(), PATHINFO_EXTENSION );
86 if ( $ext == 'php' || $ext == 'inc' || $ext == 'php5' ) {
87 $this->mFiles[] = $file->getRealPath();
93 /**
94 * Check a file for syntax errors using Parsekit. Shamelessly stolen
95 * from tools/lint.php by TimStarling
96 * @param $file String Path to a file to check for syntax errors
97 * @return boolean
99 private function checkFileWithParsekit( $file ) {
100 static $okErrors = array(
101 'Redefining already defined constructor',
102 'Assigning the return value of new by reference is deprecated',
104 $errors = array();
105 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
106 $ret = true;
107 if ( $errors ) {
108 foreach ( $errors as $error ) {
109 foreach ( $okErrors as $okError ) {
110 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
111 continue 2;
114 $ret = false;
115 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
116 $this->mFailures[$file] = $errors;
119 return $ret;
123 * Check a file for syntax errors using php -l
124 * @param $file String Path to a file to check for syntax errors
125 * @return boolean
127 private function checkFileWithCli( $file ) {
128 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
129 if( strpos( $res, 'No syntax errors detected' ) === false ) {
130 $this->mFailures[$file] = $res;
131 $this->output( $res . "\n" );
132 return false;
134 return true;
138 $maintClass = "SyntaxChecker";
139 require_once( DO_MAINTENANCE );