Some random URL protocol forcing for protocol-relative URLs
[lhc/web/wiklou.git] / includes / parser / Parser_DiffTest.php
1 <?php
2 /**
3 * Fake parser that output the difference of two different parsers
4 *
5 * @file
6 */
7
8 /**
9 * @ingroup Parser
10 */
11 class Parser_DiffTest
12 {
13 var $parsers, $conf;
14 var $shortOutput = false;
15
16 var $dtUniqPrefix;
17
18 function __construct( $conf ) {
19 if ( !isset( $conf['parsers'] ) ) {
20 throw new MWException( __METHOD__ . ': no parsers specified' );
21 }
22 $this->conf = $conf;
23 }
24
25 function init() {
26 if ( !is_null( $this->parsers ) ) {
27 return;
28 }
29
30 global $wgHooks;
31 static $doneHook = false;
32 if ( !$doneHook ) {
33 $doneHook = true;
34 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
35 }
36 if ( isset( $this->conf['shortOutput'] ) ) {
37 $this->shortOutput = $this->conf['shortOutput'];
38 }
39
40 foreach ( $this->conf['parsers'] as $i => $parserConf ) {
41 if ( !is_array( $parserConf ) ) {
42 $class = $parserConf;
43 $parserConf = array( 'class' => $parserConf );
44 } else {
45 $class = $parserConf['class'];
46 }
47 $this->parsers[$i] = new $class( $parserConf );
48 }
49 }
50
51 function __call( $name, $args ) {
52 $this->init();
53 $results = array();
54 $mismatch = false;
55 $lastResult = null;
56 $first = true;
57 foreach ( $this->parsers as $i => $parser ) {
58 $currentResult = call_user_func_array( array( &$this->parsers[$i], $name ), $args );
59 if ( $first ) {
60 $first = false;
61 } else {
62 if ( is_object( $lastResult ) ) {
63 if ( $lastResult != $currentResult ) {
64 $mismatch = true;
65 }
66 } else {
67 if ( $lastResult !== $currentResult ) {
68 $mismatch = true;
69 }
70 }
71 }
72 $results[$i] = $currentResult;
73 $lastResult = $currentResult;
74 }
75 if ( $mismatch ) {
76 if ( count( $results ) == 2 ) {
77 $resultsList = array();
78 foreach ( $this->parsers as $i => $parser ) {
79 $resultsList[] = var_export( $results[$i], true );
80 }
81 $diff = wfDiff( $resultsList[0], $resultsList[1] );
82 } else {
83 $diff = '[too many parsers]';
84 }
85 $msg = "Parser_DiffTest: results mismatch on call to $name\n";
86 if ( !$this->shortOutput ) {
87 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
88 }
89 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
90 "Diff: $diff\n";
91 throw new MWException( $msg );
92 }
93 return $lastResult;
94 }
95
96 function formatArray( $array ) {
97 if ( $this->shortOutput ) {
98 foreach ( $array as $key => $value ) {
99 if ( $value instanceof ParserOutput ) {
100 $array[$key] = "ParserOutput: {$value->getText()}";
101 }
102 }
103 }
104 return var_export( $array, true );
105 }
106
107 function setFunctionHook( $id, $callback, $flags = 0 ) {
108 $this->init();
109 foreach ( $this->parsers as $parser ) {
110 $parser->setFunctionHook( $id, $callback, $flags );
111 }
112 }
113
114 /**
115 * @param $parser Parser
116 * @return bool
117 */
118 function onClearState( &$parser ) {
119 // hack marker prefixes to get identical output
120 if ( !isset( $this->dtUniqPrefix ) ) {
121 $this->dtUniqPrefix = $parser->uniqPrefix();
122 } else {
123 $parser->mUniqPrefix = $this->dtUniqPrefix;
124 }
125 return true;
126 }
127 }