wiki.techinc.nl/includes/parser/Parsoid/ParsoidRenderID.php
Derick Alangi 13f6ec9e1b Rest: Migrate parsoid stashing logic from RESTbase
Add stash option to /page/html & /revision/html endpoints.
When this option is set, the PageBundle returned by Parsoid is
stashed and an etag is returned that can later be used to
make use of the stashed PageBundle.

The stash is for now backed by the BagOStuff returned by
ObjectCache::getLocalClusterInstance().

This patch adds additional data to the ParserOutput stored in ParserCache.
Old entries lacking that data will be ignored.

Bug: T267990
Co-Authored-by: Nikki <nnikkhoui@wikimedia.org>
Change-Id: Id35f1423a69e3ff63e4f9883b3f7e3f9521d81d5
2022-05-23 17:28:29 +01:00

98 lines
2.3 KiB
PHP

<?php
namespace MediaWiki\Parser\Parsoid;
/**
* Represents the identity of a specific rendering of a specific revision
* at some point in time.
*
* @since 1.39
* @unstable since 1.39, should be stable by 1.39 release.
*/
class ParsoidRenderID {
/** @var int */
private $revisionID;
/** @var string */
private $uniqueID;
/** @var string */
private $stashKey;
/**
* @param int $revisionID Revision that was rendered
* @param string $uniqueID An identifier for a point in time.
*/
public function __construct( int $revisionID, string $uniqueID ) {
$this->revisionID = $revisionID;
$this->uniqueID = $uniqueID;
$this->stashKey = $revisionID . '/' . $uniqueID;
}
/**
* @param string $key String representation of render ID
* (synonymous with an etag with double quotes) as returned by ::getKey().
*
* @return self
* @see newFromETag()
*
*/
public static function newFromKey( string $key ): self {
[ $revisionID, $uniqueID ] = explode( '/',
$key, 2 );
return new self( (int)$revisionID, $uniqueID );
}
/**
* This constructs a new render ID from the given ETag.
*
* Any suffix after a second forward slash will be ignored e.g.
* ->newFromEtag( '1/abc/stash' ) will return '1/abc' when ->getKey()
* is called on the ParsoidRenderID object instance.
*
* @param string $eTag ETag with double quotes,
* see https://www.rfc-editor.org/rfc/rfc7232#section-2.3
*
* @return self
* @see newFromKey() if ETag already has outside quotes trimmed
*
*/
public static function newFromETag( string $eTag ): self {
[ $revisionID, $uniqueID ] = explode( '/', trim( $eTag, '"' ) );
return new self( (int)$revisionID, $uniqueID );
}
/**
* This returns the canonical string representation from
* the parsoid render ID which can be used to in newFromString().
*
* @return string
*/
public function getKey(): string {
return $this->stashKey;
}
public function __toString() {
return $this->stashKey;
}
/**
* Get the revision ID from the parsoid render ID object.
*
* @return int
*/
public function getRevisionID(): int {
return $this->revisionID;
}
/**
* Get the unique identifier from the parsoid render ID object.
*
* @return string
*/
public function getUniqueID(): string {
return $this->uniqueID;
}
}