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' );
23 $this->dtUniqPrefix
= "\x7fUNIQ" . Parser
::getRandomString();
27 if ( !is_null( $this->parsers
) ) {
32 static $doneHook = false;
35 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
37 if ( isset( $this->conf
['shortOutput'] ) ) {
38 $this->shortOutput
= $this->conf
['shortOutput'];
41 foreach ( $this->conf
['parsers'] as $i => $parserConf ) {
42 if ( !is_array( $parserConf ) ) {
44 $parserConf = array( 'class' => $parserConf );
46 $class = $parserConf['class'];
48 $this->parsers
[$i] = new $class( $parserConf );
52 function __call( $name, $args ) {
58 foreach ( $this->parsers
as $i => $parser ) {
59 $currentResult = call_user_func_array( array( &$this->parsers
[$i], $name ), $args );
63 if ( is_object( $lastResult ) ) {
64 if ( $lastResult != $currentResult ) {
68 if ( $lastResult !== $currentResult ) {
73 $results[$i] = $currentResult;
74 $lastResult = $currentResult;
77 if ( count( $results ) == 2 ) {
78 $resultsList = array();
79 foreach ( $this->parsers
as $i => $parser ) {
80 $resultsList[] = var_export( $results[$i], true );
82 $diff = wfDiff( $resultsList[0], $resultsList[1] );
84 $diff = '[too many parsers]';
86 $msg = "Parser_DiffTest: results mismatch on call to $name\n";
87 if ( !$this->shortOutput
) {
88 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
90 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
92 throw new MWException( $msg );
97 function formatArray( $array ) {
98 if ( $this->shortOutput
) {
99 foreach ( $array as $key => $value ) {
100 if ( $value instanceof ParserOutput
) {
101 $array[$key] = "ParserOutput: {$value->getText()}";
105 return var_export( $array, true );
108 function setFunctionHook( $id, $callback, $flags = 0 ) {
110 foreach ( $this->parsers
as $i => $parser ) {
111 $parser->setFunctionHook( $id, $callback, $flags );
115 function onClearState( &$parser ) {
116 // hack marker prefixes to get identical output
117 $parser->mUniqPrefix
= $this->dtUniqPrefix
;