MediaWiki  1.28.0
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 $runParsoid;
32  private $regex;
33 
34  private $articles = [];
35  private $requirements = [];
36  private $tests = [];
37 
38  public static function read( $file, array $options = [] ) {
39  $reader = new self( $file, $options );
40  $reader->execute();
41 
42  $requirements = [];
43  foreach ( $reader->requirements as $type => $reqsOfType ) {
44  foreach ( $reqsOfType as $name => $unused ) {
45  $requirements[] = [
46  'type' => $type,
47  'name' => $name
48  ];
49  }
50  }
51 
52  return [
53  'requirements' => $requirements,
54  'tests' => $reader->tests,
55  'articles' => $reader->articles
56  ];
57  }
58 
59  private function __construct( $file, $options ) {
60  $this->file = $file;
61  $this->fh = fopen( $this->file, "rt" );
62 
63  if ( !$this->fh ) {
64  throw new MWException( "Couldn't open file '$file'\n" );
65  }
66 
67  $options = $options + [
68  'runDisabled' => false,
69  'runParsoid' => false,
70  'regex' => '//',
71  ];
72  $this->runDisabled = $options['runDisabled'];
73  $this->runParsoid = $options['runParsoid'];
74  $this->regex = $options['regex'];
75  }
76 
77  private function addCurrentTest() {
78  // "input" and "result" are old section names allowed
79  // for backwards-compatibility.
80  $input = $this->checkSection( [ 'wikitext', 'input' ], false );
81  $nonTidySection = $this->checkSection(
82  [ 'html/php', 'html/*', 'html', 'result' ], false );
83  // Some tests have "with tidy" and "without tidy" variants
84  $tidySection = $this->checkSection( [ 'html/php+tidy', 'html+tidy' ], false );
85 
86  // Remove trailing newline
87  $data = array_map( 'ParserTestRunner::chomp', $this->sectionData );
88 
89  // Apply defaults
90  $data += [
91  'options' => '',
92  'config' => ''
93  ];
94 
95  if ( $input === false ) {
96  throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
97  "lacks input section" );
98  }
99 
100  if ( preg_match( '/\\bdisabled\\b/i', $data['options'] ) && !$this->runDisabled ) {
101  // Disabled
102  return;
103  }
104 
105  if ( $tidySection === false && $nonTidySection === false ) {
106  if ( isset( $data['html/parsoid'] ) || isset( $data['wikitext/edited'] ) ) {
107  // Parsoid only
108  return;
109  } else {
110  throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
111  "lacks result section" );
112  }
113  }
114 
115  if ( preg_match( '/\\bparsoid\\b/i', $data['options'] ) && $nonTidySection === 'html'
116  && !$this->runParsoid
117  ) {
118  // A test which normally runs on Parsoid but can optionally be run with MW
119  return;
120  }
121 
122  if ( !preg_match( $this->regex, $data['test'] ) ) {
123  // Filtered test
124  return;
125  }
126 
127  $commonInfo = [
128  'test' => $data['test'],
129  'desc' => $data['test'],
130  'input' => $data[$input],
131  'options' => $data['options'],
132  'config' => $data['config'],
133  ];
134 
135  if ( $nonTidySection !== false ) {
136  // Add non-tidy test
137  $this->tests[] = [
138  'result' => $data[$nonTidySection],
139  ] + $commonInfo;
140 
141  if ( $tidySection !== false ) {
142  // Add tidy subtest
143  $this->tests[] = [
144  'desc' => $data['test'] . ' (with tidy)',
145  'result' => $data[$tidySection],
146  'options' => $data['options'] . ' tidy',
147  ] + $commonInfo;
148  }
149  } elseif ( $tidySection !== false ) {
150  // No need to override desc when there is no subtest
151  $this->tests[] = [
152  'result' => $data[$tidySection],
153  'options' => $data['options'] . ' tidy'
154  ] + $commonInfo;
155  } else {
156  throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
157  "lacks result section" );
158  }
159  }
160 
161  private function execute() {
162  while ( false !== ( $line = fgets( $this->fh ) ) ) {
163  $this->lineNum++;
164  $matches = [];
165 
166  if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
167  $this->section = strtolower( $matches[1] );
168 
169  if ( $this->section == 'endarticle' ) {
170  $this->checkSection( 'text' );
171  $this->checkSection( 'article' );
172 
173  $this->addArticle(
174  ParserTestRunner::chomp( $this->sectionData['article'] ),
175  $this->sectionData['text'], $this->lineNum );
176 
177  $this->clearSection();
178 
179  continue;
180  }
181 
182  if ( $this->section == 'endhooks' ) {
183  $this->checkSection( 'hooks' );
184 
185  foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) {
186  $line = trim( $line );
187 
188  if ( $line ) {
189  $this->addRequirement( 'hook', $line );
190  }
191  }
192 
193  $this->clearSection();
194 
195  continue;
196  }
197 
198  if ( $this->section == 'endfunctionhooks' ) {
199  $this->checkSection( 'functionhooks' );
200 
201  foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) {
202  $line = trim( $line );
203 
204  if ( $line ) {
205  $this->addRequirement( 'functionHook', $line );
206  }
207  }
208 
209  $this->clearSection();
210 
211  continue;
212  }
213 
214  if ( $this->section == 'endtransparenthooks' ) {
215  $this->checkSection( 'transparenthooks' );
216 
217  foreach ( explode( "\n", $this->sectionData['transparenthooks'] ) as $line ) {
218  $line = trim( $line );
219 
220  if ( $line ) {
221  $this->addRequirement( 'transparentHook', $line );
222  }
223  }
224 
225  $this->clearSection();
226 
227  continue;
228  }
229 
230  if ( $this->section == 'end' ) {
231  $this->checkSection( 'test' );
232  $this->addCurrentTest();
233  $this->clearSection();
234  continue;
235  }
236 
237  if ( isset( $this->sectionData[$this->section] ) ) {
238  throw new MWException( "duplicate section '$this->section' "
239  . "at line {$this->lineNum} of $this->file\n" );
240  }
241 
242  $this->sectionLineNum[$this->section] = $this->lineNum;
243  $this->sectionData[$this->section] = '';
244 
245  continue;
246  }
247 
248  if ( $this->section ) {
249  $this->sectionData[$this->section] .= $line;
250  }
251  }
252  }
253 
257  private function clearSection() {
258  $this->sectionLineNum = [];
259  $this->sectionData = [];
260  $this->section = null;
261 
262  }
263 
277  private function checkSection( $tokens, $fatal = true ) {
278  if ( is_null( $this->section ) ) {
279  throw new MWException( __METHOD__ . " can not verify a null section!\n" );
280  }
281  if ( !is_array( $tokens ) ) {
282  $tokens = [ $tokens ];
283  }
284  if ( count( $tokens ) == 0 ) {
285  throw new MWException( __METHOD__ . " can not verify zero sections!\n" );
286  }
287 
288  $data = $this->sectionData;
289  $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
290  return isset( $data[$token] );
291  } );
292 
293  if ( count( $tokens ) == 0 ) {
294  if ( !$fatal ) {
295  return false;
296  }
297  throw new MWException( sprintf(
298  "'%s' without '%s' at line %s of %s\n",
299  $this->section,
300  implode( ',', $tokens ),
301  $this->lineNum,
302  $this->file
303  ) );
304  }
305  if ( count( $tokens ) > 1 ) {
306  throw new MWException( sprintf(
307  "'%s' with unexpected tokens '%s' at line %s of %s\n",
308  $this->section,
309  implode( ',', $tokens ),
310  $this->lineNum,
311  $this->file
312  ) );
313  }
314 
315  return array_values( $tokens )[0];
316  }
317 
318  private function addArticle( $name, $text, $line ) {
319  $this->articles[] = [
320  'name' => $name,
321  'text' => $text,
322  'line' => $line,
323  'file' => $this->file
324  ];
325  }
326 
327  private function addRequirement( $type, $name ) {
328  $this->requirements[$type][$name] = true;
329  }
330 }
331 
the array() calling protocol came about after MediaWiki 1.4rc1.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
__construct($file, $options)
static chomp($s)
Remove last character if it is a newline utility.
addRequirement($type, $name)
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:93
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1046
$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
checkSection($tokens, $fatal=true)
Verify the current section data has some value for the given token name(s) (first parameter)...
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
addArticle($name, $text, $line)
static read($file, array $options=[])
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 or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491
$matches
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300