21use Wikimedia\Assert\Assert;
22use Wikimedia\Parsoid\Config\PageConfig;
23use Wikimedia\Parsoid\Parsoid;
41 private Parsoid $parsoid;
58 $this->parsoid = $parsoid;
59 $this->pageConfigFactory = $pageConfigFactory;
60 $this->languageConverterFactory = $languageConverterFactory;
61 $this->legacyParserFactory = $legacyParserFactory;
71 private function genParserOutput(
87 $htmlVariantLanguage =
null;
110 $langCode = $pageConfig->getPageLanguageBcp47();
113 $lang = $langFactory->getLanguage( $langCode );
114 $langConv = $this->languageConverterFactory->getLanguageConverter( $lang );
115 $htmlVariantLanguage = $langFactory->getLanguage( $langConv->getPreferredVariant() );
117 $htmlVariantLanguage = $langCode;
120 $oldPageConfig =
null;
121 $oldPageBundle =
null;
128 $doSample = ( $sampleRate && mt_rand( 1, $sampleRate ) === 1 );
129 if ( $doSample && $previousOutput !==
null && $previousOutput->
getCacheRevisionId() ) {
132 $oldPageConfig = $this->pageConfigFactory->create(
133 Title::newFromLinkTarget( $pageConfig->getLinkTarget() ),
146 'pageBundle' =>
true,
147 'wrapSections' =>
true,
148 'logLinterData' =>
true,
149 'body_only' =>
false,
150 'htmlVariantLanguage' => $htmlVariantLanguage,
151 'offsetType' =>
'byte',
152 'outputContentVersion' => Parsoid::defaultHTMLVersion(),
153 'previousOutput' => $oldPageBundle,
154 'previousInput' => $oldPageConfig,
156 'sampleStats' => $doSample,
158 'userAgent' => RequestContext::getMain()->getRequest()->getHeader(
'User-Agent' ),
161 $parserOutput->resetParseStartTime();
165 $pageBundle = $this->parsoid->wikitext2html(
175 $parserOutput->setExtensionData(
176 self::PARSOID_TITLE_KEY,
177 Title::newFromLinkTarget( $pageConfig->getLinkTarget() )->getPrefixedDBkey()
184 $parserOutput->setFromParserOptions( $options );
186 $parserOutput->recordTimeProfile();
193 'type' => $previousOutput ===
null ?
'full' :
'selective',
194 'wiki' => WikiMap::getCurrentWikiId(),
198 ->getCounter(
'Parsoid_parse_cpu_seconds' )
199 ->setLabels( $labels )
200 ->incrementBy( $parserOutput->getTimeProfile(
'cpu' ) );
202 ->getCounter(
'Parsoid_parse_total' )
203 ->setLabels( $labels )
207 $parserOutput->addModuleStyles( [
'mediawiki.skinning.content.parsoid' ] );
212 $parserOutput->setExtensionData(
213 'core:parsoid-version', Parsoid::version()
215 $parserOutput->setExtensionData(
216 'core:html-version', Parsoid::defaultHTMLVersion()
219 return $parserOutput;
244 bool $linestart =
true,
bool $clearState =
true, ?
int $revId =
null,
247 Assert::invariant( $linestart,
'$linestart=false is not yet supported' );
248 Assert::invariant( $clearState,
'$clearState=false is not yet supported' );
249 $title = Title::newFromPageReference( $page );
254 $pageConfig = $revId ===
null || $revId === 0 ? null : $this->pageConfigFactory->create(
264 $text = $content->getText();
266 if ( !( $pageConfig && $pageConfig->getPageMainContent() === $text ) ) {
272 if ( $revId !==
null ) {
273 $revisionRecord->setId( $revId );
275 $revisionRecord->setSlot(
276 SlotRecord::newUnsaved(
278 $content ??
new WikitextContent( $text )
281 $pageConfig = $this->pageConfigFactory->create(
290 return $this->genParserOutput( $pageConfig, $options, $previousOutput );
309 $title = Title::newFromPageReference( $page );
314 $pageConfig = $this->pageConfigFactory->create(
322 return $this->genParserOutput( $pageConfig, $options,
null );
336 if ( $cpuTime !==
null ) {
338 sprintf(
"%.3f", $cpuTime )
344 sprintf(
"%.3f", $wallTime )
347 $parserOutput->
setLimitReportData(
'limitreport-timingprofile', [
'not yet supported' ] );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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 pageBundleFromParserOutput(ParserOutput $parserOutput)
Returns a Parsoid PageBundle equivalent to the given ParserOutput.
static parserOutputFromPageBundle(PageBundle $pageBundle, ?ParserOutput $originalParserOutput=null)
Creates a ParserOutput object containing the relevant data from the given PageBundle object.