Debian Codegraph @

includes/edit/SelserContext.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Edit;
4
5 use MediaWiki\Content\Content;
6 use MediaWiki\MediaWikiServices;
7 use UnexpectedValueException;
8 use Wikimedia\JsonCodec\JsonCodecable;
9 use Wikimedia\JsonCodec\JsonCodecableTrait;
10 use Wikimedia\Parsoid\Core\PageBundle;
11 use Wikimedia\Parsoid\Core\SelserData;
12
13 /**
14 * Value object representing contextual information needed by Parsoid for selective serialization ("selser") of
15 * modified HTML.
16 *
17 * @see SelserData
18 *
19 * @since 1.40
20 */
21 class SelserContext implements JsonCodecable {
22 use JsonCodecableTrait;
23
24 public function __construct(
25 private PageBundle $pageBundle,
26 private int $revId,
27 private ?Content $content = null
28 ) {
29 if ( !$revId && !$content ) {
30 throw new UnexpectedValueException(
31 'If $revId is 0, $content must be given. ' .
32 'If we can\'t load the content from a revision, we have to stash it.'
33 );
34 }
35 }
36
37 /**
38 * @return PageBundle
39 */
40 public function getPageBundle(): PageBundle {
41 return $this->pageBundle;
42 }
43
44 /**
45 * @return int
46 */
47 public function getRevisionID(): int {
48 return $this->revId;
49 }
50
51 /**
52 * @return Content|null
53 */
54 public function getContent(): ?Content {
55 return $this->content;
56 }
57
58 public function toJsonArray(): array {
59 return [
60 'revId' => $this->revId,
61 'pb' => $this->pageBundle,
62 // After I544625136088164561b9169a63aed7450cce82f5 this can be:
63 // 'c' => $this->content,
64 'content' => $this->content ? [
65 'model' => $this->content->getModel(),
66 'data' => $this->content->serialize(),
67 ] : null,
68 ];
69 }
70
71 public static function jsonClassHintFor( string $keyName ): ?string {
72 if ( $keyName === 'pb' ) {
73 return PageBundle::class;
74 }
75 return null;
76 }
77
78 public static function newFromJsonArray( array $json ): self {
79 $revId = (int)$json['revId'];
80 $pb = $json['pb'];
81 if ( is_array( $pb ) ) {
82 // Backward compatibility with old serialization format
83 $pb = new PageBundle(
84 $pb['html'],
85 $pb['parsoid'] ?? null,
86 $pb['mw'] ?? null,
87 $pb['version'] ?? null,
88 $pb['headers'] ?? null,
89 $pb['contentmodel'] ?? null
90 );
91 }
92 $content = $json['c'] ?? $json['content'] ?? null;
93 if ( is_array( $content ) ) {
94 // Backward compatibility with old serialization format
95 $contentHandler = MediaWikiServices::getInstance()
96 ->getContentHandlerFactory()
97 ->getContentHandler( $content['model'] );
98 $content = $contentHandler->unserializeContent( $content['data'] );
99 }
100 return new self( $pb, $revId, $content );
101 }
102 }