Fix for bug 13004, in which the Postgres full-text search has too many results,
[lhc/web/wiklou.git] / includes / Parser_DiffTest.php
1 <?php
2
3 class Parser_DiffTest
4 {
5 var $parsers, $conf;
6
7 var $dfUniqPrefix;
8
9 function __construct( $conf ) {
10 if ( !isset( $conf['parsers'] ) ) {
11 throw new MWException( __METHOD__ . ': no parsers specified' );
12 }
13 $this->conf = $conf;
14 $this->dtUniqPrefix = "\x7fUNIQ" . Parser::getRandomString();
15 }
16
17 function init() {
18 if ( !is_null( $this->parsers ) ) {
19 return;
20 }
21
22 global $wgHooks;
23 static $doneHook = false;
24 if ( !$doneHook ) {
25 $doneHook = true;
26 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
27 }
28
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'];
35 }
36 $this->parsers[$i] = new $class( $parserConf );
37 }
38 }
39
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;
54 }
55 } else {
56 if ( $lastResult !== $currentResult ) {
57 $mismatch = true;
58 }
59 }
60 }
61 $results[$i] = $currentResult;
62 $lastResult = $currentResult;
63 }
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" );
68 }
69 return $lastResult;
70 }
71
72 function setFunctionHook( $id, $callback, $flags = 0 ) {
73 $this->init();
74 foreach ( $this->parsers as $i => $parser ) {
75 $parser->setFunctionHook( $id, $callback, $flags );
76 }
77 }
78
79 function onClearState( &$parser ) {
80 // hack marker prefixes to get identical output
81 $parser->mUniqPrefix = $this->dtUniqPrefix;
82 return true;
83 }
84 }
85