[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/parser/ -> ParserDiffTest.php (source)

   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  {
  29      public $parsers;
  30      public $conf;
  31      public $shortOutput = false;
  32      public $dtUniqPrefix;
  33  
  34  	public function __construct( $conf ) {
  35          if ( !isset( $conf['parsers'] ) ) {
  36              throw new MWException( __METHOD__ . ': no parsers specified' );
  37          }
  38          $this->conf = $conf;
  39      }
  40  
  41  	public function init() {
  42          if ( !is_null( $this->parsers ) ) {
  43              return;
  44          }
  45  
  46          global $wgHooks;
  47          static $doneHook = false;
  48          if ( !$doneHook ) {
  49              $doneHook = true;
  50              $wgHooks['ParserClearState'][] = array( $this, 'onClearState' );
  51          }
  52          if ( isset( $this->conf['shortOutput'] ) ) {
  53              $this->shortOutput = $this->conf['shortOutput'];
  54          }
  55  
  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'];
  62              }
  63              $this->parsers[$i] = new $class( $parserConf );
  64          }
  65      }
  66  
  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;
  81                      }
  82                  } else {
  83                      if ( $lastResult !== $currentResult ) {
  84                          $mismatch = true;
  85                      }
  86                  }
  87              }
  88              $results[$i] = $currentResult;
  89              $lastResult = $currentResult;
  90          }
  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 );
  96                  }
  97                  $diff = wfDiff( $resultsList[0], $resultsList[1] );
  98              } else {
  99                  $diff = '[too many parsers]';
 100              }
 101              $msg = "ParserDiffTest: results mismatch on call to $name\n";
 102              if ( !$this->shortOutput ) {
 103                  $msg .= 'Arguments: ' . $this->formatArray( $args ) . "\n";
 104              }
 105              $msg .= 'Results: ' . $this->formatArray( $results ) . "\n" .
 106                  "Diff: $diff\n";
 107              throw new MWException( $msg );
 108          }
 109          return $lastResult;
 110      }
 111  
 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()}";
 117                  }
 118              }
 119          }
 120          return var_export( $array, true );
 121      }
 122  
 123  	public function setFunctionHook( $id, $callback, $flags = 0 ) {
 124          $this->init();
 125          foreach ( $this->parsers as $parser ) {
 126              $parser->setFunctionHook( $id, $callback, $flags );
 127          }
 128      }
 129  
 130      /**
 131       * @param Parser $parser
 132       * @return bool
 133       */
 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;
 140          }
 141          return true;
 142      }
 143  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1