2023-01-10 10:07:09 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2024-06-05 15:49:49 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2024-05-19 18:33:58 +00:00
|
|
|
use MediaWiki\Content\TextContent;
|
2024-09-27 20:18:58 +00:00
|
|
|
use MediaWiki\Language\Language;
|
2024-10-21 17:06:13 +00:00
|
|
|
use MediaWiki\Parser\ParserOptions;
|
2023-12-14 19:20:33 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2024-06-05 15:49:49 +00:00
|
|
|
use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory;
|
2023-01-10 10:07:09 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
use Wikimedia\Bcp47Code\Bcp47Code;
|
2023-06-20 04:02:04 +00:00
|
|
|
use Wikimedia\Diff\ArrayDiffFormatter;
|
|
|
|
|
use Wikimedia\Diff\ComplexityException;
|
|
|
|
|
use Wikimedia\Diff\Diff;
|
2024-09-27 18:21:33 +00:00
|
|
|
use Wikimedia\Stats\NullStatsdDataFactory;
|
2024-09-17 11:16:03 +00:00
|
|
|
use Wikimedia\Stats\StatsFactory;
|
2023-01-10 10:07:09 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2023-01-10 10:07:09 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2023-01-10 10:07:09 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-01-17 10:28:12 +00:00
|
|
|
* Maintenance script that compares variant conversion output between Parser and
|
|
|
|
|
* HtmlOutputRendererHelper.
|
2023-01-10 10:07:09 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
class CompareLanguageConverterOutput extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addDescription( 'Compares variant conversion output between Parser and HtmlOutputRendererHelper' );
|
|
|
|
|
$this->addArg(
|
|
|
|
|
'page-title',
|
|
|
|
|
'Name of the page to be parsed and compared',
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addArg(
|
|
|
|
|
'target-variant',
|
|
|
|
|
'Target variant language code to transform the content to',
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2023-08-31 09:21:12 +00:00
|
|
|
$mwInstance = $this->getServiceContainer();
|
2023-01-10 10:07:09 +00:00
|
|
|
|
|
|
|
|
$pageName = $this->getArg( 'page-title' );
|
|
|
|
|
$pageTitle = Title::newFromText( $pageName );
|
|
|
|
|
|
|
|
|
|
if ( !$pageTitle || !$pageTitle->exists() ) {
|
|
|
|
|
$this->fatalError( "Title with name $pageName not found" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$targetVariantCode = $this->getArg( 'target-variant' );
|
|
|
|
|
$languageNameUtils = $mwInstance->getLanguageNameUtils();
|
|
|
|
|
if ( !$languageNameUtils->isValidBuiltInCode( $targetVariantCode ) ) {
|
|
|
|
|
$this->fatalError( "$targetVariantCode is not a supported variant" );
|
|
|
|
|
}
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$targetVariant = $mwInstance->getLanguageFactory()->getLanguage(
|
|
|
|
|
$targetVariantCode
|
|
|
|
|
);
|
2023-01-10 10:07:09 +00:00
|
|
|
|
|
|
|
|
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
|
|
|
|
|
$baseLanguage = $pageTitle->getPageLanguage();
|
|
|
|
|
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$parserOutput = $this->getParserOutput( $pageTitle, $baseLanguage, $targetVariant );
|
|
|
|
|
$parsoidOutput = $this->getParsoidOutput( $pageTitle, $targetVariant, $user );
|
2023-01-10 10:07:09 +00:00
|
|
|
$converterUsed = $this->getConverterUsed( $parsoidOutput );
|
|
|
|
|
|
2024-07-30 17:13:18 +00:00
|
|
|
$this->compareOutput( $parserOutput->getContentHolderText(),
|
|
|
|
|
$parsoidOutput->getText( [ 'deduplicateStyles' => false ] ), $converterUsed );
|
2023-01-10 10:07:09 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 15:49:49 +00:00
|
|
|
private function newPageRestHelperFactory(): PageRestHelperFactory {
|
2023-08-31 09:21:12 +00:00
|
|
|
$services = $this->getServiceContainer();
|
2023-01-10 10:07:09 +00:00
|
|
|
|
2024-06-05 15:49:49 +00:00
|
|
|
$factory = new PageRestHelperFactory(
|
|
|
|
|
new ServiceOptions( PageRestHelperFactory::CONSTRUCTOR_OPTIONS, $services->getMainConfig() ),
|
|
|
|
|
$services->getRevisionLookup(),
|
2024-02-08 21:07:04 +00:00
|
|
|
$services->getRevisionRenderer(),
|
2024-06-05 15:49:49 +00:00
|
|
|
$services->getTitleFormatter(),
|
|
|
|
|
$services->getPageStore(),
|
2023-01-10 10:07:09 +00:00
|
|
|
$services->getParsoidOutputStash(),
|
|
|
|
|
new NullStatsdDataFactory(),
|
2024-05-15 16:57:56 +00:00
|
|
|
$services->getParserOutputAccess(),
|
|
|
|
|
$services->getParsoidSiteConfig(),
|
2023-01-10 10:07:09 +00:00
|
|
|
$services->getHtmlTransformFactory(),
|
|
|
|
|
$services->getContentHandlerFactory(),
|
2024-06-05 15:49:49 +00:00
|
|
|
$services->getLanguageFactory(),
|
|
|
|
|
$services->getRedirectStore(),
|
2024-09-05 15:33:25 +00:00
|
|
|
$services->getLanguageConverterFactory(),
|
|
|
|
|
$services->getTitleFactory(),
|
|
|
|
|
$services->getConnectionProvider(),
|
2024-09-17 11:16:03 +00:00
|
|
|
$services->getChangeTagsStore(),
|
|
|
|
|
StatsFactory::newNull()
|
2023-01-10 10:07:09 +00:00
|
|
|
);
|
2024-06-05 15:49:49 +00:00
|
|
|
return $factory;
|
2023-01-10 10:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getParserOptions( Language $language ): ParserOptions {
|
|
|
|
|
$parserOpts = ParserOptions::newFromAnon();
|
|
|
|
|
$parserOpts->setTargetLanguage( $language );
|
|
|
|
|
$parserOpts->disableContentConversion( false );
|
|
|
|
|
$parserOpts->disableTitleConversion( false );
|
|
|
|
|
|
|
|
|
|
return $parserOpts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getParserOutput(
|
|
|
|
|
Title $pageTitle,
|
|
|
|
|
Language $baseLanguage,
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
Language $targetVariant
|
2023-01-10 10:07:09 +00:00
|
|
|
): ParserOutput {
|
2023-01-17 10:28:12 +00:00
|
|
|
// We update the default language variant because we want Parser to
|
|
|
|
|
// perform variant conversion to it.
|
2023-01-10 10:07:09 +00:00
|
|
|
global $wgDefaultLanguageVariant;
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$wgDefaultLanguageVariant = $targetVariant->getCode();
|
2023-01-10 10:07:09 +00:00
|
|
|
|
2023-08-31 09:21:12 +00:00
|
|
|
$mwInstance = $this->getServiceContainer();
|
2023-01-10 10:07:09 +00:00
|
|
|
|
|
|
|
|
$languageFactory = $mwInstance->getLanguageFactory();
|
|
|
|
|
$parser = $mwInstance->getParser();
|
|
|
|
|
$parserOptions = $this->getParserOptions(
|
2022-10-12 16:49:02 +00:00
|
|
|
$languageFactory->getParentLanguage( $baseLanguage )
|
2023-01-10 10:07:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$content = $mwInstance->getRevisionLookup()
|
|
|
|
|
->getRevisionByTitle( $pageTitle )
|
|
|
|
|
->getContent( SlotRecord::MAIN );
|
|
|
|
|
$wikiContent = ( $content instanceof TextContent ) ? $content->getText() : '';
|
|
|
|
|
|
2024-07-30 17:13:18 +00:00
|
|
|
$po = $parser->parse( $wikiContent, $pageTitle, $parserOptions );
|
|
|
|
|
// TODO T371008 consider if using the Content framework makes sense instead of creating the pipeline
|
2024-09-04 12:59:26 +00:00
|
|
|
$pipeline = $mwInstance->getDefaultOutputPipeline();
|
2024-07-30 17:13:18 +00:00
|
|
|
$options = [ 'deduplicateStyles' => false ];
|
|
|
|
|
return $pipeline->run( $po, $parserOptions, $options );
|
2023-01-10 10:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getParsoidOutput(
|
|
|
|
|
Title $pageTitle,
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
Bcp47Code $targetVariant,
|
2023-01-10 10:07:09 +00:00
|
|
|
User $user
|
|
|
|
|
): ParserOutput {
|
2024-06-05 15:49:49 +00:00
|
|
|
$htmlOutputRendererHelper = $this->newPageRestHelperFactory()->newHtmlOutputRendererHelper( $pageTitle, [
|
2023-01-10 10:07:09 +00:00
|
|
|
'stash' => false,
|
|
|
|
|
'flavor' => 'view',
|
|
|
|
|
], $user );
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$htmlOutputRendererHelper->setVariantConversionLanguage( $targetVariant );
|
2023-01-10 10:07:09 +00:00
|
|
|
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
return $htmlOutputRendererHelper->getHtml();
|
2023-01-10 10:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getWords( string $output ): array {
|
|
|
|
|
$tagsRemoved = strip_tags( $output );
|
2023-01-17 10:28:12 +00:00
|
|
|
$words = preg_split( '/\s+/', trim( $tagsRemoved ), -1, PREG_SPLIT_NO_EMPTY );
|
2023-01-10 10:07:09 +00:00
|
|
|
return $words;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getBody( string $output ): string {
|
|
|
|
|
$dom = new DOMDocument();
|
|
|
|
|
// phpcs:disable Generic.PHP.NoSilencedErrors.Discouraged
|
|
|
|
|
@$dom->loadHTML( $output );
|
|
|
|
|
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
|
|
|
|
|
if ( $body === null ) {
|
|
|
|
|
// Body element not present
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $body->textContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function compareOutput(
|
2024-07-30 17:13:18 +00:00
|
|
|
string $parserText,
|
|
|
|
|
string $parsoidText,
|
2023-01-10 10:07:09 +00:00
|
|
|
string $converterUsed
|
|
|
|
|
): void {
|
2023-01-17 10:28:12 +00:00
|
|
|
$parsoidWords = $this->getWords( $this->getBody( $parsoidText ) );
|
|
|
|
|
$parserWords = $this->getWords( $parserText );
|
2023-01-10 10:07:09 +00:00
|
|
|
|
|
|
|
|
$parserWordCount = count( $parserWords );
|
|
|
|
|
$parsoidWordCount = count( $parsoidWords );
|
|
|
|
|
$this->output( "Word count: Parsoid: $parsoidWordCount; Parser: $parserWordCount\n" );
|
|
|
|
|
|
|
|
|
|
$this->outputSimilarity( $parsoidWords, $parserWords );
|
2023-01-17 10:28:12 +00:00
|
|
|
$this->output( "\n" );
|
|
|
|
|
$this->outputDiff( $parsoidWords, $parserWords, $converterUsed );
|
2023-01-10 10:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getConverterUsed( ParserOutput $parsoidOutput ): string {
|
|
|
|
|
$isCoreConverterUsed = strpos(
|
|
|
|
|
$parsoidOutput->getRawText(),
|
|
|
|
|
'Variant conversion performed using the core LanguageConverter'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $isCoreConverterUsed ) {
|
|
|
|
|
return 'Core LanguageConverter';
|
|
|
|
|
} else {
|
|
|
|
|
return 'Parsoid LanguageConverter';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Inspired from: https://stackoverflow.com/a/55927237/903324
|
|
|
|
|
private function mb_sprintf( string $format, ...$args ): string {
|
|
|
|
|
$params = $args;
|
|
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
|
preg_replace_callback(
|
|
|
|
|
'/(?<=%|%-)\d+(?=s)/',
|
|
|
|
|
static function ( array $matches ) use ( &$params ) {
|
|
|
|
|
$value = array_shift( $params );
|
|
|
|
|
|
|
|
|
|
return (string)( strlen( $value ) - mb_strlen( $value ) + $matches[0] );
|
|
|
|
|
},
|
|
|
|
|
$format
|
|
|
|
|
),
|
|
|
|
|
...$args
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function outputSimilarity( array $parsoidWords, array $parserWords ): void {
|
|
|
|
|
$parsoidOutput = implode( ' ', $parsoidWords );
|
|
|
|
|
$parserOutput = implode( ' ', $parserWords );
|
|
|
|
|
$this->output(
|
|
|
|
|
'Total characters: Parsoid: ' . strlen( $parsoidOutput ) .
|
|
|
|
|
'; Parser: ' . strlen( $parserOutput ) . "\n"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$similarityPercent = 0;
|
|
|
|
|
$similarCharacters = similar_text( $parsoidOutput, $parserOutput, $similarityPercent );
|
|
|
|
|
$similarityPercent = round( $similarityPercent, 2 );
|
|
|
|
|
|
|
|
|
|
$this->output(
|
|
|
|
|
"Similarity via similar_text(): $similarityPercent%; Similar characters: $similarCharacters"
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-01-17 10:28:12 +00:00
|
|
|
|
|
|
|
|
private function outputDiff( array $parsoidWords, array $parserWords, string $converterUsed ): void {
|
|
|
|
|
$out = str_repeat( '-', 96 ) . "\n";
|
|
|
|
|
$out .= sprintf( "| %5s | %-35s | %-35s | %-8s |\n", 'Line', 'Parsoid', 'Parser', 'Diff' );
|
|
|
|
|
$out .= sprintf( "| %5s | %-35s | %-35s | %-8s |\n", '', "($converterUsed)", '', '' );
|
|
|
|
|
$out .= str_repeat( '-', 96 ) . "\n";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$diff = new Diff( $parsoidWords, $parserWords );
|
|
|
|
|
} catch ( ComplexityException $e ) {
|
|
|
|
|
$this->output( $e->getMessage() );
|
|
|
|
|
$this->error( 'Encountered ComplexityException while computing diff' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print the difference between the words
|
|
|
|
|
$wordDiffFormat = ( new ArrayDiffFormatter() )->format( $diff );
|
|
|
|
|
foreach ( $wordDiffFormat as $index => $wordDiff ) {
|
|
|
|
|
$action = $wordDiff['action'];
|
|
|
|
|
$old = $wordDiff['old'] ?? null;
|
|
|
|
|
$new = $wordDiff['new'] ?? null;
|
|
|
|
|
|
|
|
|
|
$out .= $this->mb_sprintf(
|
|
|
|
|
"| %5s | %-35s | %-35s | %-8s |\n",
|
|
|
|
|
str_pad( (string)( $index + 1 ), 5, ' ', STR_PAD_LEFT ),
|
|
|
|
|
mb_strimwidth( $old ?? '- N/A -', 0, 35, '…' ),
|
|
|
|
|
mb_strimwidth( $new ?? '- N/A -', 0, 35, '…' ),
|
|
|
|
|
$action
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print the footer.
|
|
|
|
|
$out .= str_repeat( '-', 96 ) . "\n";
|
|
|
|
|
$this->output( "\n" . $out );
|
|
|
|
|
}
|
2023-01-10 10:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2023-01-10 10:07:09 +00:00
|
|
|
$maintClass = CompareLanguageConverterOutput::class;
|
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|