2015-12-14 20:07:17 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Maintenance script that recursively scans MediaWiki's PHP source tree
|
|
|
|
|
* for deprecated functions and methods and pretty-prints the results.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
2019-09-07 13:06:50 +00:00
|
|
|
* @phan-file-suppress PhanUndeclaredProperty Lots of custom properties
|
2015-12-14 20:07:17 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2015-12-14 20:07:17 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-12-14 20:07:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A PHPParser node visitor that associates each node with its file name.
|
|
|
|
|
*/
|
|
|
|
|
class FileAwareNodeVisitor extends PhpParser\NodeVisitorAbstract {
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var string|null */
|
2015-12-14 20:07:17 +00:00
|
|
|
private $currentFile = null;
|
|
|
|
|
|
|
|
|
|
public function enterNode( PhpParser\Node $node ) {
|
|
|
|
|
$retVal = parent::enterNode( $node );
|
|
|
|
|
$node->filename = $this->currentFile;
|
|
|
|
|
return $retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setCurrentFile( $filename ) {
|
|
|
|
|
$this->currentFile = $filename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCurrentFile() {
|
|
|
|
|
return $this->currentFile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A PHPParser node visitor that finds deprecated functions and methods.
|
|
|
|
|
*/
|
|
|
|
|
class DeprecatedInterfaceFinder extends FileAwareNodeVisitor {
|
|
|
|
|
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var string */
|
2015-12-14 20:07:17 +00:00
|
|
|
private $currentClass = null;
|
|
|
|
|
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var array[] */
|
2016-02-17 09:09:32 +00:00
|
|
|
private $foundNodes = [];
|
2015-12-14 20:07:17 +00:00
|
|
|
|
|
|
|
|
public function getFoundNodes() {
|
|
|
|
|
// Sort results by version, then by filename, then by name.
|
2022-09-21 19:05:03 +00:00
|
|
|
foreach ( $this->foundNodes as &$nodes ) {
|
2021-02-07 02:17:10 +00:00
|
|
|
uasort( $nodes, static function ( $a, $b ) {
|
Use PHP 7 '<=>' operator in 'sort()' callbacks
`$a <=> $b` returns `-1` if `$a` is lesser, `1` if `$b` is lesser,
and `0` if they are equal, which are exactly the values 'sort()'
callbacks are supposed to return.
It also enables the neat idiom `$a[x] <=> $b[x] ?: $a[y] <=> $b[y]`
to sort arrays of objects first by 'x', and by 'y' if they are equal.
* Replace a common pattern like `return $a < $b ? -1 : 1` with the
new operator (and similar patterns with the variables, the numbers
or the comparison inverted). Some of the uses were previously not
correctly handling the variables being equal; this is now
automatically fixed.
* Also replace `return $a - $b`, which is equivalent to `return
$a <=> $b` if both variables are integers but less intuitive.
* (Do not replace `return strcmp( $a, $b )`. It is also equivalent
when both variables are strings, but if any of the variables is not,
'strcmp()' converts it to a string before comparison, which could
give different results than '<=>', so changing this would require
careful review and isn't worth it.)
* Also replace `return $a > $b`, which presumably sort of works most
of the time (returns `1` if `$b` is lesser, and `0` if they are
equal or `$a` is lesser) but is erroneous.
Change-Id: I19a3d2fc8fcdb208c10330bd7a42c4e05d7f5cf3
2017-10-06 20:39:13 +00:00
|
|
|
return ( $a['filename'] . $a['name'] ) <=> ( $b['filename'] . $b['name'] );
|
2015-12-14 20:07:17 +00:00
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
ksort( $this->foundNodes );
|
|
|
|
|
return $this->foundNodes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether a function or method includes a call to wfDeprecated(),
|
|
|
|
|
* indicating that it is a hard-deprecated interface.
|
2017-09-09 20:47:04 +00:00
|
|
|
* @param PhpParser\Node $node
|
|
|
|
|
* @return bool
|
2015-12-14 20:07:17 +00:00
|
|
|
*/
|
|
|
|
|
public function isHardDeprecated( PhpParser\Node $node ) {
|
2016-08-15 06:00:27 +00:00
|
|
|
if ( !$node->stmts ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-14 20:07:17 +00:00
|
|
|
foreach ( $node->stmts as $stmt ) {
|
2024-09-16 10:56:21 +00:00
|
|
|
$functionExpression = null;
|
|
|
|
|
if ( $stmt instanceof PhpParser\Node\Expr\FuncCall ) {
|
|
|
|
|
$functionExpression = $stmt;
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $stmt->expr ) && $stmt->expr instanceof PhpParser\Node\Expr\FuncCall ) {
|
|
|
|
|
$functionExpression = $stmt->expr;
|
|
|
|
|
}
|
|
|
|
|
if ( $functionExpression && $functionExpression->name->toString() === 'wfDeprecated' ) {
|
2015-12-14 20:07:17 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function enterNode( PhpParser\Node $node ) {
|
|
|
|
|
$retVal = parent::enterNode( $node );
|
|
|
|
|
|
|
|
|
|
if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) {
|
|
|
|
|
$this->currentClass = $node->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $node instanceof PhpParser\Node\FunctionLike ) {
|
|
|
|
|
$docComment = $node->getDocComment();
|
|
|
|
|
if ( !$docComment ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$version = $matches[1];
|
|
|
|
|
|
|
|
|
|
if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) {
|
|
|
|
|
$name = $this->currentClass . '::' . $node->name;
|
|
|
|
|
} else {
|
|
|
|
|
$name = $node->name;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->foundNodes[ $version ][] = [
|
2015-12-14 20:07:17 +00:00
|
|
|
'filename' => $node->filename,
|
|
|
|
|
'line' => $node->getLine(),
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'hard' => $this->isHardDeprecated( $node ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-12-14 20:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $retVal;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maintenance task that recursively scans MediaWiki PHP files for deprecated
|
|
|
|
|
* functions and interfaces and produces a report.
|
|
|
|
|
*/
|
|
|
|
|
class FindDeprecated extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Find deprecated interfaces' );
|
2015-12-14 20:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-16 10:56:21 +00:00
|
|
|
/**
|
|
|
|
|
* @return string The installation path of MediaWiki. This method is mocked in PHPUnit tests.
|
|
|
|
|
*/
|
|
|
|
|
protected function getMwInstallPath() {
|
|
|
|
|
return MW_INSTALL_PATH;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-04 08:18:30 +00:00
|
|
|
/**
|
|
|
|
|
* @return SplFileInfo[]
|
|
|
|
|
*/
|
2015-12-14 20:07:17 +00:00
|
|
|
public function getFiles() {
|
2024-09-16 10:56:21 +00:00
|
|
|
$files = new RecursiveDirectoryIterator( $this->getMwInstallPath() . '/includes' );
|
2015-12-14 20:07:17 +00:00
|
|
|
$files = new RecursiveIteratorIterator( $files );
|
|
|
|
|
$files = new RegexIterator( $files, '/\.php$/' );
|
|
|
|
|
return iterator_to_array( $files, false );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
global $IP;
|
|
|
|
|
|
|
|
|
|
$files = $this->getFiles();
|
2022-03-05 20:05:01 +00:00
|
|
|
$chunkSize = (int)ceil( count( $files ) / 72 );
|
2015-12-14 20:07:17 +00:00
|
|
|
|
2024-11-11 00:16:49 +00:00
|
|
|
$parser = ( new PhpParser\ParserFactory )->createForVersion( PhpParser\PhpVersion::fromComponents( 7, 0 ) );
|
2015-12-14 20:07:17 +00:00
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
|
$finder = new DeprecatedInterfaceFinder;
|
|
|
|
|
$traverser->addVisitor( $finder );
|
|
|
|
|
|
|
|
|
|
$fileCount = count( $files );
|
|
|
|
|
|
2024-09-16 10:56:21 +00:00
|
|
|
$outputProgress = !defined( 'MW_PHPUNIT_TEST' );
|
|
|
|
|
|
2015-12-14 20:07:17 +00:00
|
|
|
for ( $i = 0; $i < $fileCount; $i++ ) {
|
|
|
|
|
$file = $files[$i];
|
|
|
|
|
$code = file_get_contents( $file );
|
|
|
|
|
|
2024-01-25 14:49:57 +00:00
|
|
|
if ( !str_contains( $code, '@deprecated' ) ) {
|
2015-12-14 20:07:17 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) );
|
2018-01-04 08:18:30 +00:00
|
|
|
$nodes = $parser->parse( $code );
|
2015-12-14 20:07:17 +00:00
|
|
|
$traverser->traverse( $nodes );
|
|
|
|
|
|
|
|
|
|
if ( $i % $chunkSize === 0 ) {
|
|
|
|
|
$percentDone = 100 * $i / $fileCount;
|
2024-09-16 10:56:21 +00:00
|
|
|
if ( $outputProgress ) {
|
|
|
|
|
fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone );
|
|
|
|
|
}
|
2015-12-14 20:07:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 10:56:21 +00:00
|
|
|
if ( $outputProgress ) {
|
|
|
|
|
fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' );
|
|
|
|
|
}
|
2015-12-14 20:07:17 +00:00
|
|
|
|
|
|
|
|
// Colorize output if STDOUT is an interactive terminal.
|
|
|
|
|
if ( posix_isatty( STDOUT ) ) {
|
|
|
|
|
$versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n";
|
|
|
|
|
$entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n";
|
|
|
|
|
} else {
|
|
|
|
|
$versionFmt = "\n* Deprecated since %s:\n";
|
|
|
|
|
$entryFmt = " %s %s (%s:%d)\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $finder->getFoundNodes() as $version => $nodes ) {
|
|
|
|
|
printf( $versionFmt, $version );
|
|
|
|
|
foreach ( $nodes as $node ) {
|
|
|
|
|
printf(
|
|
|
|
|
$entryFmt,
|
|
|
|
|
$node['hard'] ? '+' : '-',
|
|
|
|
|
$node['name'],
|
|
|
|
|
$node['filename'],
|
|
|
|
|
$node['line']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = FindDeprecated::class;
|
2015-12-14 20:07:17 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|