| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Edit; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Parser\ParserOutput; |
| 7 |
use Stringable; |
| 8 |
use function count; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ParsoidRenderID implements Stringable { |
| 18 |
private int $revisionID; |
| 19 |
private string $uniqueID; |
| 20 |
private string $stashKey; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct( int $revisionID, string $uniqueID ) { |
| 27 |
$this->revisionID = $revisionID; |
| 28 |
$this->uniqueID = $uniqueID; |
| 29 |
$this->stashKey = $revisionID . '/' . $uniqueID; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static function newFromKey( string $key ): self { |
| 41 |
$parts = explode( '/', $key, 2 ); |
| 42 |
|
| 43 |
if ( count( $parts ) < 2 ) { |
| 44 |
throw new InvalidArgumentException( 'Bad key: ' . $key ); |
| 45 |
} |
| 46 |
|
| 47 |
[ $revisionID, $uniqueID ] = $parts; |
| 48 |
|
| 49 |
return new self( (int)$revisionID, $uniqueID ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static function newFromParserOutput( ParserOutput $parserOutput ): self { |
| 59 |
$revisionID = $parserOutput->getCacheRevisionId(); |
| 60 |
$uniqueID = $parserOutput->getRenderId(); |
| 61 |
|
| 62 |
if ( $revisionID === null || $uniqueID === null ) { |
| 63 |
throw new InvalidArgumentException( 'Missing render id' ); |
| 64 |
} |
| 65 |
|
| 66 |
return new self( $revisionID, $uniqueID ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static function newFromETag( string $eTag ): ?self { |
| 85 |
if ( !preg_match( '@^(?:W/)?"(\d+)/([^/]+)(:?/.*)?"$@', $eTag, $m ) ) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
[ , $revisionID, $uniqueID ] = $m; |
| 90 |
|
| 91 |
return new self( (int)$revisionID, $uniqueID ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function getKey(): string { |
| 101 |
return $this->stashKey; |
| 102 |
} |
| 103 |
|
| 104 |
public function __toString() { |
| 105 |
return $this->stashKey; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function getRevisionID(): int { |
| 114 |
return $this->revisionID; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function getUniqueID(): string { |
| 123 |
return $this->uniqueID; |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
class_alias( ParsoidRenderID::class, 'MediaWiki\\Parser\\Parsoid\\ParsoidRenderID' ); |