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 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Page;
22
23use MediaWiki\DAO\WikiAwareEntityTrait;
24use Wikimedia\Assert\Assert;
25use Wikimedia\NonSerializable\NonSerializableTrait;
26
27/**
28 * Immutable value object representing a page reference.
29 *
30 * Instances of this class are expected to always represent a viewable pages, that is,
31 * pages that can at least potentially be visited on the wiki.
32 * This class may represent Special pages, but not interwiki links, section links, etc.
33 *
34 * Code that deserializes instances of PageReferenceValue must ensure that the original
35 * meaning of the "local" Wiki ID is preserved: When an instance of PageReferenceValue
36 * is created with self::LOCAL as the Wiki ID on one wiki, gets serialized,
37 * stored, and later read and unserialized on another wiki, the value of the Wiki ID
38 * must be adjusted to refer to the original wiki.
39 *
40 * @since 1.37
41 */
42class PageReferenceValue implements PageReference {
43
44    /* Use JSON, but beware the note on serialization above. */
45    use NonSerializableTrait;
46    use WikiAwareEntityTrait;
47
48    /** @var int */
49    private $namespace;
50
51    /** @var string */
52    private $dbKey;
53
54    /** @var string|false */
55    private $wikiId;
56
57    /**
58     * @param int $namespace A valid namespace ID. Validation is the caller's responsibility!
59     * @param string $dbKey A valid DB key. Validation is the caller's responsibility!
60     * @param string|false $wikiId The Id of the wiki this page belongs to,
61     *        or self::LOCAL for the local wiki.
62     */
63    public function __construct( int $namespace, string $dbKey, $wikiId ) {
64        $this->assertWikiIdParam( $wikiId );
65
66        Assert::parameter( $dbKey !== '', '$dbKey', 'must not be empty' );
67
68        // Replace spaces with underscores
69        $dbKey = str_replace( ' ', '_', $dbKey );
70
71        $this->wikiId = $wikiId;
72        $this->namespace = $namespace;
73        $this->dbKey = $dbKey;
74    }
75
76    /**
77     * Create PageReference for a local page.
78     *
79     * @param int $namespace
80     * @param string $dbKey
81     * @return PageReferenceValue
82     */
83    public static function localReference( int $namespace, string $dbKey ): self {
84        return new self( $namespace, $dbKey, self::LOCAL );
85    }
86
87    /**
88     * Get the ID of the wiki provided to the constructor.
89     *
90     * @return string|false
91     */
92    public function getWikiId() {
93        return $this->wikiId;
94    }
95
96    /**
97     * @inheritDoc
98     *
99     * @return int
100     */
101    public function getNamespace(): int {
102        return $this->namespace;
103    }
104
105    /**
106     * @inheritDoc
107     *
108     * @return string
109     */
110    public function getDBkey(): string {
111        return $this->dbKey;
112    }
113
114    /**
115     * @inheritDoc
116     */
117    public function isSamePageAs( PageReference $other ): bool {
118        // NOTE: keep in sync with Title::isSamePageAs()!
119        // NOTE: keep in sync with WikiPage::isSamePageAs()!
120        return $this->getWikiId() === $other->getWikiId()
121            && $this->getNamespace() === $other->getNamespace()
122            && $this->getDBkey() === $other->getDBkey();
123    }
124
125    /**
126     * Returns a string representation of the title, for logging. This is purely informative
127     * and must not be used programmatically.
128     *
129     * @return string
130     */
131    public function __toString(): string {
132        $s = '[' . $this->namespace . ':' . $this->dbKey . ']';
133
134        if ( $this->wikiId ) {
135            $s .= '@' . $this->wikiId;
136        }
137
138        return $s;
139    }
140
141}