Remove silly 'm' prefix from StubObject/DeprecatedGlobal members
[mediawiki.git] / includes / parser / Parser_DiffTest.php
blob4d821cada500b2bf85d94b69113b733571b384fb
1 <?php
2 /**
3 * Fake parser that output the difference of two different parsers
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.
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.
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
20 * @file
21 * @ingroup Parser
24 /**
25 * @ingroup Parser
27 class Parser_DiffTest {
28 /** @var array */
29 protected $parsers;
31 /** @var array */
32 protected $conf;
34 /** @var bool */
35 protected $shortOutput = false;
37 /** @var string */
38 protected $dtUniqPrefix;
40 function __construct( $conf ) {
41 if ( !isset( $conf['parsers'] ) ) {
42 throw new MWException( __METHOD__ . ': no parsers specified' );
44 $this->conf = $conf;
47 function init() {
48 if ( !is_null( $this->parsers ) ) {
49 return;
52 global $wgHooks;
53 static $doneHook = false;
54 if ( !$doneHook ) {
55 $doneHook = true;
56 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
58 if ( isset( $this->conf['shortOutput'] ) ) {
59 $this->shortOutput = $this->conf['shortOutput'];
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'];
69 $this->parsers[$i] = new $class( $parserConf );
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;
88 } else {
89 if ( $lastResult !== $currentResult ) {
90 $mismatch = true;
94 $results[$i] = $currentResult;
95 $lastResult = $currentResult;
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 );
103 $diff = wfDiff( $resultsList[0], $resultsList[1] );
104 } else {
105 $diff = '[too many parsers]';
107 $msg = "Parser_DiffTest: results mismatch on call to $name\n";
108 if ( !$this->shortOutput ) {
109 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
111 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
112 "Diff: $diff\n";
113 throw new MWException( $msg );
115 return $lastResult;
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()}";
126 return var_export( $array, true );
129 function setFunctionHook( $id, $callback, $flags = 0 ) {
130 $this->init();
131 foreach ( $this->parsers as $parser ) {
132 $parser->setFunctionHook( $id, $callback, $flags );
137 * @param Parser $parser
138 * @return bool
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;
147 return true;