Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
PageReferenceValue
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
7 / 7
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 localReference
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWikiId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNamespace
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDBkey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSamePageAs
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 __toString
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Page;
8
9use MediaWiki\DAO\WikiAwareEntityTrait;
10use Stringable;
11use Wikimedia\Assert\Assert;
12use Wikimedia\NonSerializable\NonSerializableTrait;
13
14/**
15 * Immutable value object representing a page reference.
16 *
17 * Instances of this class are expected to always represent a viewable pages, that is,
18 * pages that can at least potentially be visited on the wiki.
19 * This class may represent Special pages, but not interwiki links, section links, etc.
20 *
21 * Code that deserializes instances of PageReferenceValue must ensure that the original
22 * meaning of the "local" Wiki ID is preserved: When an instance of PageReferenceValue
23 * is created with self::LOCAL as the Wiki ID on one wiki, gets serialized,
24 * stored, and later read and unserialized on another wiki, the value of the Wiki ID
25 * must be adjusted to refer to the original wiki.
26 *
27 * @since 1.37
28 */
29class PageReferenceValue implements Stringable, PageReference {
30
31    /* Use JSON, but beware the note on serialization above. */
32    use NonSerializableTrait;
33    use WikiAwareEntityTrait;
34
35    /** @var int */
36    private $namespace;
37
38    /** @var string */
39    private $dbKey;
40
41    /** @var string|false */
42    private $wikiId;
43
44    /**
45     * @param int $namespace A valid namespace ID. Validation is the caller's responsibility!
46     * @param string $dbKey A valid DB key. Validation is the caller's responsibility!
47     * @param string|false $wikiId The Id of the wiki this page belongs to,
48     *        or self::LOCAL for the local wiki. The method {@link PageReferenceValue::localReference}
49     *        is available as a shorthand for local wikis (only requires 2 parameters).
50     */
51    public function __construct( int $namespace, string $dbKey, $wikiId ) {
52        $this->assertWikiIdParam( $wikiId );
53
54        Assert::parameter( $dbKey !== '', '$dbKey', 'must not be empty' );
55
56        // Replace spaces with underscores
57        $dbKey = str_replace( ' ', '_', $dbKey );
58
59        $this->wikiId = $wikiId;
60        $this->namespace = $namespace;
61        $this->dbKey = $dbKey;
62    }
63
64    /**
65     * Create PageReference for a local page.
66     *
67     * @param int $namespace
68     * @param string $dbKey
69     * @return PageReferenceValue
70     */
71    public static function localReference( int $namespace, string $dbKey ): self {
72        return new self( $namespace, $dbKey, self::LOCAL );
73    }
74
75    /**
76     * Get the ID of the wiki provided to the constructor.
77     *
78     * @return string|false
79     */
80    public function getWikiId() {
81        return $this->wikiId;
82    }
83
84    /**
85     * @inheritDoc
86     *
87     * @return int
88     */
89    public function getNamespace(): int {
90        return $this->namespace;
91    }
92
93    /**
94     * @inheritDoc
95     *
96     * @return string
97     */
98    public function getDBkey(): string {
99        return $this->dbKey;
100    }
101
102    /**
103     * @inheritDoc
104     */
105    public function isSamePageAs( PageReference $other ): bool {
106        // NOTE: keep in sync with Title::isSamePageAs()!
107        // NOTE: keep in sync with WikiPage::isSamePageAs()!
108        return $this->getWikiId() === $other->getWikiId()
109            && $this->getNamespace() === $other->getNamespace()
110            && $this->getDBkey() === $other->getDBkey();
111    }
112
113    /**
114     * Returns a string representation of the title, for logging. This is purely informative
115     * and must not be used programmatically.
116     */
117    public function __toString(): string {
118        $s = '[' . $this->namespace . ':' . $this->dbKey . ']';
119
120        if ( $this->wikiId ) {
121            $s .= '@' . $this->wikiId;
122        }
123
124        return $s;
125    }
126
127}