| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Parser\Parsoid; |
| 4 |
|
| 5 |
use MediaWiki\Content\TextContent; |
| 6 |
use MediaWiki\Content\WikitextContent; |
| 7 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\MediaWikiServices; |
| 10 |
use MediaWiki\Page\PageReference; |
| 11 |
use MediaWiki\Parser\ParserFactory; |
| 12 |
use MediaWiki\Parser\ParserOptions; |
| 13 |
use MediaWiki\Parser\ParserOutput; |
| 14 |
use MediaWiki\Parser\Parsoid\Config\PageConfigFactory; |
| 15 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 16 |
use MediaWiki\Revision\RevisionRecord; |
| 17 |
use MediaWiki\Revision\SlotRecord; |
| 18 |
use MediaWiki\Title\Title; |
| 19 |
use MediaWiki\WikiMap\WikiMap; |
| 20 |
use Wikimedia\Assert\Assert; |
| 21 |
use Wikimedia\Parsoid\Config\PageConfig; |
| 22 |
use Wikimedia\Parsoid\Parsoid; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ParsoidParser { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public const PARSOID_TITLE_KEY = "parsoid:title-dbkey"; |
| 40 |
private Parsoid $parsoid; |
| 41 |
private PageConfigFactory $pageConfigFactory; |
| 42 |
private LanguageConverterFactory $languageConverterFactory; |
| 43 |
private ParserFactory $legacyParserFactory; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct( |
| 52 |
Parsoid $parsoid, |
| 53 |
PageConfigFactory $pageConfigFactory, |
| 54 |
LanguageConverterFactory $languageConverterFactory, |
| 55 |
ParserFactory $legacyParserFactory |
| 56 |
) { |
| 57 |
$this->parsoid = $parsoid; |
| 58 |
$this->pageConfigFactory = $pageConfigFactory; |
| 59 |
$this->languageConverterFactory = $languageConverterFactory; |
| 60 |
$this->legacyParserFactory = $legacyParserFactory; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private function genParserOutput( |
| 71 |
PageConfig $pageConfig, ParserOptions $options, ?ParserOutput $previousOutput |
| 72 |
): ParserOutput { |
| 73 |
$parserOutput = new ParserOutput(); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
$options->registerWatcher( [ $parserOutput, 'recordOption' ] ); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
$htmlVariantLanguage = null; |
| 87 |
if ( !( $options->getDisableContentConversion() || $options->getInterfaceMessage() ) ) { |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
$langCode = $pageConfig->getPageLanguageBcp47(); |
| 110 |
if ( $options->getRenderReason() === 'page-view' ) { |
| 111 |
$langFactory = MediaWikiServices::getInstance()->getLanguageFactory(); |
| 112 |
$lang = $langFactory->getLanguage( $langCode ); |
| 113 |
$langConv = $this->languageConverterFactory->getLanguageConverter( $lang ); |
| 114 |
$htmlVariantLanguage = $langFactory->getLanguage( $langConv->getPreferredVariant() ); |
| 115 |
} else { |
| 116 |
$htmlVariantLanguage = $langCode; |
| 117 |
} |
| 118 |
} |
| 119 |
$oldPageConfig = null; |
| 120 |
$oldPageBundle = null; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
$sampleRate = MediaWikiServices::getInstance()->getMainConfig()->get( |
| 125 |
MainConfigNames::ParsoidSelectiveUpdateSampleRate |
| 126 |
); |
| 127 |
$doSample = ( $sampleRate && mt_rand( 1, $sampleRate ) === 1 ); |
| 128 |
if ( $doSample && $previousOutput !== null && $previousOutput->getCacheRevisionId() ) { |
| 129 |
|
| 130 |
|
| 131 |
$oldPageConfig = $this->pageConfigFactory->create( |
| 132 |
Title::newFromLinkTarget( $pageConfig->getLinkTarget() ), |
| 133 |
$options->getUserIdentity(), |
| 134 |
$previousOutput->getCacheRevisionId(), |
| 135 |
null, |
| 136 |
$previousOutput->getLanguage(), |
| 137 |
); |
| 138 |
$oldPageBundle = |
| 139 |
PageBundleParserOutputConverter::pageBundleFromParserOutput( |
| 140 |
$previousOutput |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
$defaultOptions = [ |
| 145 |
'pageBundle' => true, |
| 146 |
'wrapSections' => true, |
| 147 |
'logLinterData' => true, |
| 148 |
'body_only' => false, |
| 149 |
'htmlVariantLanguage' => $htmlVariantLanguage, |
| 150 |
'offsetType' => 'byte', |
| 151 |
'outputContentVersion' => Parsoid::defaultHTMLVersion(), |
| 152 |
'previousOutput' => $oldPageBundle, |
| 153 |
'previousInput' => $oldPageConfig, |
| 154 |
'sampleStats' => $doSample, |
| 155 |
'renderReason' => $options->getRenderReason(), |
| 156 |
]; |
| 157 |
|
| 158 |
$parserOutput->resetParseStartTime(); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
$pageBundle = $this->parsoid->wikitext2html( |
| 163 |
$pageConfig, |
| 164 |
$defaultOptions, |
| 165 |
$headers, |
| 166 |
$parserOutput ); |
| 167 |
|
| 168 |
$parserOutput = PageBundleParserOutputConverter::parserOutputFromPageBundle( $pageBundle, $parserOutput ); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
$parserOutput->setExtensionData( |
| 173 |
self::PARSOID_TITLE_KEY, |
| 174 |
Title::newFromLinkTarget( $pageConfig->getLinkTarget() )->getPrefixedDBkey() |
| 175 |
); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
$options->registerWatcher( [ $parserOutput, 'recordOption' ] ); |
| 180 |
|
| 181 |
$parserOutput->setFromParserOptions( $options ); |
| 182 |
|
| 183 |
$parserOutput->recordTimeProfile(); |
| 184 |
$this->makeLimitReport( $options, $parserOutput ); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
$stats = MediaWikiServices::getInstance()->getStatsFactory(); |
| 189 |
$labels = [ |
| 190 |
'type' => $previousOutput === null ? 'full' : 'selective', |
| 191 |
'wiki' => WikiMap::getCurrentWikiId(), |
| 192 |
'reason' => $options->getRenderReason() ?: 'unknown', |
| 193 |
]; |
| 194 |
$stats |
| 195 |
->getCounter( 'Parsoid_parse_cpu_seconds' ) |
| 196 |
->setLabels( $labels ) |
| 197 |
->incrementBy( $parserOutput->getTimeProfile( 'cpu' ) ); |
| 198 |
$stats |
| 199 |
->getCounter( 'Parsoid_parse_total' ) |
| 200 |
->setLabels( $labels ) |
| 201 |
->increment(); |
| 202 |
|
| 203 |
|
| 204 |
$parserOutput->addModuleStyles( [ 'mediawiki.skinning.content.parsoid' ] ); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
$parserOutput->setExtensionData( |
| 210 |
'core:parsoid-version', Parsoid::version() |
| 211 |
); |
| 212 |
$parserOutput->setExtensionData( |
| 213 |
'core:html-version', Parsoid::defaultHTMLVersion() |
| 214 |
); |
| 215 |
|
| 216 |
return $parserOutput; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function parse( |
| 240 |
$text, PageReference $page, ParserOptions $options, |
| 241 |
bool $linestart = true, bool $clearState = true, ?int $revId = null, |
| 242 |
?ParserOutput $previousOutput = null |
| 243 |
): ParserOutput { |
| 244 |
Assert::invariant( $linestart, '$linestart=false is not yet supported' ); |
| 245 |
Assert::invariant( $clearState, '$clearState=false is not yet supported' ); |
| 246 |
$title = Title::newFromPageReference( $page ); |
| 247 |
$lang = $options->getTargetLanguage(); |
| 248 |
if ( $lang === null && $options->getInterfaceMessage() ) { |
| 249 |
$lang = $options->getUserLangObj(); |
| 250 |
} |
| 251 |
$pageConfig = $revId === null || $revId === 0 ? null : $this->pageConfigFactory->create( |
| 252 |
$title, |
| 253 |
$options->getUserIdentity(), |
| 254 |
$revId, |
| 255 |
null, |
| 256 |
$lang |
| 257 |
); |
| 258 |
$content = null; |
| 259 |
if ( $text instanceof TextContent ) { |
| 260 |
$content = $text; |
| 261 |
$text = $content->getText(); |
| 262 |
} |
| 263 |
if ( !( $pageConfig && $pageConfig->getPageMainContent() === $text ) ) { |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
$revisionRecord = new MutableRevisionRecord( $title ); |
| 269 |
if ( $revId !== null ) { |
| 270 |
$revisionRecord->setId( $revId ); |
| 271 |
} |
| 272 |
$revisionRecord->setSlot( |
| 273 |
SlotRecord::newUnsaved( |
| 274 |
SlotRecord::MAIN, |
| 275 |
$content ?? new WikitextContent( $text ) |
| 276 |
) |
| 277 |
); |
| 278 |
$pageConfig = $this->pageConfigFactory->create( |
| 279 |
$title, |
| 280 |
$options->getUserIdentity(), |
| 281 |
$revisionRecord, |
| 282 |
null, |
| 283 |
$lang |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
return $this->genParserOutput( $pageConfig, $options, $previousOutput ); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public function parseFakeRevision( |
| 303 |
RevisionRecord $fakeRev, PageReference $page, ParserOptions $options |
| 304 |
): ParserOutput { |
| 305 |
wfDeprecated( __METHOD__, '1.43' ); |
| 306 |
$title = Title::newFromPageReference( $page ); |
| 307 |
$lang = $options->getTargetLanguage(); |
| 308 |
if ( $lang === null && $options->getInterfaceMessage() ) { |
| 309 |
$lang = $options->getUserLangObj(); |
| 310 |
} |
| 311 |
$pageConfig = $this->pageConfigFactory->create( |
| 312 |
$title, |
| 313 |
$options->getUserIdentity(), |
| 314 |
$fakeRev, |
| 315 |
null, |
| 316 |
$lang |
| 317 |
); |
| 318 |
|
| 319 |
return $this->genParserOutput( $pageConfig, $options, null ); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
protected function makeLimitReport( |
| 328 |
ParserOptions $parserOptions, ParserOutput $parserOutput |
| 329 |
) { |
| 330 |
$maxIncludeSize = $parserOptions->getMaxIncludeSize(); |
| 331 |
|
| 332 |
$cpuTime = $parserOutput->getTimeProfile( 'cpu' ); |
| 333 |
if ( $cpuTime !== null ) { |
| 334 |
$parserOutput->setLimitReportData( 'limitreport-cputime', |
| 335 |
sprintf( "%.3f", $cpuTime ) |
| 336 |
); |
| 337 |
} |
| 338 |
|
| 339 |
$wallTime = $parserOutput->getTimeProfile( 'wall' ); |
| 340 |
$parserOutput->setLimitReportData( 'limitreport-walltime', |
| 341 |
sprintf( "%.3f", $wallTime ) |
| 342 |
); |
| 343 |
|
| 344 |
$parserOutput->setLimitReportData( 'limitreport-timingprofile', [ 'not yet supported' ] ); |
| 345 |
|
| 346 |
|
| 347 |
$parserOutput->setLimitReportData( 'cachereport-timestamp', |
| 348 |
$parserOutput->getCacheTime() ); |
| 349 |
$parserOutput->setLimitReportData( 'cachereport-ttl', |
| 350 |
$parserOutput->getCacheExpiry() ); |
| 351 |
$parserOutput->setLimitReportData( 'cachereport-transientcontent', |
| 352 |
$parserOutput->hasReducedExpiry() ); |
| 353 |
} |
| 354 |
|
| 355 |
} |