MediaWiki  1.23.13
checkSyntax.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class CheckSyntax extends Maintenance {
32 
33  // List of files we're going to check
34  private $mFiles = array(), $mFailures = array(), $mWarnings = array();
35  private $mIgnorePaths = array(), $mNoStyleCheckPaths = array();
36 
37  public function __construct() {
38  parent::__construct();
39  $this->mDescription = "Check syntax for all PHP files in MediaWiki";
40  $this->addOption( 'with-extensions', 'Also recurse the extensions folder' );
41  $this->addOption( 'path', 'Specific path (file or directory) to check, either with absolute path or relative to the root of this MediaWiki installation',
42  false, true );
43  $this->addOption( 'list-file', 'Text file containing list of files or directories to check', false, true );
44  $this->addOption( 'modified', 'Check only files that were modified (requires Git command-line client)' );
45  $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' );
46  }
47 
48  public function getDbType() {
49  return Maintenance::DB_NONE;
50  }
51 
52  public function execute() {
53  $this->buildFileList();
54 
55  // ParseKit is broken on PHP 5.3+, disabled until this is fixed
56  $useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION, '5.3', '<' );
57 
58  $str = 'Checking syntax (using ' . ( $useParseKit ?
59  'parsekit' : ' php -l, this can take a long time' ) . ")\n";
60  $this->output( $str );
61  foreach ( $this->mFiles as $f ) {
62  if ( $useParseKit ) {
63  $this->checkFileWithParsekit( $f );
64  } else {
65  $this->checkFileWithCli( $f );
66  }
67  if ( !$this->hasOption( 'syntax-only' ) ) {
68  $this->checkForMistakes( $f );
69  }
70  }
71  $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
72  count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) .
73  " warnings found\n" );
74  }
75 
79  private function buildFileList() {
80  global $IP;
81 
82  $this->mIgnorePaths = array(
83  // Compat stuff, explodes on PHP 5.3
84  "includes/NamespaceCompat.php$",
85  );
86 
87  $this->mNoStyleCheckPaths = array(
88  // Third-party code we don't care about
89  "/activemq_stomp/",
90  "EmailPage/PHPMailer",
91  "FCKeditor/fckeditor/",
92  '\bphplot-',
93  "/svggraph/",
94  "\bjsmin.php$",
95  "PEAR/File_Ogg/",
96  "QPoll/Excel/",
97  "/geshi/",
98  "/smarty/",
99  );
100 
101  if ( $this->hasOption( 'path' ) ) {
102  $path = $this->getOption( 'path' );
103  if ( !$this->addPath( $path ) ) {
104  $this->error( "Error: can't find file or directory $path\n", true );
105  }
106  return; // process only this path
107  } elseif ( $this->hasOption( 'list-file' ) ) {
108  $file = $this->getOption( 'list-file' );
110  $f = fopen( $file, 'r' );
112  if ( !$f ) {
113  $this->error( "Can't open file $file\n", true );
114  }
115  $path = trim( fgets( $f ) );
116  while ( $path ) {
117  $this->addPath( $path );
118  }
119  fclose( $f );
120  return;
121  } elseif ( $this->hasOption( 'modified' ) ) {
122  $this->output( "Retrieving list from Git... " );
123  $files = $this->getGitModifiedFiles( $IP );
124  $this->output( "done\n" );
125  foreach ( $files as $file ) {
126  if ( $this->isSuitableFile( $file ) && !is_dir( $file ) ) {
127  $this->mFiles[] = $file;
128  }
129  }
130  return;
131  }
132 
133  $this->output( 'Building file list...', 'listfiles' );
134 
135  // Only check files in these directories.
136  // Don't just put $IP, because the recursive dir thingie goes into all subdirs
137  $dirs = array(
138  $IP . '/includes',
139  $IP . '/mw-config',
140  $IP . '/languages',
141  $IP . '/maintenance',
142  $IP . '/skins',
143  );
144  if ( $this->hasOption( 'with-extensions' ) ) {
145  $dirs[] = $IP . '/extensions';
146  }
147 
148  foreach ( $dirs as $d ) {
149  $this->addDirectoryContent( $d );
150  }
151 
152  // Manually add two user-editable files that are usually sources of problems
153  if ( file_exists( "$IP/LocalSettings.php" ) ) {
154  $this->mFiles[] = "$IP/LocalSettings.php";
155  }
156 
157  $this->output( 'done.', 'listfiles' );
158  }
159 
165  private function getGitModifiedFiles( $path ) {
166 
167  global $wgMaxShellMemory;
168 
169  if ( !is_dir( "$path/.git" ) ) {
170  $this->error( "Error: Not a Git repository!\n", true );
171  }
172 
173  // git diff eats memory.
174  $oldMaxShellMemory = $wgMaxShellMemory;
175  if ( $wgMaxShellMemory < 1024000 ) {
176  $wgMaxShellMemory = 1024000;
177  }
178 
179  $ePath = wfEscapeShellArg( $path );
180 
181  // Find an ancestor in common with master (rather than just using its HEAD)
182  // to prevent files only modified there from showing up in the list.
183  $cmd = "cd $ePath && git merge-base master HEAD";
184  $retval = 0;
185  $output = wfShellExec( $cmd, $retval );
186  if ( $retval !== 0 ) {
187  $this->error( "Error retrieving base SHA1 from Git!\n", true );
188  }
189 
190  // Find files in the working tree that changed since then.
191  $eBase = wfEscapeShellArg( rtrim( $output, "\n" ) );
192  $cmd = "cd $ePath && git diff --name-only --diff-filter AM $eBase";
193  $retval = 0;
194  $output = wfShellExec( $cmd, $retval );
195  if ( $retval !== 0 ) {
196  $this->error( "Error retrieving list from Git!\n", true );
197  }
198 
199  $wgMaxShellMemory = $oldMaxShellMemory;
200 
201  $arr = array();
202  $filename = strtok( $output, "\n" );
203  while ( $filename !== false ) {
204  if ( $filename !== '' ) {
205  $arr[] = "$path/$filename";
206  }
207  $filename = strtok( "\n" );
208  }
209 
210  return $arr;
211  }
212 
218  private function isSuitableFile( $file ) {
219  $file = str_replace( '\\', '/', $file );
220  $ext = pathinfo( $file, PATHINFO_EXTENSION );
221  if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' ) {
222  return false;
223  }
224  foreach ( $this->mIgnorePaths as $regex ) {
225  $m = array();
226  if ( preg_match( "~{$regex}~", $file, $m ) ) {
227  return false;
228  }
229  }
230  return true;
231  }
232 
238  private function addPath( $path ) {
239  global $IP;
240  return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" );
241  }
242 
248  private function addFileOrDir( $path ) {
249  if ( is_dir( $path ) ) {
250  $this->addDirectoryContent( $path );
251  } elseif ( file_exists( $path ) ) {
252  $this->mFiles[] = $path;
253  } else {
254  return false;
255  }
256  return true;
257  }
258 
264  private function addDirectoryContent( $dir ) {
265  $iterator = new RecursiveIteratorIterator(
266  new RecursiveDirectoryIterator( $dir ),
267  RecursiveIteratorIterator::SELF_FIRST
268  );
269  foreach ( $iterator as $file ) {
270  if ( $this->isSuitableFile( $file->getRealPath() ) ) {
271  $this->mFiles[] = $file->getRealPath();
272  }
273  }
274  }
275 
282  private function checkFileWithParsekit( $file ) {
283  static $okErrors = array(
284  'Redefining already defined constructor',
285  'Assigning the return value of new by reference is deprecated',
286  );
287  $errors = array();
288  parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
289  $ret = true;
290  if ( $errors ) {
291  foreach ( $errors as $error ) {
292  foreach ( $okErrors as $okError ) {
293  if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
294  continue 2;
295  }
296  }
297  $ret = false;
298  $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
299  $this->mFailures[$file] = $errors;
300  }
301  }
302  return $ret;
303  }
304 
310  private function checkFileWithCli( $file ) {
311  $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
312  if ( strpos( $res, 'No syntax errors detected' ) === false ) {
313  $this->mFailures[$file] = $res;
314  $this->output( $res . "\n" );
315  return false;
316  }
317  return true;
318  }
319 
327  private function checkForMistakes( $file ) {
328  foreach ( $this->mNoStyleCheckPaths as $regex ) {
329  $m = array();
330  if ( preg_match( "~{$regex}~", $file, $m ) ) {
331  return;
332  }
333  }
334 
335  $text = file_get_contents( $file );
336  $tokens = token_get_all( $text );
337 
338  $this->checkEvilToken( $file, $tokens, '@', 'Error supression operator (@)' );
339  $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
340  $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
341  $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
342  }
343 
344  private function checkRegex( $file, $text, $regex, $desc ) {
345  if ( !preg_match( $regex, $text ) ) {
346  return;
347  }
348 
349  if ( !isset( $this->mWarnings[$file] ) ) {
350  $this->mWarnings[$file] = array();
351  }
352  $this->mWarnings[$file][] = $desc;
353  $this->output( "Warning in file $file: $desc found.\n" );
354  }
355 
356  private function checkEvilToken( $file, $tokens, $evilToken, $desc ) {
357  if ( !in_array( $evilToken, $tokens ) ) {
358  return;
359  }
360 
361  if ( !isset( $this->mWarnings[$file] ) ) {
362  $this->mWarnings[$file] = array();
363  }
364  $this->mWarnings[$file][] = $desc;
365  $this->output( "Warning in file $file: $desc found.\n" );
366  }
367 }
368 
369 $maintClass = "CheckSyntax";
370 require_once RUN_MAINTENANCE_IF_MAIN;
wfShellExec
wfShellExec( $cmd, &$retval=null, $environ=array(), $limits=array(), $options=array())
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
Definition: GlobalFunctions.php:2851
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$files
$files
Definition: importImages.php:67
Maintenance\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: Maintenance.php:399
$f
$f
Definition: UtfNormalTest2.php:38
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1530
wfSuppressWarnings
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
Definition: GlobalFunctions.php:2434
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$maintClass
$maintClass
Definition: attachLatest.php:91
Maintenance::__construct
public function __construct()
Definition: maintenance.txt:41
wfRestoreWarnings
wfRestoreWarnings()
Restore error level to previous value.
Definition: GlobalFunctions.php:2464
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$dirs
$dirs
Definition: mergeMessageFileList.php:163
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfEscapeShellArg
wfEscapeShellArg()
Windows-compatible version of escapeshellarg() Windows doesn't recognise single-quotes in the shell,...
Definition: GlobalFunctions.php:2752
Maintenance\DB_NONE
const DB_NONE
Constants for DB access type.
Definition: Maintenance.php:57
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
$dir
if(count( $args)==0) $dir
Definition: importImages.php:49
$ext
$ext
Definition: NoLocalSettings.php:34
$output
& $output
Definition: hooks.txt:375
$path
$path
Definition: NoLocalSettings.php:35
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2578
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
Maintenance::execute
public function execute()
Definition: maintenance.txt:45
$IP
$IP
Definition: WebStart.php:88
$res
$res
Definition: database.txt:21
$retval
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account incomplete not yet checked for validity & $retval
Definition: hooks.txt:237