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
30 var $shortOutput = false;
34 function __construct( $conf ) {
35 if ( !isset( $conf['parsers'] ) ) {
36 throw new MWException( __METHOD__
. ': no parsers specified' );
42 if ( !is_null( $this->parsers
) ) {
47 static $doneHook = false;
50 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
52 if ( isset( $this->conf
['shortOutput'] ) ) {
53 $this->shortOutput
= $this->conf
['shortOutput'];
56 foreach ( $this->conf
['parsers'] as $i => $parserConf ) {
57 if ( !is_array( $parserConf ) ) {
59 $parserConf = array( 'class' => $parserConf );
61 $class = $parserConf['class'];
63 $this->parsers
[$i] = new $class( $parserConf );
67 function __call( $name, $args ) {
73 foreach ( $this->parsers
as $i => $parser ) {
74 $currentResult = call_user_func_array( array( &$this->parsers
[$i], $name ), $args );
78 if ( is_object( $lastResult ) ) {
79 if ( $lastResult != $currentResult ) {
83 if ( $lastResult !== $currentResult ) {
88 $results[$i] = $currentResult;
89 $lastResult = $currentResult;
92 if ( count( $results ) == 2 ) {
93 $resultsList = array();
94 foreach ( $this->parsers
as $i => $parser ) {
95 $resultsList[] = var_export( $results[$i], true );
97 $diff = wfDiff( $resultsList[0], $resultsList[1] );
99 $diff = '[too many parsers]';
101 $msg = "Parser_DiffTest: results mismatch on call to $name\n";
102 if ( !$this->shortOutput
) {
103 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
105 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
107 throw new MWException( $msg );
112 function formatArray( $array ) {
113 if ( $this->shortOutput
) {
114 foreach ( $array as $key => $value ) {
115 if ( $value instanceof ParserOutput
) {
116 $array[$key] = "ParserOutput: {$value->getText()}";
120 return var_export( $array, true );
123 function setFunctionHook( $id, $callback, $flags = 0 ) {
125 foreach ( $this->parsers
as $parser ) {
126 $parser->setFunctionHook( $id, $callback, $flags );
131 * @param $parser Parser
134 function onClearState( &$parser ) {
135 // hack marker prefixes to get identical output
136 if ( !isset( $this->dtUniqPrefix
) ) {
137 $this->dtUniqPrefix
= $parser->uniqPrefix();
139 $parser->mUniqPrefix
= $this->dtUniqPrefix
;