Debian Codegraph @

vendor/wikimedia/parsoid/src/Core/PageBundle.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\Core;
5
6 use Composer\Semver\Semver;
7 use Wikimedia\Parsoid\DOM\Document;
8 use Wikimedia\Parsoid\DOM\Element;
9 use Wikimedia\Parsoid\DOM\Node;
10 use Wikimedia\Parsoid\Utils\ContentUtils;
11 use Wikimedia\Parsoid\Utils\DOMCompat;
12 use Wikimedia\Parsoid\Utils\DOMDataUtils;
13 use Wikimedia\Parsoid\Utils\DOMUtils;
14 use Wikimedia\Parsoid\Utils\PHPUtils;
15
16 /**
17 * PORT-FIXME: This is just a placeholder for data that was previously passed
18 * to entrypoint in JavaScript. Who will construct these objects and whether
19 * this is the correct interface is yet to be determined.
20 *
21 * Note that the parsoid/mw properties of the page bundle are in "serialized
22 * array" form; that is, they are flat arrays appropriate for json-encoding
23 * and do not contain DataParsoid or DataMw objects.
24 */
25 class PageBundle {
26 /** @var string */
27 public $html;
28
29 /**
30 * A map from ID to the array serialization of DataParsoid for the Node
31 * with that ID.
32 *
33 * @var null|array{counter?:int,offsetType?:string,ids:array<string,array>}
34 */
35 public $parsoid;
36
37 /**
38 * A map from ID to the array serialization of DataMw for the Node
39 * with that ID.
40 *
41 * @var null|array{ids:array<string,array>}
42 */
43 public $mw;
44
45 /** @var ?string */
46 public $version;
47
48 /**
49 * A map of HTTP headers: both name and value should be strings.
50 * @var array<string,string>|null
51 */
52 public $headers;
53
54 /** @var string|null */
55 public $contentmodel;
56
57 public function __construct(
58 string $html, ?array $parsoid = null, ?array $mw = null,
59 ?string $version = null, ?array $headers = null,
60 ?string $contentmodel = null
61 ) {
62 $this->html = $html;
63 $this->parsoid = $parsoid;
64 $this->mw = $mw;
65 $this->version = $version;
66 $this->headers = $headers;
67 $this->contentmodel = $contentmodel;
68 }
69
70 public function toDom(): Document {
71 $doc = DOMUtils::parseHTML( $this->html );
72 self::apply( $doc, $this );
73 return $doc;
74 }
75
76 public function toHtml(): string {
77 return ContentUtils::toXML( $this->toDom() );
78 }
79
80 /**
81 * Check if this pagebundle is valid.
82 * @param string $contentVersion Document content version to validate against.
83 * @param ?string &$errorMessage Error message will be returned here.
84 * @return bool
85 */
86 public function validate(
87 string $contentVersion, ?string &$errorMessage = null
88 ) {
89 if ( !$this->parsoid || !isset( $this->parsoid['ids'] ) ) {
90 $errorMessage = 'Invalid data-parsoid was provided.';
91 return false;
92 } elseif ( Semver::satisfies( $contentVersion, '^999.0.0' )
93 && ( !$this->mw || !isset( $this->mw['ids'] ) )
94 ) {
95 $errorMessage = 'Invalid data-mw was provided.';
96 return false;
97 }
98 return true;
99 }
100
101 /**
102 * @return array
103 */
104 public function responseData() {
105 $responseData = [
106 'contentmodel' => $this->contentmodel ?? '',
107 'html' => [
108 'headers' => array_merge( [
109 'content-type' => 'text/html; charset=utf-8; '
110 . 'profile="https://www.mediawiki.org/wiki/Specs/HTML/'
111 . $this->version . '"',
112 ], $this->headers ?? [] ),
113 'body' => $this->html,
114 ],
115 'data-parsoid' => [
116 'headers' => [
117 'content-type' => 'application/json; charset=utf-8; '
118 . 'profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/'
119 . $this->version . '"',
120 ],
121 'body' => $this->parsoid,
122 ],
123 ];
124 if ( Semver::satisfies( $this->version, '^999.0.0' ) ) {
125 $responseData['data-mw'] = [
126 'headers' => [
127 'content-type' => 'application/json; charset=utf-8; ' .
128 'profile="https://www.mediawiki.org/wiki/Specs/data-mw/' .
129 $this->version . '"',
130 ],
131 'body' => $this->mw,
132 ];
133 }
134 return $responseData;
135 }
136
137 /**
138 * Applies the `data-*` attributes JSON structure to the document.
139 * Leaves `id` attributes behind -- they are used by citation code to
140 * extract `<ref>` body from the DOM.
141 *
142 * @param Document $doc doc
143 * @param PageBundle $pb page bundle
144 */
145 public static function apply( Document $doc, PageBundle $pb ): void {
146 DOMUtils::visitDOM(
147 DOMCompat::getBody( $doc ),
148 static function ( Node $node ) use ( $pb ): void {
149 if ( $node instanceof Element ) {
150 $id = DOMCompat::getAttribute( $node, 'id' );
151 if ( $id === null ) {
152 return;
153 }
154 if ( isset( $pb->parsoid['ids'][$id] ) ) {
155 DOMDataUtils::setJSONAttribute(
156 $node, 'data-parsoid', $pb->parsoid['ids'][$id]
157 );
158 }
159 if ( isset( $pb->mw['ids'][$id] ) ) {
160 // Only apply if it isn't already set. This means
161 // earlier applications of the pagebundle have higher
162 // precedence, inline data being the highest.
163 if ( !$node->hasAttribute( 'data-mw' ) ) {
164 DOMDataUtils::setJSONAttribute(
165 $node, 'data-mw', $pb->mw['ids'][$id]
166 );
167 }
168 }
169 }
170 }
171 );
172 }
173
174 /**
175 * Encode some of these properties for emitting in the <head> element of a doc
176 * @return string
177 */
178 public function encodeForHeadElement(): string {
179 // Note that $this->parsoid and $this->mw are already serialized arrays
180 // so a naive jsonEncode is sufficient. We don't need a codec.
181 return PHPUtils::jsonEncode( [ 'parsoid' => $this->parsoid ?? [], 'mw' => $this->mw ?? [] ] );
182 }
183 }