MediaWiki  REL1_19
Parser_DiffTest.php
Go to the documentation of this file.
00001 <?php
00011 class Parser_DiffTest
00012 {
00013         var $parsers, $conf;
00014         var $shortOutput = false;
00015 
00016         var $dtUniqPrefix;
00017 
00018         function __construct( $conf ) {
00019                 if ( !isset( $conf['parsers'] ) ) {
00020                         throw new MWException( __METHOD__ . ': no parsers specified' );
00021                 }
00022                 $this->conf = $conf;
00023         }
00024 
00025         function init() {
00026                 if ( !is_null( $this->parsers ) ) {
00027                         return;
00028                 }
00029 
00030                 global $wgHooks;
00031                 static $doneHook = false;
00032                 if ( !$doneHook ) {
00033                         $doneHook = true;
00034                         $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
00035                 }
00036                 if ( isset( $this->conf['shortOutput'] ) ) {
00037                         $this->shortOutput = $this->conf['shortOutput'];
00038                 }
00039 
00040                 foreach ( $this->conf['parsers'] as $i => $parserConf ) {
00041                         if ( !is_array( $parserConf ) ) {
00042                                 $class = $parserConf;
00043                                 $parserConf = array( 'class' => $parserConf );
00044                         } else {
00045                                 $class = $parserConf['class'];
00046                         }
00047                         $this->parsers[$i] = new $class( $parserConf );
00048                 }
00049         }
00050 
00051         function __call( $name, $args ) {
00052                 $this->init();
00053                 $results = array();
00054                 $mismatch = false;
00055                 $lastResult = null;
00056                 $first = true;
00057                 foreach ( $this->parsers as $i => $parser ) {
00058                         $currentResult = call_user_func_array( array( &$this->parsers[$i], $name ), $args );
00059                         if ( $first ) {
00060                                 $first = false;
00061                         } else {
00062                                 if ( is_object( $lastResult ) ) {
00063                                         if ( $lastResult != $currentResult ) {
00064                                                 $mismatch = true;
00065                                         }
00066                                 } else {
00067                                         if ( $lastResult !== $currentResult ) {
00068                                                 $mismatch = true;
00069                                         }
00070                                 }
00071                         }
00072                         $results[$i] = $currentResult;
00073                         $lastResult = $currentResult;
00074                 }
00075                 if ( $mismatch ) {
00076                         if ( count( $results ) == 2 ) {
00077                                 $resultsList = array();
00078                                 foreach ( $this->parsers as $i => $parser ) {
00079                                         $resultsList[] = var_export( $results[$i], true );
00080                                 }
00081                                 $diff = wfDiff( $resultsList[0], $resultsList[1] );
00082                         } else {
00083                                 $diff = '[too many parsers]';
00084                         }
00085                         $msg = "Parser_DiffTest: results mismatch on call to $name\n";
00086                         if ( !$this->shortOutput ) {
00087                                 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
00088                         }
00089                         $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
00090                                 "Diff: $diff\n";
00091                         throw new MWException( $msg );
00092                 }
00093                 return $lastResult;
00094         }
00095 
00096         function formatArray( $array ) {
00097                 if ( $this->shortOutput ) {
00098                         foreach ( $array as $key => $value ) {
00099                                 if ( $value instanceof ParserOutput ) {
00100                                         $array[$key] = "ParserOutput: {$value->getText()}";
00101                                 }
00102                         }
00103                 }
00104                 return var_export( $array, true );
00105         }
00106 
00107         function setFunctionHook( $id, $callback, $flags = 0 ) {
00108                 $this->init();
00109                 foreach  ( $this->parsers as $parser ) {
00110                         $parser->setFunctionHook( $id, $callback, $flags );
00111                 }
00112         }
00113 
00118         function onClearState( &$parser ) {
00119                 // hack marker prefixes to get identical output
00120                 if ( !isset( $this->dtUniqPrefix ) ) {
00121                         $this->dtUniqPrefix = $parser->uniqPrefix();
00122                 } else {
00123                         $parser->mUniqPrefix = $this->dtUniqPrefix;
00124                 }
00125                 return true;
00126         }
00127 }