43 parent::__construct();
44 $this->
addDescription(
'Compares variant conversion output between Parser and HtmlOutputRendererHelper' );
47 'Name of the page to be parsed and compared',
52 'Target variant language code to transform the content to',
60 $pageName = $this->
getArg(
'page-title' );
61 $pageTitle = Title::newFromText( $pageName );
63 if ( !$pageTitle || !$pageTitle->
exists() ) {
64 $this->
fatalError(
"Title with name $pageName not found" );
67 $targetVariantCode = $this->
getArg(
'target-variant' );
68 $languageNameUtils = $mwInstance->getLanguageNameUtils();
69 if ( !$languageNameUtils->isValidBuiltInCode( $targetVariantCode ) ) {
70 $this->
fatalError(
"$targetVariantCode is not a supported variant" );
72 $targetVariant = $mwInstance->getLanguageFactory()->getLanguage(
76 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
79 $parserOutput = $this->getParserOutput( $pageTitle, $baseLanguage, $targetVariant );
80 $parsoidOutput = $this->getParsoidOutput( $pageTitle, $targetVariant, $user );
81 $converterUsed = $this->getConverterUsed( $parsoidOutput );
83 $this->compareOutput( $parserOutput, $parsoidOutput, $converterUsed );
92 $services->getParsoidOutputStash(),
94 $services->getParsoidOutputAccess(),
95 $services->getHtmlTransformFactory(),
96 $services->getContentHandlerFactory(),
97 $services->getLanguageFactory()
105 $parserOpts->disableContentConversion(
false );
106 $parserOpts->disableTitleConversion(
false );
111 private function getParserOutput(
121 $mwInstance = $this->getServiceContainer();
123 $languageFactory = $mwInstance->getLanguageFactory();
124 $parser = $mwInstance->getParser();
125 $parserOptions = $this->getParserOptions(
126 $languageFactory->getParentLanguage( $baseLanguage )
129 $content = $mwInstance->getRevisionLookup()
130 ->getRevisionByTitle( $pageTitle )
131 ->getContent( SlotRecord::MAIN );
132 $wikiContent = ( $content instanceof
TextContent ) ? $content->getText() :
'';
134 return $parser->parse( $wikiContent, $pageTitle, $parserOptions );
137 private function getParsoidOutput(
139 Bcp47Code $targetVariant,
142 $htmlOutputRendererHelper = $this->newHtmlOutputRendererHelper();
143 $htmlOutputRendererHelper->init( $pageTitle, [
147 $htmlOutputRendererHelper->setVariantConversionLanguage( $targetVariant );
149 return $htmlOutputRendererHelper->getHtml();
152 private function getWords(
string $output ): array {
153 $tagsRemoved = strip_tags( $output );
154 $words = preg_split(
'/\s+/', trim( $tagsRemoved ), -1, PREG_SPLIT_NO_EMPTY );
158 private function getBody(
string $output ): string {
159 $dom = new DOMDocument();
161 @$dom->loadHTML( $output );
162 $body = $dom->getElementsByTagName(
'body' )->item( 0 );
163 if ( $body ===
null ) {
168 return $body->textContent;
171 private function compareOutput(
174 string $converterUsed
176 $parsoidText = $parsoidOutput->getText( [
'deduplicateStyles' => false ] );
177 $parserText = $parserOutput->
getText( [
'deduplicateStyles' =>
false ] );
179 $parsoidWords = $this->getWords( $this->getBody( $parsoidText ) );
180 $parserWords = $this->getWords( $parserText );
182 $parserWordCount = count( $parserWords );
183 $parsoidWordCount = count( $parsoidWords );
184 $this->output(
"Word count: Parsoid: $parsoidWordCount; Parser: $parserWordCount\n" );
186 $this->outputSimilarity( $parsoidWords, $parserWords );
187 $this->output(
"\n" );
188 $this->outputDiff( $parsoidWords, $parserWords, $converterUsed );
191 private function getConverterUsed(
ParserOutput $parsoidOutput ): string {
192 $isCoreConverterUsed = strpos(
193 $parsoidOutput->getRawText(),
194 'Variant conversion performed using the core LanguageConverter'
197 if ( $isCoreConverterUsed ) {
198 return 'Core LanguageConverter';
200 return 'Parsoid LanguageConverter';
205 private function mb_sprintf(
string $format, ...$args ): string {
209 preg_replace_callback(
210 '/(?<=%|%-)\d+(?=s)/',
212 $value = array_shift(
$params );
214 return (
string)( strlen( $value ) - mb_strlen( $value ) +
$matches[0] );
222 private function outputSimilarity( array $parsoidWords, array $parserWords ): void {
223 $parsoidOutput = implode(
' ', $parsoidWords );
224 $parserOutput = implode(
' ', $parserWords );
226 'Total characters: Parsoid: ' . strlen( $parsoidOutput ) .
227 '; Parser: ' . strlen( $parserOutput ) .
"\n"
230 $similarityPercent = 0;
231 $similarCharacters = similar_text( $parsoidOutput, $parserOutput, $similarityPercent );
232 $similarityPercent = round( $similarityPercent, 2 );
235 "Similarity via similar_text(): $similarityPercent%; Similar characters: $similarCharacters"
239 private function outputDiff( array $parsoidWords, array $parserWords,
string $converterUsed ): void {
240 $out = str_repeat(
'-', 96 ) .
"\n";
241 $out .= sprintf(
"| %5s | %-35s | %-35s | %-8s |\n",
'Line',
'Parsoid',
'Parser',
'Diff' );
242 $out .= sprintf(
"| %5s | %-35s | %-35s | %-8s |\n",
'',
"($converterUsed)",
'',
'' );
243 $out .= str_repeat(
'-', 96 ) .
"\n";
246 $diff =
new Diff( $parsoidWords, $parserWords );
248 $this->output( $e->getMessage() );
249 $this->error(
'Encountered ComplexityException while computing diff' );
254 foreach ( $wordDiffFormat as $index => $wordDiff ) {
255 $action = $wordDiff[
'action'];
256 $old = $wordDiff[
'old'] ??
null;
257 $new = $wordDiff[
'new'] ??
null;
259 $out .= $this->mb_sprintf(
260 "| %5s | %-35s | %-35s | %-8s |\n",
261 str_pad( (
string)( $index + 1 ), 5,
' ', STR_PAD_LEFT ),
262 mb_strimwidth( $old ??
'- N/A -', 0, 35,
'…' ),
263 mb_strimwidth( $new ??
'- N/A -', 0, 35,
'…' ),
269 $out .= str_repeat(
'-', 96 ) .
"\n";
270 $this->output(
"\n" . $out );
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.