Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.67% |
33 / 36 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
PageBundleParserOutputConverter | |
91.67% |
33 / 36 |
|
25.00% |
1 / 4 |
7.03 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
parserOutputFromPageBundle | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
3 | |||
pageBundleFromParserOutput | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
2.00 | |||
hasPageBundle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Parser\Parsoid; |
4 | |
5 | use LanguageCode; |
6 | use MediaWiki\Parser\ParserOutput; |
7 | use Wikimedia\Parsoid\Core\PageBundle; |
8 | |
9 | /** |
10 | * Provides methods for conversion between PageBundle and ParserOutput |
11 | * TODO: Convert to a trait once we drop support for PHP < 8.2 since |
12 | * support for constants in traits was added in PHP 8.2 |
13 | * @since 1.40 |
14 | * @internal |
15 | */ |
16 | final class PageBundleParserOutputConverter { |
17 | /** |
18 | * @var string Key used to store parsoid page bundle data in ParserOutput |
19 | */ |
20 | public const PARSOID_PAGE_BUNDLE_KEY = 'parsoid-page-bundle'; |
21 | |
22 | /** |
23 | * We do not want instances of this class to be created |
24 | * @return void |
25 | */ |
26 | private function __construct() { |
27 | } |
28 | |
29 | /** |
30 | * Creates a ParserOutput object containing the relevant data from |
31 | * the given PageBundle object. |
32 | * |
33 | * We need to inject data-parsoid and other properties into the |
34 | * parser output object for caching, so we can use it for VE edits |
35 | * and transformations. |
36 | * |
37 | * @param PageBundle $pageBundle |
38 | * @param ?ParserOutput $originalParserOutput Any non-parsoid metadata |
39 | * from $originalParserOutput will be copied into the new ParserOutput object. |
40 | * |
41 | * @return ParserOutput |
42 | */ |
43 | public static function parserOutputFromPageBundle( |
44 | PageBundle $pageBundle, ?ParserOutput $originalParserOutput = null |
45 | ): ParserOutput { |
46 | $parserOutput = new ParserOutput( $pageBundle->html ); |
47 | |
48 | if ( $originalParserOutput ) { |
49 | $parserOutput->mergeHtmlMetaDataFrom( $originalParserOutput ); |
50 | $parserOutput->mergeTrackingMetaDataFrom( $originalParserOutput ); |
51 | $parserOutput->mergeInternalMetaDataFrom( $originalParserOutput ); |
52 | } |
53 | |
54 | $parserOutput->setExtensionData( |
55 | self::PARSOID_PAGE_BUNDLE_KEY, |
56 | [ |
57 | 'parsoid' => $pageBundle->parsoid, |
58 | 'mw' => $pageBundle->mw, |
59 | 'version' => $pageBundle->version, |
60 | 'headers' => $pageBundle->headers, |
61 | 'contentmodel' => $pageBundle->contentmodel, |
62 | ] |
63 | ); |
64 | |
65 | if ( isset( $pageBundle->headers['content-language'] ) ) { |
66 | $lang = LanguageCode::normalizeNonstandardCodeAndWarn( |
67 | // @phan-suppress-next-line PhanTypeArraySuspiciousNullable |
68 | $pageBundle->headers['content-language'] |
69 | ); |
70 | $parserOutput->setLanguage( $lang ); |
71 | } |
72 | |
73 | return $parserOutput; |
74 | } |
75 | |
76 | /** |
77 | * Returns a Parsoid PageBundle equivalent to the given ParserOutput. |
78 | * |
79 | * @param ParserOutput $parserOutput |
80 | * |
81 | * @return PageBundle |
82 | */ |
83 | public static function pageBundleFromParserOutput( ParserOutput $parserOutput ): PageBundle { |
84 | $pageBundleData = $parserOutput->getExtensionData( self::PARSOID_PAGE_BUNDLE_KEY ); |
85 | $lang = $parserOutput->getLanguage(); |
86 | |
87 | $headers = $pageBundleData['headers'] ?? []; |
88 | |
89 | if ( $lang ) { |
90 | $headers['content-language'] = $lang; |
91 | } |
92 | |
93 | return new PageBundle( |
94 | $parserOutput->getRawText(), |
95 | $pageBundleData['parsoid'] ?? [], |
96 | $pageBundleData['mw'] ?? [], |
97 | // It would be nice to have this be "null", but PageBundle::responseData() |
98 | // chocks on that: T325137. |
99 | $pageBundleData['version'] ?? '0.0.0', |
100 | $pageBundleData['headers'] ?? $headers, |
101 | $pageBundleData['contentmodel'] ?? null |
102 | ); |
103 | } |
104 | |
105 | public static function hasPageBundle( ParserOutput $parserOutput ) { |
106 | return $parserOutput->getExtensionData( self::PARSOID_PAGE_BUNDLE_KEY ) !== null; |
107 | } |
108 | } |