Merge "Remove $wgSessionStarted"
[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 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * @ingroup Parser
26 */
27 class Parser_DiffTest {
28 /** @var array */
29 protected $parsers;
30
31 /** @var array */
32 protected $conf;
33
34 /** @var bool */
35 protected $shortOutput = false;
36
37 /** @var string */
38 protected $dtUniqPrefix;
39
40 function __construct( $conf ) {
41 if ( !isset( $conf['parsers'] ) ) {
42 throw new MWException( __METHOD__ . ': no parsers specified' );
43 }
44 $this->conf = $conf;
45 }
46
47 function init() {
48 if ( !is_null( $this->parsers ) ) {
49 return;
50 }
51
52 global $wgHooks;
53 static $doneHook = false;
54 if ( !$doneHook ) {
55 $doneHook = true;
56 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
57 }
58 if ( isset( $this->conf['shortOutput'] ) ) {
59 $this->shortOutput = $this->conf['shortOutput'];
60 }
61
62 foreach ( $this->conf['parsers'] as $i => $parserConf ) {
63 if ( !is_array( $parserConf ) ) {
64 $class = $parserConf;
65 $parserConf = array( 'class' => $parserConf );
66 } else {
67 $class = $parserConf['class'];
68 }
69 $this->parsers[$i] = new $class( $parserConf );
70 }
71 }
72
73 function __call( $name, $args ) {
74 $this->init();
75 $results = array();
76 $mismatch = false;
77 $lastResult = null;
78 $first = true;
79 foreach ( $this->parsers as $i => $parser ) {
80 $currentResult = call_user_func_array( array( &$this->parsers[$i], $name ), $args );
81 if ( $first ) {
82 $first = false;
83 } else {
84 if ( is_object( $lastResult ) ) {
85 if ( $lastResult != $currentResult ) {
86 $mismatch = true;
87 }
88 } else {
89 if ( $lastResult !== $currentResult ) {
90 $mismatch = true;
91 }
92 }
93 }
94 $results[$i] = $currentResult;
95 $lastResult = $currentResult;
96 }
97 if ( $mismatch ) {
98 if ( count( $results ) == 2 ) {
99 $resultsList = array();
100 foreach ( $this->parsers as $i => $parser ) {
101 $resultsList[] = var_export( $results[$i], true );
102 }
103 $diff = wfDiff( $resultsList[0], $resultsList[1] );
104 } else {
105 $diff = '[too many parsers]';
106 }
107 $msg = "Parser_DiffTest: results mismatch on call to $name\n";
108 if ( !$this->shortOutput ) {
109 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
110 }
111 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
112 "Diff: $diff\n";
113 throw new MWException( $msg );
114 }
115 return $lastResult;
116 }
117
118 function formatArray( $array ) {
119 if ( $this->shortOutput ) {
120 foreach ( $array as $key => $value ) {
121 if ( $value instanceof ParserOutput ) {
122 $array[$key] = "ParserOutput: {$value->getText()}";
123 }
124 }
125 }
126 return var_export( $array, true );
127 }
128
129 function setFunctionHook( $id, $callback, $flags = 0 ) {
130 $this->init();
131 foreach ( $this->parsers as $parser ) {
132 $parser->setFunctionHook( $id, $callback, $flags );
133 }
134 }
135
136 /**
137 * @param Parser $parser
138 * @return bool
139 */
140 function onClearState( &$parser ) {
141 // hack marker prefixes to get identical output
142 if ( !isset( $this->dtUniqPrefix ) ) {
143 $this->dtUniqPrefix = $parser->uniqPrefix();
144 } else {
145 $parser->mUniqPrefix = $this->dtUniqPrefix;
146 }
147 return true;
148 }
149 }