Profiler: Call getContentType() only once in logData()
[mediawiki.git] / includes / parser / ParserDiffTest.php
blob174c1d61ac309ef2f073bfef9c38e57db5caa187
1 <?php
2 /**
3 * Fake parser that output the difference of two different parsers
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.
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.
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
20 * @file
21 * @ingroup Parser
24 /**
25 * @ingroup Parser
27 class ParserDiffTest
29 public $parsers;
30 public $conf;
31 public $shortOutput = false;
32 public $dtUniqPrefix;
34 public function __construct( $conf ) {
35 if ( !isset( $conf['parsers'] ) ) {
36 throw new MWException( __METHOD__ . ': no parsers specified' );
38 $this->conf = $conf;
41 public function init() {
42 if ( !is_null( $this->parsers ) ) {
43 return;
46 global $wgHooks;
47 static $doneHook = false;
48 if ( !$doneHook ) {
49 $doneHook = true;
50 $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
52 if ( isset( $this->conf['shortOutput'] ) ) {
53 $this->shortOutput = $this->conf['shortOutput'];
56 foreach ( $this->conf['parsers'] as $i => $parserConf ) {
57 if ( !is_array( $parserConf ) ) {
58 $class = $parserConf;
59 $parserConf = array( 'class' => $parserConf );
60 } else {
61 $class = $parserConf['class'];
63 $this->parsers[$i] = new $class( $parserConf );
67 public function __call( $name, $args ) {
68 $this->init();
69 $results = array();
70 $mismatch = false;
71 $lastResult = null;
72 $first = true;
73 foreach ( $this->parsers as $i => $parser ) {
74 $currentResult = call_user_func_array( array( &$this->parsers[$i], $name ), $args );
75 if ( $first ) {
76 $first = false;
77 } else {
78 if ( is_object( $lastResult ) ) {
79 if ( $lastResult != $currentResult ) {
80 $mismatch = true;
82 } else {
83 if ( $lastResult !== $currentResult ) {
84 $mismatch = true;
88 $results[$i] = $currentResult;
89 $lastResult = $currentResult;
91 if ( $mismatch ) {
92 if ( count( $results ) == 2 ) {
93 $resultsList = array();
94 foreach ( $this->parsers as $i => $parser ) {
95 $resultsList[] = var_export( $results[$i], true );
97 $diff = wfDiff( $resultsList[0], $resultsList[1] );
98 } else {
99 $diff = '[too many parsers]';
101 $msg = "ParserDiffTest: results mismatch on call to $name\n";
102 if ( !$this->shortOutput ) {
103 $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
105 $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
106 "Diff: $diff\n";
107 throw new MWException( $msg );
109 return $lastResult;
112 public function formatArray( $array ) {
113 if ( $this->shortOutput ) {
114 foreach ( $array as $key => $value ) {
115 if ( $value instanceof ParserOutput ) {
116 $array[$key] = "ParserOutput: {$value->getText()}";
120 return var_export( $array, true );
123 public function setFunctionHook( $id, $callback, $flags = 0 ) {
124 $this->init();
125 foreach ( $this->parsers as $parser ) {
126 $parser->setFunctionHook( $id, $callback, $flags );
131 * @param Parser $parser
132 * @return bool
134 public function onClearState( &$parser ) {
135 // hack marker prefixes to get identical output
136 if ( !isset( $this->dtUniqPrefix ) ) {
137 $this->dtUniqPrefix = $parser->uniqPrefix();
138 } else {
139 $parser->mUniqPrefix = $this->dtUniqPrefix;
141 return true;