Clean up typos, wrap to 80 chars instead of some random number, fix some grammar...
[mediawiki.git] / includes / Parser_DiffTest.php
blobd88709f01295728b46159089b449fde6633632b7
1 <?php
3 class Parser_DiffTest
5 var $parsers, $conf;
7 var $dfUniqPrefix;
9 function __construct( $conf ) {
10 if ( !isset( $conf['parsers'] ) ) {
11 throw new MWException( __METHOD__ . ': no parsers specified' );
13 $this->conf = $conf;
14 $this->dtUniqPrefix = "\x7fUNIQ" . Parser::getRandomString();
17 function init() {
18 if ( !is_null( $this->parsers ) ) {
19 return;
22 global $wgHooks;
23 static $doneHook = false;
24 if ( !$doneHook ) {
25 $doneHook = true;
26 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
29 foreach ( $this->conf['parsers'] as $i => $parserConf ) {
30 if ( !is_array( $parserConf ) ) {
31 $class = $parserConf;
32 $parserConf = array( 'class' => $parserConf );
33 } else {
34 $class = $parserConf['class'];
36 $this->parsers[$i] = new $class( $parserConf );
40 function __call( $name, $args ) {
41 $this->init();
42 $results = array();
43 $mismatch = false;
44 $lastResult = null;
45 $first = true;
46 foreach ( $this->parsers as $i => $parser ) {
47 $currentResult = call_user_func_array( array( &$this->parsers[$i], $name ), $args );
48 if ( $first ) {
49 $first = false;
50 } else {
51 if ( is_object( $lastResult ) ) {
52 if ( $lastResult != $currentResult ) {
53 $mismatch = true;
55 } else {
56 if ( $lastResult !== $currentResult ) {
57 $mismatch = true;
61 $results[$i] = $currentResult;
62 $lastResult = $currentResult;
64 if ( $mismatch ) {
65 throw new MWException( "Parser_DiffTest: results mismatch on call to $name\n" .
66 'Arguments: ' . var_export( $args, true ) . "\n" .
67 'Results: ' . var_export( $results, true ) . "\n" );
69 return $lastResult;
72 function setFunctionHook( $id, $callback, $flags = 0 ) {
73 $this->init();
74 foreach ( $this->parsers as $i => $parser ) {
75 $parser->setFunctionHook( $id, $callback, $flags );
79 function onClearState( &$parser ) {
80 // hack marker prefixes to get identical output
81 $parser->mUniqPrefix = $this->dtUniqPrefix;
82 return true;