MediaWiki REL1_33
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 if ( !MWTidy::isEnabled() ) {
99 $this->fatalError( 'Tidy was requested but $wgTidyConfig is not set in LocalSettings.php' );
100 }
101 $this->options->setTidy( true );
102 }
103
104 $this->failed = 0;
105 }
106
107 public function conclusions() {
108 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
109 if ( $this->count > 0 ) {
110 $this->output( " (" . ( $this->failed / $this->count ) . "%)\n" );
111 }
112 }
113
114 function stripParameters( $text ) {
115 if ( !$this->stripParametersEnabled ) {
116 return $text;
117 }
118
119 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
120 }
121
126 public function processRevision( $rev ) {
127 $title = $rev->getTitle();
128
129 $parser1Name = $this->getOption( 'parser1' );
130 $parser2Name = $this->getOption( 'parser2' );
131
132 self::checkParserLocally( $parser1Name );
133 self::checkParserLocally( $parser2Name );
134
135 $parser1 = new $parser1Name();
136 $parser2 = new $parser2Name();
137
138 $content = $rev->getContent();
139
140 if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
141 $this->error( "Page {$title->getPrefixedText()} does not contain wikitext "
142 . "but {$content->getModel()}\n" );
143
144 return;
145 }
146
147 $text = strval( $content->getText() );
148
149 $output1 = $parser1->parse( $text, $title, $this->options );
150 $output2 = $parser2->parse( $text, $title, $this->options );
151
152 if ( $output1->getText() != $output2->getText() ) {
153 $this->failed++;
154 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
155
156 if ( $this->saveFailed ) {
157 file_put_contents(
158 $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt",
159 $text
160 );
161 }
162 if ( $this->showDiff ) {
163 $this->output( wfDiff(
164 $this->stripParameters( $output1->getText() ),
165 $this->stripParameters( $output2->getText() ),
166 ''
167 ) );
168 }
169 } else {
170 $this->output( $title->getPrefixedText() . "\tOK\n" );
171
172 if ( $this->showParsedOutput ) {
173 $this->output( $this->stripParameters( $output1->getText() ) );
174 }
175 }
176 }
177
178 private static function checkParserLocally( $parserName ) {
179 /* Look for the parser in a file appropiately named in the current folder */
180 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
181 global $wgAutoloadClasses;
182 $wgAutoloadClasses[$parserName] = realpath( '.' ) . "/$parserName.php";
183 }
184 }
185}
186
187$maintClass = CompareParsers::class;
188require_once RUN_MAINTENANCE_IF_MAIN;
$wgAutoloadClasses
Array mapping class names to filenames, for autoloading.
$wgDiff
Path to the GNU diff utility.
wfDiff( $before, $after, $params='-u')
Returns unified plain-text diff of two texts.
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.
static isEnabled()
Definition MWTidy.php:54
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option 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:48
$maintClass
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:244
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function 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
return true to allow those checks to and false if checking is done & $user
Definition hooks.txt:1510
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:1779
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
require_once RUN_MAINTENANCE_IF_MAIN
$content