Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
14 / 16 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SimpleParsoidOutputStash | |
87.50% |
14 / 16 |
|
75.00% |
3 / 4 |
6.07 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| makeCacheKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Edit; |
| 4 | |
| 5 | use MediaWiki\Json\JsonCodec; |
| 6 | use Wikimedia\ObjectCache\BagOStuff; |
| 7 | |
| 8 | /** |
| 9 | * @internal |
| 10 | * @since 1.39 |
| 11 | */ |
| 12 | class SimpleParsoidOutputStash implements ParsoidOutputStash { |
| 13 | |
| 14 | public function __construct( |
| 15 | private JsonCodec $jsonCodec, |
| 16 | /** Storage backend */ |
| 17 | private BagOStuff $bagOfStuff, |
| 18 | /** Cache duration in seconds */ |
| 19 | private int $duration, |
| 20 | ) { |
| 21 | } |
| 22 | |
| 23 | private function makeCacheKey( ParsoidRenderID $renderId ): string { |
| 24 | return $this->bagOfStuff->makeKey( 'ParsoidOutputStash', $renderId->getKey() ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Before we stash, we serialize & encode into JSON the relevant |
| 29 | * parts of the data we need to construct a page bundle in the future. |
| 30 | * |
| 31 | * @param ParsoidRenderID $renderId Combination of revision ID and revision's time ID |
| 32 | * @param SelserContext $selserContext |
| 33 | * |
| 34 | * @return bool |
| 35 | */ |
| 36 | public function set( ParsoidRenderID $renderId, SelserContext $selserContext ): bool { |
| 37 | $jsonic = $this->jsonCodec->toJsonArray( |
| 38 | $selserContext, SelserContext::class |
| 39 | ); |
| 40 | $key = $this->makeCacheKey( $renderId ); |
| 41 | return $this->bagOfStuff->set( $key, $jsonic, $this->duration ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * This will decode the JSON data and create a page bundle from it |
| 46 | * if we have something in the stash that matches a given rendering or |
| 47 | * will just return an empty array if no entry in the stash. |
| 48 | * |
| 49 | * @param ParsoidRenderID $renderId |
| 50 | * |
| 51 | * @return SelserContext|null |
| 52 | */ |
| 53 | public function get( ParsoidRenderID $renderId ): ?SelserContext { |
| 54 | $key = $this->makeCacheKey( $renderId ); |
| 55 | $jsonic = $this->bagOfStuff->get( $key ) ?? []; |
| 56 | |
| 57 | if ( !is_array( $jsonic ) ) { |
| 58 | // Defend against old stashed data: MW 1.39 stored a string. |
| 59 | // We're supposed to maintain upgrade compatibility with the |
| 60 | // last two LTS releases, so we can remove this in MW 1.48. |
| 61 | return null; |
| 62 | } |
| 63 | if ( !isset( $jsonic['pb'] ) ) { |
| 64 | return null; |
| 65 | } |
| 66 | return $this->jsonCodec->newFromJsonArray( |
| 67 | $jsonic, SelserContext::class |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | } |