MediaWiki  REL1_31
ParserDiffTest.php
Go to the documentation of this file.
1 <?php
28  public $parsers;
29  public $conf;
30  public $shortOutput = false;
31 
32  public function __construct( $conf ) {
33  if ( !isset( $conf['parsers'] ) ) {
34  throw new MWException( __METHOD__ . ': no parsers specified' );
35  }
36  $this->conf = $conf;
37  }
38 
39  public function init() {
40  if ( !is_null( $this->parsers ) ) {
41  return;
42  }
43 
44  if ( isset( $this->conf['shortOutput'] ) ) {
45  $this->shortOutput = $this->conf['shortOutput'];
46  }
47 
48  foreach ( $this->conf['parsers'] as $i => $parserConf ) {
49  if ( !is_array( $parserConf ) ) {
50  $class = $parserConf;
51  $parserConf = [ 'class' => $parserConf ];
52  } else {
53  $class = $parserConf['class'];
54  }
55  $this->parsers[$i] = new $class( $parserConf );
56  }
57  }
58 
59  public function __call( $name, $args ) {
60  $this->init();
61  $results = [];
62  $mismatch = false;
63  $lastResult = null;
64  $first = true;
65  foreach ( $this->parsers as $i => $parser ) {
66  $currentResult = call_user_func_array( [ &$this->parsers[$i], $name ], $args );
67  if ( $first ) {
68  $first = false;
69  } else {
70  if ( is_object( $lastResult ) ) {
71  if ( $lastResult != $currentResult ) {
72  $mismatch = true;
73  }
74  } else {
75  if ( $lastResult !== $currentResult ) {
76  $mismatch = true;
77  }
78  }
79  }
80  $results[$i] = $currentResult;
81  $lastResult = $currentResult;
82  }
83  if ( $mismatch ) {
84  if ( count( $results ) == 2 ) {
85  $resultsList = [];
86  foreach ( $this->parsers as $i => $parser ) {
87  $resultsList[] = var_export( $results[$i], true );
88  }
89  $diff = wfDiff( $resultsList[0], $resultsList[1] );
90  } else {
91  $diff = '[too many parsers]';
92  }
93  $msg = "ParserDiffTest: results mismatch on call to $name\n";
94  if ( !$this->shortOutput ) {
95  $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
96  }
97  $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
98  "Diff: $diff\n";
99  throw new MWException( $msg );
100  }
101  return $lastResult;
102  }
103 
104  public function formatArray( $array ) {
105  if ( $this->shortOutput ) {
106  foreach ( $array as $key => $value ) {
107  if ( $value instanceof ParserOutput ) {
108  $array[$key] = "ParserOutput: {$value->getText()}";
109  }
110  }
111  }
112  return var_export( $array, true );
113  }
114 
115  public function setFunctionHook( $id, $callback, $flags = 0 ) {
116  $this->init();
117  foreach ( $this->parsers as $parser ) {
118  $parser->setFunctionHook( $id, $callback, $flags );
119  }
120  }
121 }
ParserDiffTest\setFunctionHook
setFunctionHook( $id, $callback, $flags=0)
Definition: ParserDiffTest.php:115
ParserOutput
Definition: ParserOutput.php:25
wfDiff
wfDiff( $before, $after, $params='-u')
Returns unified plain-text diff of two texts.
Definition: GlobalFunctions.php:2442
ParserDiffTest\formatArray
formatArray( $array)
Definition: ParserDiffTest.php:104
ParserDiffTest\$conf
$conf
Definition: ParserDiffTest.php:29
php
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:37
MWException
MediaWiki exception.
Definition: MWException.php:26
ParserDiffTest\$parsers
$parsers
Definition: ParserDiffTest.php:28
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:2603
$value
$value
Definition: styleTest.css.php:45
$args
if( $line===false) $args
Definition: cdb.php:64
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ParserDiffTest\__call
__call( $name, $args)
Definition: ParserDiffTest.php:59
ParserDiffTest
Definition: ParserDiffTest.php:27
as
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:22
ParserDiffTest\$shortOutput
$shortOutput
Definition: ParserDiffTest.php:30
ParserDiffTest\__construct
__construct( $conf)
Definition: ParserDiffTest.php:32
ParserDiffTest\init
init()
Definition: ParserDiffTest.php:39