| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\OutputTransform; |
| 4 |
|
| 5 |
use MediaWiki\Parser\ParserOptions; |
| 6 |
use MediaWiki\Parser\ParserOutput; |
| 7 |
use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter; |
| 8 |
use stdClass; |
| 9 |
use Wikimedia\Parsoid\Core\PageBundle; |
| 10 |
use Wikimedia\Parsoid\DOM\Document; |
| 11 |
use Wikimedia\Parsoid\DOM\Element; |
| 12 |
use Wikimedia\Parsoid\Mocks\MockEnv; |
| 13 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 14 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 15 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 16 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 17 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 18 |
use Wikimedia\Parsoid\Utils\Utils; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
abstract class ContentDOMTransformStage extends OutputTransformStage { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function transform( |
| 34 |
ParserOutput $po, ?ParserOptions $popts, array &$options |
| 35 |
): ParserOutput { |
| 36 |
if ( $options['isParsoidContent'] ?? false ) { |
| 37 |
return $this->parsoidTransform( $po, $popts, $options ); |
| 38 |
} else { |
| 39 |
return $this->legacyTransform( $po, $popts, $options ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
private function legacyTransform( |
| 44 |
ParserOutput $po, ?ParserOptions $popts, array &$options |
| 45 |
): ParserOutput { |
| 46 |
$text = $po->getContentHolderText(); |
| 47 |
$doc = DOMUtils::parseHTML( $text ); |
| 48 |
|
| 49 |
$doc = $this->transformDOM( $doc, $po, $popts, $options ); |
| 50 |
|
| 51 |
$body = DOMCompat::getBody( $doc ); |
| 52 |
$text = ContentUtils::toXML( $body, [ |
| 53 |
'innerXML' => true, |
| 54 |
] ); |
| 55 |
$po->setContentHolderText( $text ); |
| 56 |
return $po; |
| 57 |
} |
| 58 |
|
| 59 |
private function parsoidTransform( |
| 60 |
ParserOutput $po, ?ParserOptions $popts, array &$options |
| 61 |
): ParserOutput { |
| 62 |
|
| 63 |
$doc = null; |
| 64 |
$hasPageBundle = PageBundleParserOutputConverter::hasPageBundle( $po ); |
| 65 |
$origPb = null; |
| 66 |
if ( $hasPageBundle ) { |
| 67 |
$origPb = PageBundleParserOutputConverter::pageBundleFromParserOutput( $po ); |
| 68 |
$doc = DOMUtils::parseHTML( $po->getContentHolderText() ); |
| 69 |
PageBundle::apply( $doc, $origPb ); |
| 70 |
DOMDataUtils::prepareDoc( $doc ); |
| 71 |
$body = DOMCompat::getBody( $doc ); |
| 72 |
'@phan-var Element $body'; |
| 73 |
DOMDataUtils::visitAndLoadDataAttribs( $body ); |
| 74 |
} else { |
| 75 |
$doc = ContentUtils::createAndLoadDocument( |
| 76 |
$po->getContentHolderText(), |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
$doc = $this->transformDOM( $doc, $po, $popts, $options ); |
| 81 |
|
| 82 |
|
| 83 |
$body = DOMCompat::getBody( $doc ); |
| 84 |
'@phan-var Element $body'; |
| 85 |
if ( $hasPageBundle ) { |
| 86 |
DOMDataUtils::visitAndStoreDataAttribs( |
| 87 |
$body, |
| 88 |
[ |
| 89 |
'storeInPageBundle' => true, |
| 90 |
'env' => new MockEnv( [] ), |
| 91 |
] |
| 92 |
); |
| 93 |
$pb = DOMDataUtils::getPageBundle( $doc ); |
| 94 |
$pb = self::workaroundT365036( $pb ); |
| 95 |
$pb = self::combinePageBundles( $pb, $origPb ); |
| 96 |
|
| 97 |
PageBundleParserOutputConverter::applyPageBundleDataToParserOutput( $pb, $po ); |
| 98 |
$text = ContentUtils::toXML( $body, [ |
| 99 |
'innerXML' => true, |
| 100 |
] ); |
| 101 |
} else { |
| 102 |
$text = ContentUtils::ppToXML( $body, [ |
| 103 |
'innerXML' => true, |
| 104 |
] ); |
| 105 |
} |
| 106 |
$po->setContentHolderText( $text ); |
| 107 |
return $po; |
| 108 |
} |
| 109 |
|
| 110 |
private function combinePageBundles( stdClass $pb, ?PageBundle $origPb ): stdClass { |
| 111 |
if ( $origPb === null ) { |
| 112 |
return $pb; |
| 113 |
} |
| 114 |
$headers = $origPb->headers; |
| 115 |
$pb->headers = $headers ? Utils::clone( $headers ) : null; |
| 116 |
$pb->version = $origPb->version; |
| 117 |
$pb->contentmodel = $origPb->contentmodel; |
| 118 |
return $pb; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
private function workaroundT365036( PageBundle $pb ): stdClass { |
| 128 |
$arrayPageBundle = PHPUtils::jsonDecode( PHPUtils::jsonEncode( $pb ) ); |
| 129 |
return (object)$arrayPageBundle; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
abstract public function transformDOM( |
| 134 |
Document $dom, ParserOutput $po, ?ParserOptions $popts, array &$options |
| 135 |
): Document; |
| 136 |
|
| 137 |
} |