MediaWiki master
ContentDOMTransformStage.php
Go to the documentation of this file.
1<?php
2
4
8use Wikimedia\Parsoid\Core\DomPageBundle;
9use Wikimedia\Parsoid\Core\PageBundle;
10use Wikimedia\Parsoid\DOM\Document;
11use Wikimedia\Parsoid\DOM\Element;
12use Wikimedia\Parsoid\Utils\ContentUtils;
13use Wikimedia\Parsoid\Utils\DOMCompat;
14use Wikimedia\Parsoid\Utils\DOMUtils;
15
25
29 public function transform(
30 ParserOutput $po, ?ParserOptions $popts, array &$options
31 ): ParserOutput {
32 if ( $options['isParsoidContent'] ?? false ) {
33 return $this->parsoidTransform( $po, $popts, $options );
34 } else {
35 return $this->legacyTransform( $po, $popts, $options );
36 }
37 }
38
39 private function legacyTransform(
40 ParserOutput $po, ?ParserOptions $popts, array &$options
41 ): ParserOutput {
42 $text = $po->getContentHolderText();
43 $doc = DOMUtils::parseHTML( $text );
44
45 $doc = $this->transformDOM( $doc, $po, $popts, $options );
46
47 $body = DOMCompat::getBody( $doc );
48 $text = ContentUtils::toXML( $body, [
49 'innerXML' => true,
50 ] );
51 $po->setContentHolderText( $text );
52 return $po;
53 }
54
55 private function parsoidTransform(
56 ParserOutput $po, ?ParserOptions $popts, array &$options
57 ): ParserOutput {
58 // TODO will use HTMLHolder in the future
59 $doc = null;
60 $hasPageBundle = PageBundleParserOutputConverter::hasPageBundle( $po );
61 $origPb = null;
62 if ( $hasPageBundle ) {
63 $origPb = PageBundleParserOutputConverter::pageBundleFromParserOutput( $po );
64 // TODO: pageBundleFromParserOutput should be able to create a
65 // DomPageBundle when the HTMLHolder has a DOM already.
66 $doc = DomPageBundle::fromPageBundle( $origPb )->toDom( true );
67 } else {
68 $doc = ContentUtils::createAndLoadDocument(
69 $po->getContentHolderText(), [ 'markNew' => true, 'validateXMLNames' => true, ]
70 );
71 }
72
73 $doc = $this->transformDOM( $doc, $po, $popts, $options );
74
75 // TODO will use HTMLHolder/DomPageBundle in the future
76 if ( $hasPageBundle ) {
77 $dpb = DomPageBundle::fromLoadedDocument( $doc, [
78 'pageBundle' => $origPb,
79 ] );
80 $pb = PageBundle::fromDomPageBundle( $dpb, [ 'body_only' => true ] );
81 PageBundleParserOutputConverter::applyPageBundleDataToParserOutput( $pb, $po );
82 $text = $pb->html;
83 } else {
84 $body = DOMCompat::getBody( $doc );
85 '@phan-var Element $body'; // assert non-null
86 $text = ContentUtils::ppToXML( $body, [
87 'innerXML' => true,
88 ] );
89 }
90 $po->setContentHolderText( $text );
91 return $po;
92 }
93
95 abstract public function transformDOM(
96 Document $dom, ParserOutput $po, ?ParserOptions $popts, array &$options
97 ): Document;
98
99}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
OutputTransformStages that modify the content as a HTML DOM tree.
transformDOM(Document $dom, ParserOutput $po, ?ParserOptions $popts, array &$options)
Applies the transformation to a DOM document.
transform(ParserOutput $po, ?ParserOptions $popts, array &$options)
Transforms the input ParserOutput into the returned ParserOutput.The returned ParserOutput can explic...
Classes implementing the OutputTransformStage aim at being added to a pipeline of transformations tha...
Set options of the Parser.
ParserOutput is a rendering of a Content object or a message.
Provides methods for conversion between PageBundle and ParserOutput TODO: Convert to a trait once we ...