Revert r39949 "* Revert revert r39662 of my parser changes."
[lhc/web/wiklou.git] / includes / parser / Parser_DiffTest.php
1 <?php
2
3 /**
4 * @ingroup Parser
5 */
6 class Parser_DiffTest
7 {
8 var $parsers, $conf;
9 var $shortOutput = false;
10
11 var $dfUniqPrefix;
12
13 function __construct( $conf ) {
14 if ( !isset( $conf['parsers'] ) ) {
15 throw new MWException( __METHOD__ . ': no parsers specified' );
16 }
17 $this->conf = $conf;
18 $this->dtUniqPrefix = "\x7fUNIQ" . Parser::getRandomString();
19 }
20
21 function init() {
22 if ( !is_null( $this->parsers ) ) {
23 return;
24 }
25
26 global $wgHooks;
27 static $doneHook = false;
28 if ( !$doneHook ) {
29 $doneHook = true;
30 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
31 }
32 if ( isset( $this->conf['shortOutput'] ) ) {
33 $this->shortOutput = $this->conf['shortOutput'];
34 }
35
36 foreach ( $this->conf['parsers'] as $i => $parserConf ) {
37 if ( !is_array( $parserConf ) ) {
38 $class = $parserConf;
39 $parserConf = array( 'class' => $parserConf );
40 } else {
41 $class = $parserConf['class'];
42 }
43 $this->parsers[$i] = new $class( $parserConf );
44 }
45 }
46
47 function __call( $name, $args ) {
48 $this->init();
49 $results = array();
50 $mismatch = false;
51 $lastResult = null;
52 $first = true;
53 foreach ( $this->parsers as $i => $parser ) {
54 $currentResult = call_user_func_array( array( &$this->parsers[$i], $name ), $args );
55 if ( $first ) {
56 $first = false;
57 } else {
58 if ( is_object( $lastResult ) ) {
59 if ( $lastResult != $currentResult ) {
60 $mismatch = true;
61 }
62 } else {
63 if ( $lastResult !== $currentResult ) {
64 $mismatch = true;
65 }
66 }
67 }
68 $results[$i] = $currentResult;
69 $lastResult = $currentResult;
70 }
71 if ( $mismatch ) {
72 throw new MWException( "Parser_DiffTest: results mismatch on call to $name\n" .
73 'Arguments: ' . $this->formatArray( $args ) . "\n" .
74 'Results: ' . $this->formatArray( $results ) . "\n" );
75 }
76 return $lastResult;
77 }
78
79 function formatArray( $array ) {
80 if ( $this->shortOutput ) {
81 foreach ( $array as $key => $value ) {
82 if ( $value instanceof ParserOutput ) {
83 $array[$key] = "ParserOutput: {$value->getText()}";
84 }
85 }
86 }
87 return var_export( $array, true );
88 }
89
90 function setFunctionHook( $id, $callback, $flags = 0 ) {
91 $this->init();
92 foreach ( $this->parsers as $i => $parser ) {
93 $parser->setFunctionHook( $id, $callback, $flags );
94 }
95 }
96
97 function onClearState( &$parser ) {
98 // hack marker prefixes to get identical output
99 $parser->mUniqPrefix = $this->dtUniqPrefix;
100 return true;
101 }
102 }