MediaWiki REL1_31
compareParsers.php
Go to the documentation of this file.
1<?php
31require_once __DIR__ . '/dumpIterator.php';
32
40
41 private $count = 0;
42
43 public function __construct() {
44 parent::__construct();
45 $this->saveFailed = false;
46 $this->addDescription( 'Run a file or dump with several parsers' );
47 $this->addOption( 'parser1', 'The first parser to compare.', true, true );
48 $this->addOption( 'parser2', 'The second parser to compare.', true, true );
49 $this->addOption( 'tidy', 'Run tidy on the articles.', false, false );
50 $this->addOption(
51 'save-failed',
52 'Folder in which articles which differ will be stored.',
53 false,
54 true
55 );
56 $this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
57 $this->addOption(
58 'diff-bin',
59 'Binary to use for diffing (can also be provided by DIFF env var).',
60 false,
61 false
62 );
63 $this->addOption(
64 'strip-parameters',
65 'Remove parameters of html tags to increase readability.',
66 false,
67 false
68 );
69 $this->addOption(
70 'show-parsed-output',
71 'Show the parsed html if both Parsers give the same output.',
72 false,
73 false
74 );
75 }
76
77 public function checkOptions() {
78 if ( $this->hasOption( 'save-failed' ) ) {
79 $this->saveFailed = $this->getOption( 'save-failed' );
80 }
81
82 $this->stripParametersEnabled = $this->hasOption( 'strip-parameters' );
83 $this->showParsedOutput = $this->hasOption( 'show-parsed-output' );
84
85 $this->showDiff = $this->hasOption( 'show-diff' );
86 if ( $this->showDiff ) {
87 $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
88 if ( $bin != '' ) {
89 global $wgDiff;
90 $wgDiff = $bin;
91 }
92 }
93
94 $user = new User();
95 $this->options = ParserOptions::newFromUser( $user );
96
97 if ( $this->hasOption( 'tidy' ) ) {
98 global $wgUseTidy;
99 if ( !$wgUseTidy ) {
100 $this->fatalError( 'Tidy was requested but $wgUseTidy is not set in LocalSettings.php' );
101 }
102 $this->options->setTidy( true );
103 }
104
105 $this->failed = 0;
106 }
107
108 public function conclusions() {
109 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
110 if ( $this->count > 0 ) {
111 $this->output( " (" . ( $this->failed / $this->count ) . "%)\n" );
112 }
113 }
114
115 function stripParameters( $text ) {
116 if ( !$this->stripParametersEnabled ) {
117 return $text;
118 }
119
120 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
121 }
122
127 public function processRevision( $rev ) {
128 $title = $rev->getTitle();
129
130 $parser1Name = $this->getOption( 'parser1' );
131 $parser2Name = $this->getOption( 'parser2' );
132
133 self::checkParserLocally( $parser1Name );
134 self::checkParserLocally( $parser2Name );
135
136 $parser1 = new $parser1Name();
137 $parser2 = new $parser2Name();
138
139 $content = $rev->getContent();
140
141 if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
142 $this->error( "Page {$title->getPrefixedText()} does not contain wikitext "
143 . "but {$content->getModel()}\n" );
144
145 return;
146 }
147
148 $text = strval( $content->getNativeData() );
149
150 $output1 = $parser1->parse( $text, $title, $this->options );
151 $output2 = $parser2->parse( $text, $title, $this->options );
152
153 if ( $output1->getText() != $output2->getText() ) {
154 $this->failed++;
155 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
156
157 if ( $this->saveFailed ) {
158 file_put_contents(
159 $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt",
160 $text
161 );
162 }
163 if ( $this->showDiff ) {
164 $this->output( wfDiff(
165 $this->stripParameters( $output1->getText() ),
166 $this->stripParameters( $output2->getText() ),
167 ''
168 ) );
169 }
170 } else {
171 $this->output( $title->getPrefixedText() . "\tOK\n" );
172
173 if ( $this->showParsedOutput ) {
174 $this->output( $this->stripParameters( $output1->getText() ) );
175 }
176 }
177 }
178
179 private static function checkParserLocally( $parserName ) {
180 /* Look for the parser in a file appropiately named in the current folder */
181 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
182 global $wgAutoloadClasses;
183 $wgAutoloadClasses[$parserName] = realpath( '.' ) . "/$parserName.php";
184 }
185 }
186}
187
188$maintClass = CompareParsers::class;
189require_once RUN_MAINTENANCE_IF_MAIN;
$wgUseTidy
Set this to true to use the deprecated tidy configuration parameters.
$wgDiff
Path to the GNU diff utility.
wfDiff( $before, $after, $params='-u')
Returns unified plain-text diff of two texts.
$wgAutoloadClasses['ReplaceTextHooks']
Maintenance script to take page text out of an XML dump file and render basic HTML out to files.
static checkParserLocally( $parserName)
__construct()
Default constructor.
processRevision( $rev)
Callback function for each revision, parse with both parsers and compare.
Base class for interating over a dump.
hasOption( $name)
Checks to see if a particular param exists.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
$maintClass
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing options(say) and put it in one place. Instead of having little title-reversing if-blocks spread all over the codebase in showAnArticle
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2612
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1777
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:245
require_once RUN_MAINTENANCE_IF_MAIN