Debian Codegraph @

includes/edit/ParsoidRenderID.php

raw symbols SCIP
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 * Represents the identity of a specific rendering of a specific revision
12 * at some point in time.
13 *
14 * @since 1.39
15 * @unstable since 1.39, should be stable by 1.39 release.
16 */
17 class ParsoidRenderID implements Stringable {
18 private int $revisionID;
19 private string $uniqueID;
20 private string $stashKey;
21
22 /**
23 * @param int $revisionID Revision that was rendered
24 * @param string $uniqueID An identifier for a point in time.
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 * @param string $key String representation of render ID
34 * (synonymous with an etag with double quotes) as returned by ::getKey().
35 *
36 * @return self
37 * @see newFromETag()
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 * Create a ParsoidRenderID from the revision and render id stored in
54 * a ParserOutput.
55 * @param ParserOutput $parserOutput
56 * @return self
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 * This constructs a new render ID from the given ETag.
71 *
72 * Any suffix after a second forward slash will be ignored e.g.
73 * ->newFromEtag( '1/abc/stash' ) will return '1/abc' when ->getKey()
74 * is called on the ParsoidRenderID object instance.
75 *
76 * @param string $eTag ETag with double quotes,
77 * see https://www.rfc-editor.org/rfc/rfc7232#section-2.3
78 *
79 * @return ParsoidRenderID|null The render ID embedded in the ETag,
80 * or null if the ETag was malformed.
81 * @see newFromKey() if ETag already has outside quotes trimmed
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 * This returns the canonical string representation from
96 * the parsoid render ID which can be used to in newFromString().
97 *
98 * @return string
99 */
100 public function getKey(): string {
101 return $this->stashKey;
102 }
103
104 public function __toString() {
105 return $this->stashKey;
106 }
107
108 /**
109 * Get the revision ID from the parsoid render ID object.
110 *
111 * @return int
112 */
113 public function getRevisionID(): int {
114 return $this->revisionID;
115 }
116
117 /**
118 * Get the unique identifier from the parsoid render ID object.
119 *
120 * @return string
121 */
122 public function getUniqueID(): string {
123 return $this->uniqueID;
124 }
125
126 }
127
128 /** @deprecated class alias since 1.42 */
129 class_alias( ParsoidRenderID::class, 'MediaWiki\\Parser\\Parsoid\\ParsoidRenderID' );