MediaWiki REL1_31
findDeprecated.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/Maintenance.php';
26require_once __DIR__ . '/../vendor/autoload.php';
27
31class FileAwareNodeVisitor extends PhpParser\NodeVisitorAbstract {
32 private $currentFile = null;
33
34 public function enterNode( PhpParser\Node $node ) {
35 $retVal = parent::enterNode( $node );
36 $node->filename = $this->currentFile;
37 return $retVal;
38 }
39
40 public function setCurrentFile( $filename ) {
41 $this->currentFile = $filename;
42 }
43
44 public function getCurrentFile() {
45 return $this->currentFile;
46 }
47}
48
53
54 private $currentClass = null;
55
56 private $foundNodes = [];
57
58 public function getFoundNodes() {
59 // Sort results by version, then by filename, then by name.
60 foreach ( $this->foundNodes as $version => &$nodes ) {
61 uasort( $nodes, function ( $a, $b ) {
62 return ( $a['filename'] . $a['name'] ) < ( $b['filename'] . $b['name'] ) ? -1 : 1;
63 } );
64 }
65 ksort( $this->foundNodes );
66 return $this->foundNodes;
67 }
68
75 public function isHardDeprecated( PhpParser\Node $node ) {
76 if ( !$node->stmts ) {
77 return false;
78 }
79 foreach ( $node->stmts as $stmt ) {
80 if (
81 $stmt instanceof PhpParser\Node\Expr\FuncCall
82 && $stmt->name->toString() === 'wfDeprecated'
83 ) {
84 return true;
85 }
86 return false;
87 }
88 }
89
90 public function enterNode( PhpParser\Node $node ) {
91 $retVal = parent::enterNode( $node );
92
93 if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) {
94 $this->currentClass = $node->name;
95 }
96
97 if ( $node instanceof PhpParser\Node\FunctionLike ) {
98 $docComment = $node->getDocComment();
99 if ( !$docComment ) {
100 return;
101 }
102 if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) {
103 return;
104 }
105 $version = $matches[1];
106
107 if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) {
108 $name = $this->currentClass . '::' . $node->name;
109 } else {
110 $name = $node->name;
111 }
112
113 $this->foundNodes[ $version ][] = [
114 'filename' => $node->filename,
115 'line' => $node->getLine(),
116 'name' => $name,
117 'hard' => $this->isHardDeprecated( $node ),
118 ];
119 }
120
121 return $retVal;
122 }
123}
124
130 public function __construct() {
131 parent::__construct();
132 $this->addDescription( 'Find deprecated interfaces' );
133 }
134
138 public function getFiles() {
139 global $IP;
140
141 $files = new RecursiveDirectoryIterator( $IP . '/includes' );
142 $files = new RecursiveIteratorIterator( $files );
143 $files = new RegexIterator( $files, '/\.php$/' );
144 return iterator_to_array( $files, false );
145 }
146
147 public function execute() {
148 global $IP;
149
150 $files = $this->getFiles();
151 $chunkSize = ceil( count( $files ) / 72 );
152
153 $parser = ( new PhpParser\ParserFactory )->create( PhpParser\ParserFactory::PREFER_PHP7 );
154 $traverser = new PhpParser\NodeTraverser;
155 $finder = new DeprecatedInterfaceFinder;
156 $traverser->addVisitor( $finder );
157
158 $fileCount = count( $files );
159
160 for ( $i = 0; $i < $fileCount; $i++ ) {
161 $file = $files[$i];
162 $code = file_get_contents( $file );
163
164 if ( strpos( $code, '@deprecated' ) === -1 ) {
165 continue;
166 }
167
168 $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) );
169 $nodes = $parser->parse( $code );
170 $traverser->traverse( $nodes );
171
172 if ( $i % $chunkSize === 0 ) {
173 $percentDone = 100 * $i / $fileCount;
174 fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone );
175 }
176 }
177
178 fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' );
179
180 // Colorize output if STDOUT is an interactive terminal.
181 if ( posix_isatty( STDOUT ) ) {
182 $versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n";
183 $entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n";
184 } else {
185 $versionFmt = "\n* Deprecated since %s:\n";
186 $entryFmt = " %s %s (%s:%d)\n";
187 }
188
189 foreach ( $finder->getFoundNodes() as $version => $nodes ) {
190 printf( $versionFmt, $version );
191 foreach ( $nodes as $node ) {
192 printf(
193 $entryFmt,
194 $node['hard'] ? '+' : '-',
195 $node['name'],
196 $node['filename'],
197 $node['line']
198 );
199 }
200 }
201 printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" );
202 }
203}
204
205$maintClass = FindDeprecated::class;
206require_once RUN_MAINTENANCE_IF_MAIN;
A PHPParser node visitor that finds deprecated functions and methods.
enterNode(PhpParser\Node $node)
isHardDeprecated(PhpParser\Node $node)
Check whether a function or method includes a call to wfDeprecated(), indicating that it is a hard-de...
A PHPParser node visitor that associates each node with its file name.
enterNode(PhpParser\Node $node)
Maintenance task that recursively scans MediaWiki PHP files for deprecated functions and interfaces a...
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
static posix_isatty( $fd)
Wrapper for posix_isatty() We default as considering stdin a tty (for nice readline methods) but trea...
addDescription( $text)
Set the description text.
do that in ParserLimitReportFormat instead $parser
Definition hooks.txt:2603
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:865
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
$IP
Definition update.php:3
$maintClass
require_once RUN_MAINTENANCE_IF_MAIN