2011-01-13 17:32:42 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Take page text out of an XML dump file and render basic HTML out to files.
|
|
|
|
|
* This is *NOT* suitable for publishing or offline use; it's intended for
|
|
|
|
|
* running comparative tests of parsing behavior using real-world data.
|
|
|
|
|
*
|
|
|
|
|
* Templates etc are pulled from the local wiki database, not from the dump.
|
|
|
|
|
*
|
2012-06-25 19:54:41 +00:00
|
|
|
* Copyright © 2011 Platonides
|
2014-03-20 15:45:01 +00:00
|
|
|
* https://www.mediawiki.org/
|
2012-06-25 19:54:41 +00:00
|
|
|
*
|
2011-01-13 17:32:42 +00:00
|
|
|
* 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
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2024-08-06 13:40:20 +00:00
|
|
|
use MediaWiki\Content\WikitextContent;
|
2024-10-21 17:06:13 +00:00
|
|
|
use MediaWiki\Parser\ParserOptions;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-06-20 04:02:04 +00:00
|
|
|
use Wikimedia\Diff\Diff;
|
|
|
|
|
use Wikimedia\Diff\UnifiedDiffFormatter;
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/dumpIterator.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2011-01-13 17:32:42 +00:00
|
|
|
|
2012-06-25 19:54:41 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script to take page text out of an XML dump file and render
|
|
|
|
|
* basic HTML out to files.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2011-05-26 18:49:25 +00:00
|
|
|
class CompareParsers extends DumpIterator {
|
2011-01-13 17:32:42 +00:00
|
|
|
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var int */
|
2011-01-13 17:32:42 +00:00
|
|
|
private $count = 0;
|
2022-03-08 18:41:30 +00:00
|
|
|
/** @var string|false */
|
2021-05-12 08:52:47 +00:00
|
|
|
private $saveFailed = false;
|
2019-09-09 09:11:50 +00:00
|
|
|
/** @var bool */
|
|
|
|
|
private $stripParametersEnabled;
|
|
|
|
|
/** @var bool */
|
|
|
|
|
private $showParsedOutput;
|
|
|
|
|
/** @var bool */
|
|
|
|
|
private $showDiff;
|
|
|
|
|
/** @var ParserOptions */
|
|
|
|
|
private $options;
|
|
|
|
|
/** @var int */
|
|
|
|
|
private $failed;
|
2011-01-13 17:32:42 +00:00
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Run a file or dump with several parsers' );
|
2011-01-13 17:32:42 +00:00
|
|
|
$this->addOption( 'parser1', 'The first parser to compare.', true, true );
|
|
|
|
|
$this->addOption( 'parser2', 'The second parser to compare.', true, true );
|
2014-04-23 09:22:55 +00:00
|
|
|
$this->addOption(
|
|
|
|
|
'save-failed',
|
|
|
|
|
'Folder in which articles which differ will be stored.',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
2011-01-13 17:32:42 +00:00
|
|
|
$this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
|
2014-04-23 09:22:55 +00:00
|
|
|
$this->addOption(
|
|
|
|
|
'diff-bin',
|
|
|
|
|
'Binary to use for diffing (can also be provided by DIFF env var).',
|
|
|
|
|
false,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
$this->addOption(
|
|
|
|
|
'strip-parameters',
|
|
|
|
|
'Remove parameters of html tags to increase readability.',
|
|
|
|
|
false,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
$this->addOption(
|
|
|
|
|
'show-parsed-output',
|
|
|
|
|
'Show the parsed html if both Parsers give the same output.',
|
|
|
|
|
false,
|
|
|
|
|
false
|
|
|
|
|
);
|
2011-01-13 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-26 18:49:25 +00:00
|
|
|
public function checkOptions() {
|
2013-04-18 18:48:44 +00:00
|
|
|
if ( $this->hasOption( 'save-failed' ) ) {
|
|
|
|
|
$this->saveFailed = $this->getOption( 'save-failed' );
|
2011-01-13 17:32:42 +00:00
|
|
|
}
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
$this->stripParametersEnabled = $this->hasOption( 'strip-parameters' );
|
|
|
|
|
$this->showParsedOutput = $this->hasOption( 'show-parsed-output' );
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
$this->showDiff = $this->hasOption( 'show-diff' );
|
|
|
|
|
if ( $this->showDiff ) {
|
|
|
|
|
$bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
|
|
|
|
|
if ( $bin != '' ) {
|
|
|
|
|
global $wgDiff;
|
|
|
|
|
$wgDiff = $bin;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-25 19:54:41 +00:00
|
|
|
|
|
|
|
|
$user = new User();
|
2011-01-13 17:32:42 +00:00
|
|
|
$this->options = ParserOptions::newFromUser( $user );
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
$this->failed = 0;
|
2011-05-26 18:49:25 +00:00
|
|
|
}
|
2012-06-25 19:54:41 +00:00
|
|
|
|
|
|
|
|
public function conclusions() {
|
2011-01-13 17:32:42 +00:00
|
|
|
$this->error( "{$this->failed} failed revisions out of {$this->count}" );
|
2013-04-18 18:48:44 +00:00
|
|
|
if ( $this->count > 0 ) {
|
2011-01-13 17:32:42 +00:00
|
|
|
$this->output( " (" . ( $this->failed / $this->count ) . "%)\n" );
|
2013-04-18 18:48:44 +00:00
|
|
|
}
|
2011-01-13 17:32:42 +00:00
|
|
|
}
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function stripParameters( $text ) {
|
2011-01-13 17:32:42 +00:00
|
|
|
if ( !$this->stripParametersEnabled ) {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
|
|
|
|
|
}
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
/**
|
|
|
|
|
* Callback function for each revision, parse with both parsers and compare
|
2020-04-03 23:03:40 +00:00
|
|
|
* @param WikiRevision $rev
|
2011-01-13 17:32:42 +00:00
|
|
|
*/
|
2020-04-03 23:03:40 +00:00
|
|
|
public function processRevision( WikiRevision $rev ) {
|
2011-01-13 17:32:42 +00:00
|
|
|
$title = $rev->getTitle();
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
$parser1Name = $this->getOption( 'parser1' );
|
|
|
|
|
$parser2Name = $this->getOption( 'parser2' );
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
self::checkParserLocally( $parser1Name );
|
|
|
|
|
self::checkParserLocally( $parser2Name );
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
$parser1 = new $parser1Name();
|
|
|
|
|
$parser2 = new $parser2Name();
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2012-09-14 11:13:29 +00:00
|
|
|
$content = $rev->getContent();
|
|
|
|
|
|
|
|
|
|
if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
|
2014-04-23 09:22:55 +00:00
|
|
|
$this->error( "Page {$title->getPrefixedText()} does not contain wikitext "
|
|
|
|
|
. "but {$content->getModel()}\n" );
|
|
|
|
|
|
2012-09-14 11:13:29 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 16:14:38 +00:00
|
|
|
/** @var WikitextContent $content */
|
|
|
|
|
'@phan-var WikitextContent $content';
|
2019-02-07 00:14:23 +00:00
|
|
|
$text = strval( $content->getText() );
|
2012-09-14 11:13:29 +00:00
|
|
|
|
|
|
|
|
$output1 = $parser1->parse( $text, $title, $this->options );
|
|
|
|
|
$output2 = $parser2->parse( $text, $title, $this->options );
|
2011-01-13 17:32:42 +00:00
|
|
|
|
|
|
|
|
if ( $output1->getText() != $output2->getText() ) {
|
|
|
|
|
$this->failed++;
|
|
|
|
|
$this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2011-01-13 17:43:45 +00:00
|
|
|
if ( $this->saveFailed ) {
|
2014-04-23 09:22:55 +00:00
|
|
|
file_put_contents(
|
|
|
|
|
$this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt",
|
|
|
|
|
$text
|
|
|
|
|
);
|
2011-01-13 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
if ( $this->showDiff ) {
|
2020-03-31 22:16:42 +00:00
|
|
|
$diffs = new Diff(
|
|
|
|
|
explode( "\n", $this->stripParameters( $output1->getText() ) ),
|
|
|
|
|
explode( "\n", $this->stripParameters( $output2->getText() ) )
|
|
|
|
|
);
|
|
|
|
|
$formatter = new UnifiedDiffFormatter();
|
|
|
|
|
$unifiedDiff = $formatter->format( $diffs );
|
|
|
|
|
|
|
|
|
|
$this->output( $unifiedDiff );
|
2011-01-13 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( $title->getPrefixedText() . "\tOK\n" );
|
2014-04-23 09:22:55 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
if ( $this->showParsedOutput ) {
|
|
|
|
|
$this->output( $this->stripParameters( $output1->getText() ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-25 19:54:41 +00:00
|
|
|
|
2011-01-13 17:32:42 +00:00
|
|
|
private static function checkParserLocally( $parserName ) {
|
2022-05-09 09:09:00 +00:00
|
|
|
/* Look for the parser in a file appropriately named in the current folder */
|
2011-01-13 17:32:42 +00:00
|
|
|
if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
|
|
|
|
|
global $wgAutoloadClasses;
|
2014-04-23 18:08:42 +00:00
|
|
|
$wgAutoloadClasses[$parserName] = realpath( '.' ) . "/$parserName.php";
|
2011-01-13 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = CompareParsers::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|