3 * Fake parser that output the difference of two different parsers
14 var $shortOutput = false;
18 function __construct( $conf ) {
19 if ( !isset( $conf['parsers'] ) ) {
20 throw new MWException( __METHOD__
. ': no parsers specified' );
26 if ( !is_null( $this->parsers
) ) {
31 static $doneHook = false;
34 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
36 if ( isset( $this->conf
['shortOutput'] ) ) {
37 $this->shortOutput
= $this->conf
['shortOutput'];
40 foreach ( $this->conf
['parsers'] as $i => $parserConf ) {
41 if ( !is_array( $parserConf ) ) {
43 $parserConf = array( 'class' => $parserConf );
45 $class = $parserConf['class'];
47 $this->parsers
[$i] = new $class( $parserConf );
51 function __call( $name, $args ) {
57 foreach ( $this->parsers
as $i => $parser ) {
58 $currentResult = call_user_func_array( array( &$this->parsers
[$i], $name ), $args );
62 if ( is_object( $lastResult ) ) {
63 if ( $lastResult != $currentResult ) {
67 if ( $lastResult !== $currentResult ) {
72 $results[$i] = $currentResult;
73 $lastResult = $currentResult;
76 if ( count( $results ) == 2 ) {
77 $resultsList = array();
78 foreach ( $this->parsers
as $i => $parser ) {
79 $resultsList[] = var_export( $results[$i], true );
81 $diff = wfDiff( $resultsList[0], $resultsList[1] );
83 $diff = '[too many parsers]';
85 $msg = "Parser_DiffTest: results mismatch on call to $name\n";
86 if ( !$this->shortOutput
) {
87 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
89 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
91 throw new MWException( $msg );
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()}";
104 return var_export( $array, true );
107 function setFunctionHook( $id, $callback, $flags = 0 ) {
109 foreach ( $this->parsers
as $parser ) {
110 $parser->setFunctionHook( $id, $callback, $flags );
115 * @param $parser Parser
118 function onClearState( &$parser ) {
119 // hack marker prefixes to get identical output
120 if ( !isset( $this->dtUniqPrefix
) ) {
121 $this->dtUniqPrefix
= $parser->uniqPrefix();
123 $parser->mUniqPrefix
= $this->dtUniqPrefix
;