MediaWiki  1.28.0
findDeprecated.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 require_once __DIR__ . '/../vendor/autoload.php';
27 
31 class 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 
73  public function isHardDeprecated( PhpParser\Node $node ) {
74  if ( !$node->stmts ) {
75  return false;
76  }
77  foreach ( $node->stmts as $stmt ) {
78  if (
79  $stmt instanceof PhpParser\Node\Expr\FuncCall
80  && $stmt->name->toString() === 'wfDeprecated'
81  ) {
82  return true;
83  }
84  return false;
85  }
86  }
87 
88  public function enterNode( PhpParser\Node $node ) {
89  $retVal = parent::enterNode( $node );
90 
91  if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) {
92  $this->currentClass = $node->name;
93  }
94 
95  if ( $node instanceof PhpParser\Node\FunctionLike ) {
96  $docComment = $node->getDocComment();
97  if ( !$docComment ) {
98  return;
99  }
100  if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) {
101  return;
102  }
103  $version = $matches[1];
104 
105  if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) {
106  $name = $this->currentClass . '::' . $node->name;
107  } else {
108  $name = $node->name;
109  }
110 
111  $this->foundNodes[ $version ][] = [
112  'filename' => $node->filename,
113  'line' => $node->getLine(),
114  'name' => $name,
115  'hard' => $this->isHardDeprecated( $node ),
116  ];
117  }
118 
119  return $retVal;
120  }
121 }
122 
128  public function __construct() {
129  parent::__construct();
130  $this->addDescription( 'Find deprecated interfaces' );
131  }
132 
133  public function getFiles() {
134  global $IP;
135 
136  $files = new RecursiveDirectoryIterator( $IP . '/includes' );
137  $files = new RecursiveIteratorIterator( $files );
138  $files = new RegexIterator( $files, '/\.php$/' );
139  return iterator_to_array( $files, false );
140  }
141 
142  public function execute() {
143  global $IP;
144 
145  $files = $this->getFiles();
146  $chunkSize = ceil( count( $files ) / 72 );
147 
148  $parser = ( new PhpParser\ParserFactory )->create( PhpParser\ParserFactory::PREFER_PHP7 );
149  $traverser = new PhpParser\NodeTraverser;
150  $finder = new DeprecatedInterfaceFinder;
151  $traverser->addVisitor( $finder );
152 
153  $fileCount = count( $files );
154 
155  for ( $i = 0; $i < $fileCount; $i++ ) {
156  $file = $files[$i];
157  $code = file_get_contents( $file );
158 
159  if ( strpos( $code, '@deprecated' ) === -1 ) {
160  continue;
161  }
162 
163  $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) );
164  $nodes = $parser->parse( $code, [ 'throwOnError' => false ] );
165  $traverser->traverse( $nodes );
166 
167  if ( $i % $chunkSize === 0 ) {
168  $percentDone = 100 * $i / $fileCount;
169  fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone );
170  }
171  }
172 
173  fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' );
174 
175  // Colorize output if STDOUT is an interactive terminal.
176  if ( posix_isatty( STDOUT ) ) {
177  $versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n";
178  $entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n";
179  } else {
180  $versionFmt = "\n* Deprecated since %s:\n";
181  $entryFmt = " %s %s (%s:%d)\n";
182  }
183 
184  foreach ( $finder->getFoundNodes() as $version => $nodes ) {
185  printf( $versionFmt, $version );
186  foreach ( $nodes as $node ) {
187  printf(
188  $entryFmt,
189  $node['hard'] ? '+' : '-',
190  $node['name'],
191  $node['filename'],
192  $node['line']
193  );
194  }
195  }
196  printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" );
197  }
198 }
199 
200 $maintClass = 'FindDeprecated';
201 require_once RUN_MAINTENANCE_IF_MAIN;
$IP
Definition: WebStart.php:58
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$files
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static posix_isatty($fd)
Wrapper for posix_isatty() We default as considering stdin a tty (for nice readline methods) but trea...
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of create
Definition: hooks.txt:2491
magic word & $parser
Definition: hooks.txt:2487
enterNode(PhpParser\Node $node)
Maintenance task that recursively scans MediaWiki PHP files for deprecated functions and interfaces a...
addDescription($text)
Set the description text.
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
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:802
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
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 finds deprecated functions and methods.
A PHPParser node visitor that associates each node with its file name.
enterNode(PhpParser\Node $node)
$maintClass
$matches
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300