14use Wikimedia\Bcp47Code\Bcp47Code;
15use Wikimedia\Bcp47Code\Bcp47CodeValue;
16use Wikimedia\Parsoid\Config\PageConfig;
17use Wikimedia\Parsoid\Config\SiteConfig;
18use Wikimedia\Parsoid\Core\PageBundle;
19use Wikimedia\Parsoid\Parsoid;
27 private $pageConfigFactory;
33 private $pageIdentity;
42 private $parsoidSettings;
48 private $titleFactory;
51 private $languageConverterFactory;
54 private $languageFactory;
60 private $pageLanguageOverride;
63 private $isFallbackLanguageConverterEnabled =
true;
69 array $parsoidSettings,
75 $this->pageConfigFactory = $pageConfigFactory;
76 $this->pageIdentity = $pageIdentity;
77 $this->parsoid = $parsoid;
78 $this->parsoidSettings = $parsoidSettings;
79 $this->siteConfig = $siteConfig;
80 $this->titleFactory = $titleFactory;
83 $this->languageConverterFactory = $languageConverterFactory;
84 $this->languageFactory = $languageFactory;
85 $this->pageLanguageOverride =
null;
96 $this->pageConfig = $pageConfig;
106 $this->pageLanguageOverride = $language;
121 PageBundle $pageBundle,
122 Bcp47Code $targetVariant,
123 ?Bcp47Code $sourceVariant =
null
125 [ $pageLanguage, $sourceVariant ] =
126 $this->getBaseAndSourceLanguage( $pageBundle, $sourceVariant );
128 if ( !$this->siteConfig->langConverterEnabledBcp47( $pageLanguage ) ) {
133 $pageConfig = $this->getPageConfig( $pageLanguage, $sourceVariant );
135 if ( !$this->parsoid->implementsLanguageConversionBcp47( $pageConfig, $targetVariant ) ) {
136 if ( !$this->isFallbackLanguageConverterEnabled ) {
144 $baseLanguage = $this->languageFactory->getParentLanguage( $targetVariant );
145 $languageConverter = $this->languageConverterFactory->getLanguageConverter( $baseLanguage );
146 $targetVariantCode = $this->languageFactory->getLanguage( $targetVariant )->getCode();
147 if ( $languageConverter->hasVariant( $targetVariantCode ) ) {
148 $convertedHtml = $languageConverter->convertTo( $pageBundle->html, $targetVariantCode );
151 $convertedHtml = $pageBundle->html;
155 $msg =
"<!-- Variant conversion performed using the core LanguageConverter -->";
156 $convertedHtml = $msg . $convertedHtml;
162 $pageBundle =
new PageBundle(
166 $pageBundle->version,
167 [
'content-language' => $targetVariant->toBcp47Code() ]
171 $modifiedPageBundle = $this->parsoid->pb2pb(
172 $pageConfig,
'variant', $pageBundle,
175 'source' => $sourceVariant,
176 'target' => $targetVariant,
181 return $modifiedPageBundle;
195 Bcp47Code $targetVariant,
196 ?Bcp47Code $sourceVariant =
null
199 $modifiedPageBundle = $this->convertPageBundleVariant( $pageBundle, $targetVariant, $sourceVariant );
209 $this->isFallbackLanguageConverterEnabled = false;
212 private function getPageConfig( Bcp47Code $pageLanguage, ?Bcp47Code $sourceVariant ):
PageConfig {
213 if ( $this->pageConfig ) {
214 return $this->pageConfig;
218 $this->pageConfig = $this->pageConfigFactory->create(
224 $this->parsoidSettings
227 if ( $sourceVariant ) {
228 $this->pageConfig->setVariantBcp47( $sourceVariant );
230 }
catch ( RevisionAccessException $exception ) {
233 throw new HttpException(
'The specified revision is deleted or suppressed.', 404 );
236 return $this->pageConfig;
265 private function getPageLanguage( PageBundle $pageBundle, ?Bcp47Code $default =
null ): Bcp47Code {
267 if ( $this->pageLanguageOverride ) {
268 return $this->pageLanguageOverride;
272 $pageBundleLanguage = $pageBundle->headers[
'content-language' ] ??
null;
273 if ( $pageBundleLanguage ) {
276 return new Bcp47CodeValue( $pageBundleLanguage );
287 if ( $this->pageConfig ) {
288 return $this->pageConfig->getPageLanguageBcp47();
293 return $this->pageTitle->getPageLanguage();
311 private function getBaseAndSourceLanguage( PageBundle $pageBundle, ?Bcp47Code $sourceLanguage ): array {
314 $baseLanguage = $this->getPageLanguage( $pageBundle, $sourceLanguage );
317 $parentLang = $this->languageFactory->getParentLanguage( $baseLanguage );
323 if ( $parentLang && strcasecmp( $parentLang->toBcp47Code(), $baseLanguage->toBcp47Code() ) !== 0 ) {
324 if ( !$sourceLanguage ) {
325 $sourceLanguage = $baseLanguage;
327 $baseLanguage = $parentLang;
333 if ( $sourceLanguage && strcasecmp( $sourceLanguage->toBcp47Code(), $baseLanguage->toBcp47Code() ) === 0 ) {
334 $sourceLanguage =
null;
337 return [ $baseLanguage, $sourceLanguage ];
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Helper class used by MediaWiki to create Parsoid PageConfig objects.
Page-level configuration interface for Parsoid.
Provides methods for conversion between PageBundle and ParserOutput TODO: Convert to a trait once we ...
static parserOutputFromPageBundle(PageBundle $pageBundle, ?ParserOutput $parserOutput=null)
Creates a ParserOutput object containing the relevant data from the given PageBundle object.
Interface for objects (potentially) representing an editable wiki page.