2007-11-20 10:55:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class Parser_DiffTest
|
|
|
|
|
{
|
|
|
|
|
var $parsers, $conf;
|
|
|
|
|
|
2008-02-05 08:23:58 +00:00
|
|
|
var $dfUniqPrefix;
|
|
|
|
|
|
2007-11-20 10:55:08 +00:00
|
|
|
function __construct( $conf ) {
|
|
|
|
|
if ( !isset( $conf['parsers'] ) ) {
|
|
|
|
|
throw new MWException( __METHOD__ . ': no parsers specified' );
|
|
|
|
|
}
|
|
|
|
|
$this->conf = $conf;
|
2008-02-05 08:23:58 +00:00
|
|
|
$this->dtUniqPrefix = "\x7fUNIQ" . Parser::getRandomString();
|
2007-11-20 10:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
|
if ( !is_null( $this->parsers ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-02-05 08:23:58 +00:00
|
|
|
|
|
|
|
|
global $wgHooks;
|
|
|
|
|
static $doneHook = false;
|
|
|
|
|
if ( !$doneHook ) {
|
|
|
|
|
$doneHook = true;
|
|
|
|
|
$wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-20 10:55:08 +00:00
|
|
|
foreach ( $this->conf['parsers'] as $i => $parserConf ) {
|
|
|
|
|
if ( !is_array( $parserConf ) ) {
|
|
|
|
|
$class = $parserConf;
|
2008-01-24 04:29:56 +00:00
|
|
|
$parserConf = array( 'class' => $parserConf );
|
2007-11-20 10:55:08 +00:00
|
|
|
} else {
|
|
|
|
|
$class = $parserConf['class'];
|
|
|
|
|
}
|
|
|
|
|
$this->parsers[$i] = new $class( $parserConf );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function __call( $name, $args ) {
|
|
|
|
|
$this->init();
|
|
|
|
|
$results = array();
|
|
|
|
|
$mismatch = false;
|
|
|
|
|
$lastResult = null;
|
|
|
|
|
$first = true;
|
|
|
|
|
foreach ( $this->parsers as $i => $parser ) {
|
|
|
|
|
$currentResult = call_user_func_array( array( &$this->parsers[$i], $name ), $args );
|
|
|
|
|
if ( $first ) {
|
|
|
|
|
$first = false;
|
|
|
|
|
} else {
|
2008-02-05 08:23:58 +00:00
|
|
|
if ( is_object( $lastResult ) ) {
|
|
|
|
|
if ( $lastResult != $currentResult ) {
|
|
|
|
|
$mismatch = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if ( $lastResult !== $currentResult ) {
|
|
|
|
|
$mismatch = true;
|
|
|
|
|
}
|
2007-11-20 10:55:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$results[$i] = $currentResult;
|
|
|
|
|
$lastResult = $currentResult;
|
|
|
|
|
}
|
|
|
|
|
if ( $mismatch ) {
|
|
|
|
|
throw new MWException( "Parser_DiffTest: results mismatch on call to $name\n" .
|
2008-04-14 07:45:50 +00:00
|
|
|
'Arguments: ' . var_export( $args, true ) . "\n" .
|
2007-11-20 10:55:08 +00:00
|
|
|
'Results: ' . var_export( $results, true ) . "\n" );
|
|
|
|
|
}
|
|
|
|
|
return $lastResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setFunctionHook( $id, $callback, $flags = 0 ) {
|
|
|
|
|
$this->init();
|
|
|
|
|
foreach ( $this->parsers as $i => $parser ) {
|
|
|
|
|
$parser->setFunctionHook( $id, $callback, $flags );
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-02-05 08:23:58 +00:00
|
|
|
|
|
|
|
|
function onClearState( &$parser ) {
|
|
|
|
|
// hack marker prefixes to get identical output
|
|
|
|
|
$parser->mUniqPrefix = $this->dtUniqPrefix;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2007-11-20 10:55:08 +00:00
|
|
|
}
|