2declare( strict_types = 1 );
24use Wikimedia\Assert\Assert;
25use Wikimedia\Parsoid\Config\PageConfig;
26use Wikimedia\Parsoid\Config\SiteConfig;
27use Wikimedia\Parsoid\Parsoid;
43 private Parsoid $parsoid,
46 private readonly SiteConfig $siteConfig,
60 private function genParserOutput(
76 $htmlVariantLanguage =
null;
99 $langCode = $pageConfig->getPageLanguageBcp47();
103 $lang = $langFactory->getLanguage( $langCode );
104 $langConv = $this->languageConverterFactory->getLanguageConverter( $lang );
105 $htmlVariantLanguage = $langFactory->getLanguage( $langConv->getPreferredVariant() );
107 $htmlVariantLanguage = $langCode;
110 $oldPageConfig =
null;
111 $oldPageBundle =
null;
118 $doSample = ( $sampleRate && mt_rand( 1, $sampleRate ) === 1 );
119 if ( $doSample && $previousOutput !==
null && $previousOutput->
getCacheRevisionId() ) {
122 $oldPageConfig = $this->pageConfigFactory->createFromParserOptions(
124 Title::newFromLinkTarget( $pageConfig->getLinkTarget() ),
130 $previousOutput, $this->siteConfig, bodyOnly: false,
134 'pageBundle' =>
true,
135 'wrapSections' =>
true,
136 'logLinterData' =>
true,
142 'htmlVariantLanguage' => $htmlVariantLanguage,
144 'skipLanguageConversionPass' =>
true,
145 'offsetType' =>
'byte',
146 'outputContentVersion' => Parsoid::defaultHTMLVersion(),
147 'previousOutput' => $oldPageBundle,
148 'previousInput' => $oldPageConfig,
150 'sampleStats' => $doSample,
152 'userAgent' => RequestContext::getMain()->getRequest()->getHeader(
'User-Agent' ),
159 $pageBundle = $this->parsoid->wikitext2html(
166 $pageBundle, $parserOutput,
167 title: $pageConfig->getLinkTarget(),
168 siteConfig: $this->siteConfig,
178 $this->dataAccess->makeLimitReport( $pageConfig, $options, $parserOutput );
184 'type' => $previousOutput ===
null ?
'full' :
'selective',
185 'wiki' => WikiMap::getCurrentWikiId(),
187 'has_async_content' =>
188 $parserOutput->
getOutputFlag( ParserOutputFlags::HAS_ASYNC_CONTENT )
191 $parserOutput->
getOutputFlag( ParserOutputFlags::ASYNC_NOT_READY )
195 ->getCounter(
'Parsoid_parse_cpu_seconds' )
196 ->setLabels( $labels )
199 ->getCounter(
'Parsoid_parse_total' )
200 ->setLabels( $labels )
203 return $this->
addMetadata( $parserOutput, $pageConfig );
215 $parserOutput->addModuleStyles( [
'mediawiki.skinning.content.parsoid' ] );
218 # __NOINDEX__ always overrides __INDEX__, see T16899
219 foreach ( [
'noindex',
'index' ] as $indexSwitch ) {
222 $this->namespaceInfo->canUseNoindex(
223 $pageConfig->getLinkTarget()->getNamespace()
228 $this->trackingCategories->addTrackingCategory(
230 $indexSwitch .
'-category',
231 Title::newFromLinkTarget( $pageConfig->getLinkTarget() ),
238 'core:base-uri', $this->siteConfig->baseURI()
245 'core:parsoid-version', Parsoid::version()
248 'core:html-version', Parsoid::defaultHTMLVersion()
252 'wgParsoidHtmlVersion', Parsoid::defaultHTMLVersion()
258 'core:parsoid-languageconverter',
'postprocess'
261 return $parserOutput;
286 bool $linestart =
true,
bool $clearState =
true, ?
int $revId =
null,
289 Assert::invariant( $linestart,
'$linestart=false is not yet supported' );
290 Assert::invariant( $clearState,
'$clearState=false is not yet supported' );
291 $title = Title::newFromPageReference( $page );
296 $pageConfig = $revId ===
null || $revId === 0 ? null : $this->pageConfigFactory->createFromParserOptions(
305 $text = $content->getText();
307 if ( !( $pageConfig && $pageConfig->getPageMainContent() === $text ) ) {
313 if ( $revId !==
null ) {
314 $revisionRecord->setId( $revId );
316 $revisionRecord->setSlot(
317 SlotRecord::newUnsaved(
319 $content ??
new WikitextContent( $text )
322 $pageConfig = $this->pageConfigFactory->createFromParserOptions(
330 return $this->genParserOutput( $pageConfig, $options, $previousOutput );
Content object implementation for representing flat text.
Content object for wiki text pages.
Group all the pieces relevant to the context of a request into one instance.
A class containing constants representing the names of configuration variables.
const ParsoidSelectiveUpdateSampleRate
Name constant for the ParsoidSelectiveUpdateSampleRate setting, for use with Config::get()
Helper class used by MediaWiki to create Parsoid PageConfig objects.
static htmlPageBundleFromParserOutput(ParserOutput $parserOutput, SiteConfig $siteConfig, bool $bodyOnly=false,)
Returns a Parsoid HtmlPageBundle equivalent to the given ParserOutput.
static parserOutputFromPageBundle(HtmlPageBundle $pageBundle, ?ParserOutput $originalParserOutput=null, ParsoidLinkTarget|PageReference|null $title=null, ?SiteConfig $siteConfig=null,)
Creates a ParserOutput object containing the relevant data from the given HtmlPageBundle object.