Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.48% covered (warning)
81.48%
22 / 27
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SelserContext
81.48% covered (warning)
81.48%
22 / 27
71.43% covered (warning)
71.43%
5 / 7
12.91
0.00% covered (danger)
0.00%
0 / 1
 __construct
20.00% covered (danger)
20.00%
1 / 5
0.00% covered (danger)
0.00%
0 / 1
7.61
 getPageBundle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRevisionID
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toJsonArray
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 jsonClassHintFor
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 newFromJsonArray
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2
3namespace MediaWiki\Edit;
4
5use MediaWiki\Content\Content;
6use MediaWiki\MediaWikiServices;
7use UnexpectedValueException;
8use Wikimedia\JsonCodec\JsonCodecable;
9use Wikimedia\JsonCodec\JsonCodecableTrait;
10use Wikimedia\Parsoid\Core\HtmlPageBundle;
11use 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 */
21class SelserContext implements JsonCodecable {
22    use JsonCodecableTrait;
23
24    public function __construct(
25        private HtmlPageBundle $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    public function getPageBundle(): HtmlPageBundle {
38        return $this->pageBundle;
39    }
40
41    public function getRevisionID(): int {
42        return $this->revId;
43    }
44
45    public function getContent(): ?Content {
46        return $this->content;
47    }
48
49    public function toJsonArray(): array {
50        return [
51            'revId' => $this->revId,
52            'pb' => $this->pageBundle,
53            'c' => $this->content,
54        ];
55    }
56
57    public static function jsonClassHintFor( string $keyName ): ?string {
58        if ( $keyName === 'pb' ) {
59            return HtmlPageBundle::class;
60        }
61        return null;
62    }
63
64    public static function newFromJsonArray( array $json ): self {
65        $revId = (int)$json['revId'];
66        $pb = $json['pb'];
67        if ( is_array( $pb ) ) {
68            // Backward compatibility with old MW 1.44 serialization format
69            $pb = HtmlPageBundle::newFromJsonArray( $pb );
70        }
71        $content = $json['c'] ?? $json['content'] ?? null;
72        if ( is_array( $content ) ) {
73            // Backward compatibility with MW 1.43/1.44 serialization format.
74            // Can be removed when back-compatibility with MW 1.43 is no longer
75            // required.
76            $contentHandler = MediaWikiServices::getInstance()
77                ->getContentHandlerFactory()
78                ->getContentHandler( $content['model'] );
79            $content = $contentHandler->unserializeContent( $content['data'] );
80        }
81        return new self( $pb, $revId, $content );
82    }
83}