blob: a19db49e7b4c9d16d48b96a218ea34667a47124c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
<?php
namespace MediaWiki\Edit;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Parser\Parsoid\PageBundleJsonTrait;
use Wikimedia\ObjectCache\BagOStuff;
/**
* @internal
* @since 1.39
*/
class SimpleParsoidOutputStash implements ParsoidOutputStash {
use PageBundleJsonTrait;
/** @var BagOStuff */
private $bagOfStuff;
/** @var int */
private $duration;
/** @var IContentHandlerFactory */
private $contentHandlerFactory;
/**
* @param IContentHandlerFactory $contentHandlerFactory
* @param BagOStuff $bagOfStuff storage backend
* @param int $duration cache duration in seconds
*/
public function __construct( IContentHandlerFactory $contentHandlerFactory, BagOStuff $bagOfStuff, int $duration ) {
$this->bagOfStuff = $bagOfStuff;
$this->duration = $duration;
$this->contentHandlerFactory = $contentHandlerFactory;
}
private function makeCacheKey( ParsoidRenderID $renderId ): string {
return $this->bagOfStuff->makeKey( 'ParsoidOutputStash', $renderId->getKey() );
}
/**
* Before we stash, we serialize & encode into JSON the relevant
* parts of the data we need to construct a page bundle in the future.
*
* @param ParsoidRenderID $renderId Combination of revision ID and revision's time ID
* @param SelserContext $selserContext
*
* @return bool
*/
public function set( ParsoidRenderID $renderId, SelserContext $selserContext ): bool {
$jsonic = $this->selserContextToJsonArray( $selserContext );
$key = $this->makeCacheKey( $renderId );
return $this->bagOfStuff->set( $key, $jsonic, $this->duration );
}
/**
* This will decode the JSON data and create a page bundle from it
* if we have something in the stash that matches a given rendering or
* will just return an empty array if no entry in the stash.
*
* @param ParsoidRenderID $renderId
*
* @return SelserContext|null
*/
public function get( ParsoidRenderID $renderId ): ?SelserContext {
$key = $this->makeCacheKey( $renderId );
$jsonic = $this->bagOfStuff->get( $key ) ?? [];
if ( !is_array( $jsonic ) ) {
// Defend against old stashed data.
// Only needed for a couple of days after this code has been deployed.
return null;
}
$selserContext = $this->newSelserContextFromJson( $jsonic );
return $selserContext ?: null;
}
private function newSelserContextFromJson( array $json ): ?SelserContext {
if ( !isset( $json['pb'] ) ) {
return null;
}
$pb = $this->newPageBundleFromJson( $json['pb'] );
if ( !$pb ) {
return null;
}
$revId = (int)$json['revId'];
if ( isset( $json['content'] ) ) {
$contentHandler = $this->contentHandlerFactory->getContentHandler( $json['content']['model'] );
$content = $contentHandler->unserializeContent( $json['content']['data'] );
} else {
$content = null;
}
return new SelserContext( $pb, $revId, $content );
}
private function selserContextToJsonArray( SelserContext $selserContext ): array {
$json = [
'revId' => $selserContext->getRevisionID(),
];
$json['pb'] = $this->jsonSerializePageBundle( $selserContext->getPageBundle() );
$content = $selserContext->getContent();
if ( $content ) {
$json['content'] = [
'model' => $content->getModel(),
'data' => $content->serialize()
];
}
return $json;
}
}
|