24require_once __DIR__ .
'/Maintenance.php';
38 parent::__construct();
39 $this->
addDescription(
'Check syntax for all PHP files in MediaWiki' );
40 $this->
addOption(
'with-extensions',
'Also recurse the extensions folder' );
43 'Specific path (file or directory) to check, either with absolute path or '
44 .
'relative to the root of this MediaWiki installation',
50 'Text file containing list of files or directories to check',
56 'Check only files that were modified (requires Git command-line client)'
58 $this->
addOption(
'syntax-only',
'Check for syntax validity only, skip code style warnings' );
68 $this->
output(
"Checking syntax (using php -l, this can take a long time)\n" );
69 foreach ( $this->mFiles as $f ) {
71 if ( !$this->
hasOption(
'syntax-only' ) ) {
75 $this->
output(
"\nDone! " . count( $this->mFiles ) .
" files checked, " .
76 count( $this->mFailures ) .
" failures and " . count( $this->mWarnings ) .
77 " warnings found\n" );
86 $this->mIgnorePaths = [
89 $this->mNoStyleCheckPaths = [
92 "EmailPage/PHPMailer",
93 "FCKeditor/fckeditor/",
105 if ( !$this->
addPath( $path ) ) {
106 $this->
error(
"Error: can't find file or directory $path\n",
true );
110 } elseif ( $this->
hasOption(
'list-file' ) ) {
112 MediaWiki\suppressWarnings();
113 $f = fopen( $file,
'r' );
114 MediaWiki\restoreWarnings();
116 $this->
error(
"Can't open file $file\n",
true );
118 $path = trim( fgets( $f ) );
125 } elseif ( $this->
hasOption(
'modified' ) ) {
126 $this->
output(
"Retrieving list from Git... " );
128 $this->
output(
"done\n" );
129 foreach ( $files as $file ) {
131 $this->mFiles[] = $file;
138 $this->
output(
'Building file list...',
'listfiles' );
146 $IP .
'/maintenance',
149 if ( $this->
hasOption(
'with-extensions' ) ) {
150 $dirs[] = $IP .
'/extensions';
153 foreach (
$dirs as $d ) {
158 if ( file_exists(
"$IP/LocalSettings.php" ) ) {
159 $this->mFiles[] =
"$IP/LocalSettings.php";
162 $this->
output(
'done.',
'listfiles' );
173 if ( !is_dir(
"$path/.git" ) ) {
174 $this->
error(
"Error: Not a Git repository!\n",
true );
187 $cmd =
"cd $ePath && git merge-base master HEAD";
191 $this->
error(
"Error retrieving base SHA1 from Git!\n",
true );
196 $cmd =
"cd $ePath && git diff --name-only --diff-filter AM $eBase";
200 $this->
error(
"Error retrieving list from Git!\n",
true );
203 $wgMaxShellMemory = $oldMaxShellMemory;
206 $filename = strtok(
$output,
"\n" );
207 while ( $filename !==
false ) {
208 if ( $filename !==
'' ) {
209 $arr[] =
"$path/$filename";
211 $filename = strtok(
"\n" );
223 $file = str_replace(
'\\',
'/', $file );
224 $ext = pathinfo( $file, PATHINFO_EXTENSION );
225 if (
$ext !=
'php' &&
$ext !=
'inc' &&
$ext !=
'php5' ) {
228 foreach ( $this->mIgnorePaths as $regex ) {
230 if ( preg_match(
"~{$regex}~", $file, $m ) ) {
255 if ( is_dir( $path ) ) {
257 } elseif ( file_exists( $path ) ) {
258 $this->mFiles[] =
$path;
272 $iterator =
new RecursiveIteratorIterator(
273 new RecursiveDirectoryIterator(
$dir ),
274 RecursiveIteratorIterator::SELF_FIRST
276 foreach ( $iterator as $file ) {
278 $this->mFiles[] = $file->getRealPath();
290 if ( strpos(
$res,
'No syntax errors detected' ) ===
false ) {
291 $this->mFailures[$file] =
$res;
307 foreach ( $this->mNoStyleCheckPaths as $regex ) {
309 if ( preg_match(
"~{$regex}~", $file, $m ) ) {
314 $text = file_get_contents( $file );
315 $tokens = token_get_all( $text );
318 $this->
checkRegex( $file, $text,
'/^[\s\r\n]+<\?/',
'leading whitespace' );
319 $this->
checkRegex( $file, $text,
'/\?>[\s\r\n]*$/',
'trailing ?>' );
320 $this->
checkRegex( $file, $text,
'/^[\xFF\xFE\xEF]/',
'byte-order mark' );
324 if ( !preg_match( $regex, $text ) ) {
328 if ( !isset( $this->mWarnings[$file] ) ) {
329 $this->mWarnings[$file] = [];
331 $this->mWarnings[$file][] = $desc;
332 $this->
output(
"Warning in file $file: $desc found.\n" );
336 if ( !in_array( $evilToken,
$tokens ) ) {
340 if ( !isset( $this->mWarnings[$file] ) ) {
341 $this->mWarnings[$file] = [];
343 $this->mWarnings[$file][] = $desc;
344 $this->
output(
"Warning in file $file: $desc found.\n" );
$wgMaxShellMemory
Maximum amount of virtual memory available to shell processes under linux, in KB.
wfShellExec( $cmd, &$retval=null, $environ=[], $limits=[], $options=[])
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
wfEscapeShellArg()
Version of escapeshellarg() that works better on Windows.
Maintenance script to check syntax of all PHP files in MediaWiki.
getGitModifiedFiles( $path)
Returns a list of tracked files in a Git work tree differing from the master branch.
addFileOrDir( $path)
Add given file to file list, or, if it's a directory, add its content.
execute()
Do the actual work.
isSuitableFile( $file)
Returns true if $file is of a type we can check.
checkFileWithCli( $file)
Check a file for syntax errors using php -l.
__construct()
Default constructor.
addPath( $path)
Add given path to file list, searching it in include path if needed.
checkEvilToken( $file, $tokens, $evilToken, $desc)
addDirectoryContent( $dir)
Add all suitable files in given directory or its subdirectories to the file list.
buildFileList()
Build the list of files we'll check for syntax errors.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
checkForMistakes( $file)
Check a file for non-fatal coding errors, such as byte-order marks in the beginning or pointless ?...
checkRegex( $file, $text, $regex, $desc)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
const DB_NONE
Constants for DB access type.
hasOption( $name)
Checks to see if a particular param exists.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
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 local account incomplete not yet checked for validity & $retval
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
require_once RUN_MAINTENANCE_IF_MAIN