Fix use of GenderCache in ApiPageSet::processTitlesArray
[lhc/web/wiklou.git] / includes / parser / ParserDiffTest.php
1 <?php
2 /**
3 * Fake parser that output the difference of two different parsers
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * @ingroup Parser
26 */
27 class ParserDiffTest {
28 public $parsers;
29 public $conf;
30 public $shortOutput = false;
31
32 public function __construct( $conf ) {
33 if ( !isset( $conf['parsers'] ) ) {
34 throw new MWException( __METHOD__ . ': no parsers specified' );
35 }
36 $this->conf = $conf;
37 }
38
39 public function init() {
40 if ( !is_null( $this->parsers ) ) {
41 return;
42 }
43
44 if ( isset( $this->conf['shortOutput'] ) ) {
45 $this->shortOutput = $this->conf['shortOutput'];
46 }
47
48 foreach ( $this->conf['parsers'] as $i => $parserConf ) {
49 if ( !is_array( $parserConf ) ) {
50 $class = $parserConf;
51 $parserConf = [ 'class' => $parserConf ];
52 } else {
53 $class = $parserConf['class'];
54 }
55 $this->parsers[$i] = new $class( $parserConf );
56 }
57 }
58
59 public function __call( $name, $args ) {
60 $this->init();
61 $results = [];
62 $mismatch = false;
63 $lastResult = null;
64 foreach ( $this->parsers as $parser ) {
65 $currentResult = $parser->$name( ...$args );
66 if ( count( $results ) > 0 ) {
67 if ( is_object( $lastResult ) ) {
68 if ( $lastResult != $currentResult ) {
69 $mismatch = true;
70 }
71 } else {
72 if ( $lastResult !== $currentResult ) {
73 $mismatch = true;
74 }
75 }
76 }
77 $results[] = $currentResult;
78 $lastResult = $currentResult;
79 }
80 if ( $mismatch ) {
81 if ( count( $results ) == 2 ) {
82 $resultsList = [];
83 foreach ( $this->parsers as $i => $parser ) {
84 $resultsList[] = var_export( $results[$i], true );
85 }
86 $diff = wfDiff( $resultsList[0], $resultsList[1] );
87 } else {
88 $diff = '[too many parsers]';
89 }
90 $msg = "ParserDiffTest: results mismatch on call to $name\n";
91 if ( !$this->shortOutput ) {
92 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
93 }
94 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
95 "Diff: $diff\n";
96 throw new MWException( $msg );
97 }
98 return $lastResult;
99 }
100
101 public function formatArray( $array ) {
102 if ( $this->shortOutput ) {
103 foreach ( $array as $key => $value ) {
104 if ( $value instanceof ParserOutput ) {
105 $array[$key] = "ParserOutput: {$value->getText()}";
106 }
107 }
108 }
109 return var_export( $array, true );
110 }
111
112 public function setFunctionHook( $id, $callback, $flags = 0 ) {
113 $this->init();
114 foreach ( $this->parsers as $parser ) {
115 $parser->setFunctionHook( $id, $callback, $flags );
116 }
117 }
118 }