wiki.techinc.nl/includes/edit/SimpleParsoidOutputStash.php
C. Scott Ananian e9539efe2c Use JsonCodec to serialize SelserContext
This cleans up a FIXME left over from
I9e6b924d62ccc3312f5c70989477da1e2f21c86b.

SimpleParsoidOutputStashTest was temporary changed from a unit test to
an integration test, since the serialization/deserialization mechanism
for Content relies on ContentHandlerFactory in a way which is
difficult to unit test.  This will be restored in
I0cc1fc1b7403674467d85618b38a3b5a4718b66e once native JSON
serialization for Content is landed.

Follows-Up: I9e6b924d62ccc3312f5c70989477da1e2f21c86b
Change-Id: If985e99f9ca9596d0fe40f0a5ef2cdb72286627d
(cherry picked from commit 2ebf7e12df28f9861bb204ff4134871089a1c771)
2025-09-10 14:08:31 -04:00

72 lines
1.9 KiB
PHP

<?php
namespace MediaWiki\Edit;
use MediaWiki\Json\JsonCodec;
use MediaWiki\Parser\Parsoid\PageBundleJsonTrait;
use Wikimedia\ObjectCache\BagOStuff;
/**
* @internal
* @since 1.39
*/
class SimpleParsoidOutputStash implements ParsoidOutputStash {
use PageBundleJsonTrait;
public function __construct(
private JsonCodec $jsonCodec,
/** Storage backend */
private BagOStuff $bagOfStuff,
/** Cache duration in seconds */
private int $duration,
) {
}
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->jsonCodec->toJsonArray(
$selserContext, SelserContext::class
);
$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;
}
if ( !isset( $jsonic['pb'] ) ) {
return null;
}
return $this->jsonCodec->newFromJsonArray(
$jsonic, SelserContext::class
);
}
}