Parser_DiffTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @ingroup Parser
  4. */
  5. class Parser_DiffTest
  6. {
  7. var $parsers, $conf;
  8. var $shortOutput = false;
  9. var $dfUniqPrefix;
  10. function __construct( $conf ) {
  11. if ( !isset( $conf['parsers'] ) ) {
  12. throw new MWException( __METHOD__ . ': no parsers specified' );
  13. }
  14. $this->conf = $conf;
  15. $this->dtUniqPrefix = "\x7fUNIQ" . Parser::getRandomString();
  16. }
  17. function init() {
  18. if ( !is_null( $this->parsers ) ) {
  19. return;
  20. }
  21. global $wgHooks;
  22. static $doneHook = false;
  23. if ( !$doneHook ) {
  24. $doneHook = true;
  25. $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
  26. }
  27. if ( isset( $this->conf['shortOutput'] ) ) {
  28. $this->shortOutput = $this->conf['shortOutput'];
  29. }
  30. foreach ( $this->conf['parsers'] as $i => $parserConf ) {
  31. if ( !is_array( $parserConf ) ) {
  32. $class = $parserConf;
  33. $parserConf = array( 'class' => $parserConf );
  34. } else {
  35. $class = $parserConf['class'];
  36. }
  37. $this->parsers[$i] = new $class( $parserConf );
  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. if ( count( $results ) == 2 ) {
  66. $resultsList = array();
  67. foreach ( $this->parsers as $i => $parser ) {
  68. $resultsList[] = var_export( $results[$i], true );
  69. }
  70. $diff = wfDiff( $resultsList[0], $resultsList[1] );
  71. } else {
  72. $diff = '[too many parsers]';
  73. }
  74. $msg = "Parser_DiffTest: results mismatch on call to $name\n";
  75. if ( !$this->shortOutput ) {
  76. $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
  77. }
  78. $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
  79. "Diff: $diff\n";
  80. throw new MWException( $msg );
  81. }
  82. return $lastResult;
  83. }
  84. function formatArray( $array ) {
  85. if ( $this->shortOutput ) {
  86. foreach ( $array as $key => $value ) {
  87. if ( $value instanceof ParserOutput ) {
  88. $array[$key] = "ParserOutput: {$value->getText()}";
  89. }
  90. }
  91. }
  92. return var_export( $array, true );
  93. }
  94. function setFunctionHook( $id, $callback, $flags = 0 ) {
  95. $this->init();
  96. foreach ( $this->parsers as $i => $parser ) {
  97. $parser->setFunctionHook( $id, $callback, $flags );
  98. }
  99. }
  100. function onClearState( &$parser ) {
  101. // hack marker prefixes to get identical output
  102. $parser->mUniqPrefix = $this->dtUniqPrefix;
  103. return true;
  104. }
  105. }