Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.18% covered (warning)
68.18%
15 / 22
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParsoidRenderID
71.43% covered (warning)
71.43%
15 / 21
75.00% covered (warning)
75.00%
6 / 8
15.36
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 newFromKey
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 newFromParserOutput
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 newFromETag
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRevisionID
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUniqueID
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Edit;
4
5use InvalidArgumentException;
6use MediaWiki\Parser\ParserOutput;
7use Stringable;
8use 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 */
17class 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    public static function newFromKey( string $key ): self {
40        $parts = explode( '/', $key, 2 );
41
42        if ( count( $parts ) < 2 ) {
43            throw new InvalidArgumentException( 'Bad key: ' . $key );
44        }
45
46        [ $revisionID, $uniqueID ] = $parts;
47
48        return new self( (int)$revisionID, $uniqueID );
49    }
50
51    /**
52     * Create a ParsoidRenderID from the revision and render id stored in
53     * a ParserOutput.
54     * @param ParserOutput $parserOutput
55     * @return self
56     */
57    public static function newFromParserOutput( ParserOutput $parserOutput ): self {
58        $revisionID = $parserOutput->getCacheRevisionId();
59        $uniqueID = $parserOutput->getRenderId();
60
61        if ( $revisionID === null || $uniqueID === null ) {
62            throw new InvalidArgumentException( 'Missing render id' );
63        }
64
65        return new self( $revisionID, $uniqueID );
66    }
67
68    /**
69     * This constructs a new render ID from the given ETag.
70     *
71     * Any suffix after a second forward slash will be ignored e.g.
72     * ->newFromEtag( '1/abc/stash' ) will return '1/abc' when ->getKey()
73     * is called on the ParsoidRenderID object instance.
74     *
75     * @param string $eTag ETag with double quotes,
76     *   see https://www.rfc-editor.org/rfc/rfc7232#section-2.3
77     *
78     * @return self|null The render ID embedded in the ETag,
79     *         or null if the ETag was malformed.
80     * @see newFromKey() if ETag already has outside quotes trimmed
81     */
82    public static function newFromETag( string $eTag ): ?self {
83        if ( !preg_match( '@^(?:W/)?"(\d+)/([^/]+)(:?/.*)?"$@', $eTag, $m ) ) {
84            return null;
85        }
86
87        [ , $revisionID, $uniqueID ] = $m;
88
89        return new self( (int)$revisionID, $uniqueID );
90    }
91
92    /**
93     * This returns the canonical string representation from
94     * the parsoid render ID which can be used to in newFromString().
95     */
96    public function getKey(): string {
97        return $this->stashKey;
98    }
99
100    public function __toString() {
101        return $this->stashKey;
102    }
103
104    /**
105     * Get the revision ID from the parsoid render ID object.
106     */
107    public function getRevisionID(): int {
108        return $this->revisionID;
109    }
110
111    /**
112     * Get the unique identifier from the parsoid render ID object.
113     */
114    public function getUniqueID(): string {
115        return $this->uniqueID;
116    }
117
118}
119
120/** @deprecated class alias since 1.42 */
121class_alias( ParsoidRenderID::class, 'MediaWiki\\Parser\\Parsoid\\ParsoidRenderID' );