MediaWiki  master
postprocess-phan.php
Go to the documentation of this file.
1 <?php
2 
3 abstract class Suppressor {
8  abstract public function suppress( $input );
9 
16  protected function isSuppressed( array $source, $type, $lineno ) {
17  return $lineno > 0 && preg_match(
18  "|/\*\* @suppress {$type} |",
19  $source[$lineno - 1]
20  );
21  }
22 }
23 
24 class TextSuppressor extends Suppressor {
29  public function suppress( $input ) {
30  $hasErrors = false;
31  $errors = [];
32  foreach ( explode( "\n", $input ) as $error ) {
33  if ( empty( $error ) ) {
34  continue;
35  }
36  if ( !preg_match( '/^(.*):(\d+) (Phan\w+) (.*)$/', $error, $matches ) ) {
37  echo "Failed to parse line: $error\n";
38  continue;
39  }
40  list( $source, $file, $lineno, $type, $message ) = $matches;
41  $errors[$file][] = [
42  'orig' => $error,
43  // convert from 1 indexed to 0 indexed
44  'lineno' => $lineno - 1,
45  'type' => $type,
46  ];
47  }
48  foreach ( $errors as $file => $fileErrors ) {
49  $source = file( $file );
50  foreach ( $fileErrors as $error ) {
51  if ( !$this->isSuppressed( $source, $error['type'], $error['lineno'] ) ) {
52  echo $error['orig'], "\n";
53  $hasErrors = true;
54  }
55  }
56  }
57 
58  return $hasErrors;
59  }
60 }
61 
67  public function suppress( $input ) {
68  $dom = new DOMDocument();
69  $dom->loadXML( $input );
70  $hasErrors = false;
71  // DOMNodeList's are "live", convert to an array so it works as expected
72  $files = [];
73  foreach ( $dom->getElementsByTagName( 'file' ) as $file ) {
74  $files[] = $file;
75  }
76  foreach ( $files as $file ) {
77  $errors = [];
78  foreach ( $file->getElementsByTagName( 'error' ) as $error ) {
79  $errors[] = $error;
80  }
81  $source = file( $file->getAttribute( 'name' ) );
82  $fileHasErrors = false;
83  foreach ( $errors as $error ) {
84  $lineno = $error->getAttribute( 'line' ) - 1;
85  $type = $error->getAttribute( 'source' );
86  if ( $this->isSuppressed( $source, $type, $lineno ) ) {
87  $error->parentNode->removeChild( $error );
88  } else {
89  $fileHasErrors = true;
90  $hasErrors = true;
91  }
92  }
93  if ( !$fileHasErrors ) {
94  $file->parentNode->removeChild( $file );
95  }
96  }
97  echo $dom->saveXML();
98 
99  return $hasErrors;
100  }
101 }
102 
103 class NoopSuppressor extends Suppressor {
104  private $mode;
105 
106  public function __construct( $mode ) {
107  $this->mode = $mode;
108  }
109 
110  public function suppress( $input ) {
111  echo "Unsupported output mode: {$this->mode}\n$input";
112  return true;
113  }
114 }
115 
116 $opt = getopt( "m:", [ "output-mode:" ] );
117 // if provided multiple times getopt returns an array
118 if ( isset( $opt['m'] ) ) {
119  $mode = $opt['m'];
120 } elseif ( isset( $mode['output-mode'] ) ) {
121  $mode = $opt['output-mode'];
122 } else {
123  $mode = 'text';
124 }
125 if ( is_array( $mode ) ) {
126  // If an option is passed multiple times getopt returns an
127  // array. Just take the last one.
128  $mode = end( $mode );
129 }
130 
131 switch ( $mode ) {
132 case 'text':
133  $suppressor = new TextSuppressor();
134  break;
135 case 'checkstyle':
136  $suppressor = new CheckStyleSuppressor();
137  break;
138 default:
139  $suppressor = new NoopSuppressor( $mode );
140 }
141 
142 $input = file_get_contents( 'php://stdin' );
143 $hasErrors = $suppressor->suppress( $input );
144 
145 if ( $hasErrors ) {
146  exit( 1 );
147 }
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
if(is_array( $mode)) switch( $mode) $input
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
$source
isSuppressed(array $source, $type, $lineno)
suppress( $input)
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing we can concentrate it all in an extension file
Definition: hooks.txt:91
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
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
$matches