http://www.mediawiki.org/wiki/User:Catrope/Stub_threshold shows us people setting...
[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 if ( count( $results ) == 2 ) {
73 $resultsList = array();
74 foreach ( $this->parsers as $i => $parser ) {
75 $resultsList[] = var_export( $results[$i], true );
76 }
77 $diff = wfDiff( $resultsList[0], $resultsList[1] );
78 } else {
79 $diff = '[too many parsers]';
80 }
81 $msg = "Parser_DiffTest: results mismatch on call to $name\n";
82 if ( !$this->shortOutput ) {
83 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
84 }
85 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
86 "Diff: $diff\n";
87 throw new MWException( $msg );
88 }
89 return $lastResult;
90 }
91
92 function formatArray( $array ) {
93 if ( $this->shortOutput ) {
94 foreach ( $array as $key => $value ) {
95 if ( $value instanceof ParserOutput ) {
96 $array[$key] = "ParserOutput: {$value->getText()}";
97 }
98 }
99 }
100 return var_export( $array, true );
101 }
102
103 function setFunctionHook( $id, $callback, $flags = 0 ) {
104 $this->init();
105 foreach ( $this->parsers as $i => $parser ) {
106 $parser->setFunctionHook( $id, $callback, $flags );
107 }
108 }
109
110 function onClearState( &$parser ) {
111 // hack marker prefixes to get identical output
112 $parser->mUniqPrefix = $this->dtUniqPrefix;
113 return true;
114 }
115 }