MediaWiki  master
TestFileReader.php
Go to the documentation of this file.
1 <?php
23  private $file;
24  private $fh;
25  private $section = null;
27  private $sectionData = [];
28  private $sectionLineNum = [];
29  private $lineNum = 0;
30  private $runDisabled;
31  private $regex;
32 
33  private $articles = [];
34  private $requirements = [];
35  private $tests = [];
36 
37  public static function read( $file, array $options = [] ) {
38  $reader = new self( $file, $options );
39  $reader->execute();
40 
41  $requirements = [];
42  foreach ( $reader->requirements as $type => $reqsOfType ) {
43  foreach ( $reqsOfType as $name => $unused ) {
44  $requirements[] = [
45  'type' => $type,
46  'name' => $name
47  ];
48  }
49  }
50 
51  return [
52  'requirements' => $requirements,
53  'tests' => $reader->tests,
54  'articles' => $reader->articles
55  ];
56  }
57 
58  private function __construct( $file, $options ) {
59  $this->file = $file;
60  $this->fh = fopen( $this->file, "rt" );
61 
62  if ( !$this->fh ) {
63  throw new MWException( "Couldn't open file '$file'\n" );
64  }
65 
66  $options = $options + [
67  'runDisabled' => false,
68  'regex' => '//',
69  ];
70  $this->runDisabled = $options['runDisabled'];
71  $this->regex = $options['regex'];
72  }
73 
74  private function addCurrentTest() {
75  // "input" and "result" are old section names allowed
76  // for backwards-compatibility.
77  $input = $this->checkSection( [ 'wikitext', 'input' ], false );
78  $nonTidySection = $this->checkSection(
79  [ 'html/php', 'html/*', 'html', 'result' ], false );
80  // Some tests have "with tidy" and "without tidy" variants
81  $tidySection = $this->checkSection( [ 'html/php+tidy', 'html+tidy' ], false );
82 
83  // Remove trailing newline
84  $data = array_map( 'ParserTestRunner::chomp', $this->sectionData );
85 
86  // Apply defaults
87  $data += [
88  'options' => '',
89  'config' => ''
90  ];
91 
92  if ( $input === false ) {
93  throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
94  "lacks input section" );
95  }
96 
97  if ( preg_match( '/\\bdisabled\\b/i', $data['options'] ) && !$this->runDisabled ) {
98  // Disabled
99  return;
100  }
101 
102  if ( $tidySection === false && $nonTidySection === false ) {
103  if ( isset( $data['html/parsoid'] ) || isset( $data['wikitext/edited'] ) ) {
104  // Parsoid only
105  return;
106  } else {
107  throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
108  "lacks result section" );
109  }
110  }
111 
112  if ( !preg_match( $this->regex, $data['test'] ) ) {
113  // Filtered test
114  return;
115  }
116 
117  $commonInfo = [
118  'test' => $data['test'],
119  'desc' => $data['test'],
120  'input' => $data[$input],
121  'options' => $data['options'],
122  'config' => $data['config'],
123  'line' => $this->sectionLineNum['test'],
124  'file' => $this->file
125  ];
126 
127  if ( $nonTidySection !== false ) {
128  // Add non-tidy test
129  $this->tests[] = [
130  'result' => $data[$nonTidySection],
131  'resultSection' => $nonTidySection
132  ] + $commonInfo;
133 
134  if ( $tidySection !== false ) {
135  // Add tidy subtest
136  $this->tests[] = [
137  'desc' => $data['test'] . ' (with tidy)',
138  'result' => $data[$tidySection],
139  'resultSection' => $tidySection,
140  'options' => $data['options'] . ' tidy',
141  'isSubtest' => true,
142  ] + $commonInfo;
143  }
144  } elseif ( $tidySection !== false ) {
145  // No need to override desc when there is no subtest
146  $this->tests[] = [
147  'result' => $data[$tidySection],
148  'resultSection' => $tidySection,
149  'options' => $data['options'] . ' tidy'
150  ] + $commonInfo;
151  } else {
152  throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
153  "lacks result section" );
154  }
155  }
156 
157  private function execute() {
158  while ( ( $line = fgets( $this->fh ) ) !== false ) {
159  $this->lineNum++;
160  $matches = [];
161 
162  if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
163  $this->section = strtolower( $matches[1] );
164 
165  if ( $this->section == 'endarticle' ) {
166  $this->checkSection( 'text' );
167  $this->checkSection( 'article' );
168 
169  $this->addArticle(
170  ParserTestRunner::chomp( $this->sectionData['article'] ),
171  $this->sectionData['text'], $this->lineNum );
172 
173  $this->clearSection();
174 
175  continue;
176  }
177 
178  if ( $this->section == 'endhooks' ) {
179  $this->checkSection( 'hooks' );
180 
181  foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) {
182  $line = trim( $line );
183 
184  if ( $line ) {
185  $this->addRequirement( 'hook', $line );
186  }
187  }
188 
189  $this->clearSection();
190 
191  continue;
192  }
193 
194  if ( $this->section == 'endfunctionhooks' ) {
195  $this->checkSection( 'functionhooks' );
196 
197  foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) {
198  $line = trim( $line );
199 
200  if ( $line ) {
201  $this->addRequirement( 'functionHook', $line );
202  }
203  }
204 
205  $this->clearSection();
206 
207  continue;
208  }
209 
210  if ( $this->section == 'endtransparenthooks' ) {
211  $this->checkSection( 'transparenthooks' );
212 
213  foreach ( explode( "\n", $this->sectionData['transparenthooks'] ) as $line ) {
214  $line = trim( $line );
215 
216  if ( $line ) {
217  $this->addRequirement( 'transparentHook', $line );
218  }
219  }
220 
221  $this->clearSection();
222 
223  continue;
224  }
225 
226  if ( $this->section == 'end' ) {
227  $this->checkSection( 'test' );
228  $this->addCurrentTest();
229  $this->clearSection();
230  continue;
231  }
232 
233  if ( isset( $this->sectionData[$this->section] ) ) {
234  throw new MWException( "duplicate section '$this->section' "
235  . "at line {$this->lineNum} of $this->file\n" );
236  }
237 
238  $this->sectionLineNum[$this->section] = $this->lineNum;
239  $this->sectionData[$this->section] = '';
240 
241  continue;
242  }
243 
244  if ( $this->section ) {
245  $this->sectionData[$this->section] .= $line;
246  }
247  }
248  }
249 
253  private function clearSection() {
254  $this->sectionLineNum = [];
255  $this->sectionData = [];
256  $this->section = null;
257  }
258 
272  private function checkSection( $tokens, $fatal = true ) {
273  if ( is_null( $this->section ) ) {
274  throw new MWException( __METHOD__ . " can not verify a null section!\n" );
275  }
276  if ( !is_array( $tokens ) ) {
277  $tokens = [ $tokens ];
278  }
279  if ( count( $tokens ) == 0 ) {
280  throw new MWException( __METHOD__ . " can not verify zero sections!\n" );
281  }
282 
283  $data = $this->sectionData;
284  $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
285  return isset( $data[$token] );
286  } );
287 
288  if ( count( $tokens ) == 0 ) {
289  if ( !$fatal ) {
290  return false;
291  }
292  throw new MWException( sprintf(
293  "'%s' without '%s' at line %s of %s\n",
294  $this->section,
295  implode( ',', $tokens ),
296  $this->lineNum,
297  $this->file
298  ) );
299  }
300  if ( count( $tokens ) > 1 ) {
301  throw new MWException( sprintf(
302  "'%s' with unexpected tokens '%s' at line %s of %s\n",
303  $this->section,
304  implode( ',', $tokens ),
305  $this->lineNum,
306  $this->file
307  ) );
308  }
309 
310  return array_values( $tokens )[0];
311  }
312 
313  private function addArticle( $name, $text, $line ) {
314  $this->articles[] = [
315  'name' => $name,
316  'text' => $text,
317  'line' => $line,
318  'file' => $this->file
319  ];
320  }
321 
322  private function addRequirement( $type, $name ) {
323  $this->requirements[$type][$name] = true;
324  }
325 }
if(is_array( $mode)) switch( $mode) $input
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static read( $file, array $options=[])
checkSection( $tokens, $fatal=true)
Verify the current section data has some value for the given token name(s) (first parameter)...
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
addArticle( $name, $text, $line)
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1972
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
$tokens
clearSection()
Clear section name and its data.
$sectionData
String|null: current test section being analyzed.
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
$line
Definition: cdb.php:59
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
addRequirement( $type, $name)
static chomp( $s)
Remove last character if it is a newline.
__construct( $file, $options)
$matches