MediaWiki master
LanguageVariantConverter.php
Go to the documentation of this file.
1<?php
2declare( strict_types = 1 );
3
5
15use Wikimedia\Bcp47Code\Bcp47Code;
16use Wikimedia\Parsoid\Config\SiteConfig;
17use Wikimedia\Parsoid\Core\HtmlPageBundle;
18
24 private readonly Title $pageTitle;
28 private ?Language $pageLanguageOverride = null;
29
31 private ?ParserOptions $parserOptionsForTest = null;
32
33 public function __construct(
34 private readonly OutputTransformPipeline $languageConverterPipeline,
35 private readonly LanguageFactory $languageFactory,
36 private readonly SiteConfig $siteConfig,
37 TitleFactory $titleFactory,
38 private readonly PageIdentity $pageIdentity,
39 ) {
40 $this->pageTitle = $titleFactory->newFromPageIdentity( $pageIdentity );
41 }
42
49 public function setPageLanguageOverride( Bcp47Code $language ) {
50 $this->pageLanguageOverride = $this->languageFactory->getLanguage(
51 $language
52 );
53 }
54
66 public function convertPageBundleVariant(
67 HtmlPageBundle $pageBundle,
68 Bcp47Code $targetVariant,
69 ?Bcp47Code $sourceVariant = null
70 ): HtmlPageBundle {
71 $parserOutput = PageBundleParserOutputConverter::parserOutputFromPageBundle(
72 $pageBundle, title: $this->pageIdentity,
73 siteConfig: $this->siteConfig
74 );
75 $modifiedParserOutput = $this->convertParserOutputVariant(
76 $parserOutput, $targetVariant, $sourceVariant
77 );
79 $modifiedParserOutput,
80 siteConfig: $this->siteConfig,
81 bodyOnly: false,
82 );
83 }
84
95 ParserOutput $parserOutput,
96 Bcp47Code $targetVariant,
97 ?Bcp47Code $sourceVariant = null
98 ): ParserOutput {
99 if ( !$parserOutput->getContentHolder()->isParsoidContent() ) {
100 // Do not convert non-Parsoid HTML (it should be preconverted)
101 return $parserOutput;
102 }
103 $targetVariant = $this->languageFactory->getLanguage( $targetVariant );
104 $popts = $this->parserOptionsForTest ?? ParserOptions::newFromAnon();
105 $popts->setVariant( $targetVariant );
106 $popts->setUseParsoid( true );
107 $parent = fn ( $l ) => $l === null ? null :
108 $this->languageFactory->getParentLanguage( $l ) ??
109 $this->languageFactory->getLanguage( $l );
110 $popts->setTargetLanguage(
111 $parent( $sourceVariant ) ??
112 $parent( $this->pageLanguageOverride ) ??
113 $parent( $parserOutput->getLanguage() ) ??
114 $this->pageTitle->getPageLanguage()
115 );
116 // TEMPORARY during transition to new LanguageConverter
117 // Ensure we can distinguish ParserOutputs created using the
118 // older language converter implementation.
119 $parserOutput->setExtensionData(
120 'core:parsoid-languageconverter', 'postprocess'
121 );
122 return $this->languageConverterPipeline->run(
123 $parserOutput, $popts, []
124 );
125 }
126}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Base class for language-specific code.
Definition Language.php:65
Set options of the Parser.
static newFromAnon()
Get a ParserOptions object for an anonymous user.
ParserOutput is a rendering of a Content object or a message.
__construct(private readonly OutputTransformPipeline $languageConverterPipeline, private readonly LanguageFactory $languageFactory, private readonly SiteConfig $siteConfig, TitleFactory $titleFactory, private readonly PageIdentity $pageIdentity,)
convertPageBundleVariant(HtmlPageBundle $pageBundle, Bcp47Code $targetVariant, ?Bcp47Code $sourceVariant=null)
Perform variant conversion on a HtmlPageBundle object.
convertParserOutputVariant(ParserOutput $parserOutput, Bcp47Code $targetVariant, ?Bcp47Code $sourceVariant=null)
Perform variant conversion on a ParserOutput object.
setPageLanguageOverride(Bcp47Code $language)
Set the page content language override.
Provides methods for conversion between HtmlPageBundle and ParserOutput.
static htmlPageBundleFromParserOutput(ParserOutput $parserOutput, SiteConfig $siteConfig, bool $bodyOnly=false,)
Returns a Parsoid HtmlPageBundle equivalent to the given ParserOutput.
This is the base exception class for non-fatal exceptions thrown from REST handlers.
Creates Title objects.
newFromPageIdentity(PageIdentity $pageIdentity)
Represents a title within MediaWiki.
Definition Title.php:69
Interface for objects (potentially) representing an editable wiki page.