We need the output content language when fetching HTML in VE so this needs to match whatever parsoid gives us. In order for this to happen, we need to loop in more data to the parser output after parsing. This patch adds that more relevant data and exposes it via a public method: `getHtmlOutputContentLanguage()` In addition, this patch fixes a bug that was introduced in the PageBundleParserOutputConverterTest when setting extension data on parser output (L#64). Follow-up: I33076c359ee45719c1c4ef63f77c1f1285951d0c (test fix) Change-Id: I06bf9f575ed5a2521cf4b2c42fc6e0e7faab6bc0
73 lines
1.9 KiB
PHP
73 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Parser\Parsoid;
|
|
|
|
use ParserOutput;
|
|
use Wikimedia\Parsoid\Core\PageBundle;
|
|
|
|
/**
|
|
* Provides methods for conversion between PageBundle and ParserOutput
|
|
* TODO: Convert to a trait once we drop support for PHP < 8.2 since
|
|
* support for constants in traits was added in PHP 8.2
|
|
* @since 1.40
|
|
* @internal
|
|
*/
|
|
final class PageBundleParserOutputConverter {
|
|
/**
|
|
* @var string Key used to store parsoid page bundle data in ParserOutput
|
|
*/
|
|
public const PARSOID_PAGE_BUNDLE_KEY = 'parsoid-page-bundle';
|
|
|
|
/**
|
|
* We do not want instances of this class to be created
|
|
* @return void
|
|
*/
|
|
private function __construct() {
|
|
}
|
|
|
|
/**
|
|
* Creates a ParserOutput object containing the relevant data from
|
|
* the given PageBundle object.
|
|
*
|
|
* We need to inject data-parsoid and other properties into the
|
|
* parser output object for caching, so we can use it for VE edits
|
|
* and transformations.
|
|
*
|
|
* @param PageBundle $pageBundle
|
|
*
|
|
* @return ParserOutput
|
|
*/
|
|
public static function parserOutputFromPageBundle( PageBundle $pageBundle ): ParserOutput {
|
|
$parserOutput = new ParserOutput( $pageBundle->html );
|
|
$parserOutput->setExtensionData(
|
|
self::PARSOID_PAGE_BUNDLE_KEY,
|
|
[
|
|
'parsoid' => $pageBundle->parsoid,
|
|
'mw' => $pageBundle->mw,
|
|
'version' => $pageBundle->version,
|
|
'headers' => $pageBundle->headers
|
|
]
|
|
);
|
|
|
|
return $parserOutput;
|
|
}
|
|
|
|
/**
|
|
* Returns a Parsoid PageBundle equivalent to the given ParserOutput.
|
|
*
|
|
* @param ParserOutput $parserOutput
|
|
*
|
|
* @return PageBundle
|
|
*/
|
|
public static function pageBundleFromParserOutput( ParserOutput $parserOutput ): PageBundle {
|
|
$pageBundleData = $parserOutput->getExtensionData( self::PARSOID_PAGE_BUNDLE_KEY );
|
|
return new PageBundle(
|
|
$parserOutput->getRawText(),
|
|
$pageBundleData['parsoid'] ?? [],
|
|
$pageBundleData['mw'] ?? [],
|
|
$pageBundleData['version'] ?? null,
|
|
$pageBundleData['headers'] ?? []
|
|
);
|
|
}
|
|
|
|
}
|