Merge "(bug 40857) fix non-array sidebar links handling in CologneBlue"
[lhc/web/wiklou.git] / maintenance / compareParsers.php
index 4fe4e4d..1f3ac1c 100644 (file)
@@ -6,8 +6,9 @@
  *
  * Templates etc are pulled from the local wiki database, not from the dump.
  *
- * Copyright (C) 2011 Platonides - http://www.mediawiki.org/
- * 
+ * Copyright © 2011 Platonides
+ * http://www.mediawiki.org/
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * @file
  * @ingroup Maintenance
  */
-require_once( dirname( __FILE__ ) . '/dumpIterator.php' );
 
+require_once( __DIR__ . '/dumpIterator.php' );
+
+/**
+ * Maintenance script to take page text out of an XML dump file and render
+ * basic HTML out to files.
+ *
+ * @ingroup Maintenance
+ */
 class CompareParsers extends DumpIterator {
 
        private $count = 0;
@@ -51,10 +58,10 @@ class CompareParsers extends DumpIterator {
                if ( $this->hasOption('save-failed') ) {
                        $this->saveFailed = $this->getOption('save-failed');
                }
-               
+
                $this->stripParametersEnabled = $this->hasOption( 'strip-parameters' );
                $this->showParsedOutput = $this->hasOption( 'show-parsed-output' );
-               
+
                $this->showDiff = $this->hasOption( 'show-diff' );
                if ( $this->showDiff ) {
                        $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
@@ -63,10 +70,10 @@ class CompareParsers extends DumpIterator {
                                $wgDiff = $bin;
                        }
                }
-               
-               $user = new User();             
+
+               $user = new User();
                $this->options = ParserOptions::newFromUser( $user );
-               
+
                if ( $this->hasOption( 'tidy' ) ) {
                        global $wgUseTidy;
                        if ( !$wgUseTidy ) {
@@ -74,48 +81,57 @@ class CompareParsers extends DumpIterator {
                        }
                        $this->options->setTidy( true );
                }
-               
+
                $this->failed = 0;
        }
-       
-       public function conclusions() { 
+
+       public function conclusions() {
                $this->error( "{$this->failed} failed revisions out of {$this->count}" );
                if ($this->count > 0)
                        $this->output( " (" . ( $this->failed / $this->count ) . "%)\n" );
        }
-       
+
        function stripParameters( $text ) {
                if ( !$this->stripParametersEnabled ) {
                        return $text;
                }
                return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
        }
-       
+
        /**
         * Callback function for each revision, parse with both parsers and compare
         * @param $rev Revision
         */
        public function processRevision( $rev ) {
                $title = $rev->getTitle();
-                               
+
                $parser1Name = $this->getOption( 'parser1' );
                $parser2Name = $this->getOption( 'parser2' );
-               
+
                self::checkParserLocally( $parser1Name );
                self::checkParserLocally( $parser2Name );
-               
+
                $parser1 = new $parser1Name();
                $parser2 = new $parser2Name();
-               
-               $output1 = $parser1->parse( $rev->getText(), $title, $this->options );
-               $output2 = $parser2->parse( $rev->getText(), $title, $this->options );
+
+               $content = $rev->getContent();
+
+               if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
+                       $this->error( "Page {$title->getPrefixedText()} does not contain wikitext but {$content->getModel()}\n" );
+                       return;
+               }
+
+               $text = strval( $content->getNativeData() );
+
+               $output1 = $parser1->parse( $text, $title, $this->options );
+               $output2 = $parser2->parse( $text, $title, $this->options );
 
                if ( $output1->getText() != $output2->getText() ) {
                        $this->failed++;
                        $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
-                       
+
                        if ( $this->saveFailed ) {
-                               file_put_contents( $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt", $rev->getText());
+                               file_put_contents( $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt", $text );
                        }
                        if ( $this->showDiff ) {
                                $this->output( wfDiff( $this->stripParameters( $output1->getText() ), $this->stripParameters( $output2->getText() ), '' ) );
@@ -127,7 +143,7 @@ class CompareParsers extends DumpIterator {
                        }
                }
        }
-       
+
        private static function checkParserLocally( $parserName ) {
                /* Look for the parser in a file appropiately named in the current folder */
                if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {