| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 39 |
|
| 40 |
public function getPageBundle(): PageBundle { |
| 41 |
return $this->pageBundle; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function getRevisionID(): int { |
| 48 |
return $this->revId; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 63 |
|
| 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 |
|
| 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 |
|
| 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 |
} |