Debian Codegraph @

includes/edit/SimpleParsoidOutputStash.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Edit;
4
5 use MediaWiki\Json\JsonCodec;
6 use MediaWiki\Parser\Parsoid\PageBundleJsonTrait;
7 use Wikimedia\ObjectCache\BagOStuff;
8
9 /**
10 * @internal
11 * @since 1.39
12 */
13 class SimpleParsoidOutputStash implements ParsoidOutputStash {
14 use PageBundleJsonTrait;
15
16 public function __construct(
17 private JsonCodec $jsonCodec,
18 /** Storage backend */
19 private BagOStuff $bagOfStuff,
20 /** Cache duration in seconds */
21 private int $duration,
22 ) {
23 }
24
25 private function makeCacheKey( ParsoidRenderID $renderId ): string {
26 return $this->bagOfStuff->makeKey( 'ParsoidOutputStash', $renderId->getKey() );
27 }
28
29 /**
30 * Before we stash, we serialize & encode into JSON the relevant
31 * parts of the data we need to construct a page bundle in the future.
32 *
33 * @param ParsoidRenderID $renderId Combination of revision ID and revision's time ID
34 * @param SelserContext $selserContext
35 *
36 * @return bool
37 */
38 public function set( ParsoidRenderID $renderId, SelserContext $selserContext ): bool {
39 $jsonic = $this->jsonCodec->toJsonArray(
40 $selserContext, SelserContext::class
41 );
42 $key = $this->makeCacheKey( $renderId );
43 return $this->bagOfStuff->set( $key, $jsonic, $this->duration );
44 }
45
46 /**
47 * This will decode the JSON data and create a page bundle from it
48 * if we have something in the stash that matches a given rendering or
49 * will just return an empty array if no entry in the stash.
50 *
51 * @param ParsoidRenderID $renderId
52 *
53 * @return SelserContext|null
54 */
55 public function get( ParsoidRenderID $renderId ): ?SelserContext {
56 $key = $this->makeCacheKey( $renderId );
57 $jsonic = $this->bagOfStuff->get( $key ) ?? [];
58
59 if ( !is_array( $jsonic ) ) {
60 // Defend against old stashed data.
61 // Only needed for a couple of days after this code has been deployed.
62 return null;
63 }
64 if ( !isset( $jsonic['pb'] ) ) {
65 return null;
66 }
67 return $this->jsonCodec->newFromJsonArray(
68 $jsonic, SelserContext::class
69 );
70 }
71
72 }