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     */
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    public function getKey(): string {
99        return $this->stashKey;
100    }
101
102    public function __toString() {
103        return $this->stashKey;
104    }
105
106    /**
107     * Get the revision ID from the parsoid render ID object.
108     */
109    public function getRevisionID(): int {
110        return $this->revisionID;
111    }
112
113    /**
114     * Get the unique identifier from the parsoid render ID object.
115     */
116    public function getUniqueID(): string {
117        return $this->uniqueID;
118    }
119
120}
121
122/** @deprecated class alias since 1.42 */
123class_alias( ParsoidRenderID::class, 'MediaWiki\\Parser\\Parsoid\\ParsoidRenderID' );