Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / page / PageReferenceValue.php
blobfb3ec4d4dd3095f9864cd8ad21172caa41a2a28d
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.
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.
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
18 * @file
21 namespace MediaWiki\Page;
23 use MediaWiki\DAO\WikiAwareEntityTrait;
24 use Stringable;
25 use Wikimedia\Assert\Assert;
26 use Wikimedia\NonSerializable\NonSerializableTrait;
28 /**
29 * Immutable value object representing a page reference.
31 * Instances of this class are expected to always represent a viewable pages, that is,
32 * pages that can at least potentially be visited on the wiki.
33 * This class may represent Special pages, but not interwiki links, section links, etc.
35 * Code that deserializes instances of PageReferenceValue must ensure that the original
36 * meaning of the "local" Wiki ID is preserved: When an instance of PageReferenceValue
37 * is created with self::LOCAL as the Wiki ID on one wiki, gets serialized,
38 * stored, and later read and unserialized on another wiki, the value of the Wiki ID
39 * must be adjusted to refer to the original wiki.
41 * @since 1.37
43 class PageReferenceValue implements Stringable, PageReference {
45 /* Use JSON, but beware the note on serialization above. */
46 use NonSerializableTrait;
47 use WikiAwareEntityTrait;
49 /** @var int */
50 private $namespace;
52 /** @var string */
53 private $dbKey;
55 /** @var string|false */
56 private $wikiId;
58 /**
59 * @param int $namespace A valid namespace ID. Validation is the caller's responsibility!
60 * @param string $dbKey A valid DB key. Validation is the caller's responsibility!
61 * @param string|false $wikiId The Id of the wiki this page belongs to,
62 * or self::LOCAL for the local wiki.
64 public function __construct( int $namespace, string $dbKey, $wikiId ) {
65 $this->assertWikiIdParam( $wikiId );
67 Assert::parameter( $dbKey !== '', '$dbKey', 'must not be empty' );
69 // Replace spaces with underscores
70 $dbKey = str_replace( ' ', '_', $dbKey );
72 $this->wikiId = $wikiId;
73 $this->namespace = $namespace;
74 $this->dbKey = $dbKey;
77 /**
78 * Create PageReference for a local page.
80 * @param int $namespace
81 * @param string $dbKey
82 * @return PageReferenceValue
84 public static function localReference( int $namespace, string $dbKey ): self {
85 return new self( $namespace, $dbKey, self::LOCAL );
88 /**
89 * Get the ID of the wiki provided to the constructor.
91 * @return string|false
93 public function getWikiId() {
94 return $this->wikiId;
97 /**
98 * @inheritDoc
100 * @return int
102 public function getNamespace(): int {
103 return $this->namespace;
107 * @inheritDoc
109 * @return string
111 public function getDBkey(): string {
112 return $this->dbKey;
116 * @inheritDoc
118 public function isSamePageAs( PageReference $other ): bool {
119 // NOTE: keep in sync with Title::isSamePageAs()!
120 // NOTE: keep in sync with WikiPage::isSamePageAs()!
121 return $this->getWikiId() === $other->getWikiId()
122 && $this->getNamespace() === $other->getNamespace()
123 && $this->getDBkey() === $other->getDBkey();
127 * Returns a string representation of the title, for logging. This is purely informative
128 * and must not be used programmatically.
130 * @return string
132 public function __toString(): string {
133 $s = '[' . $this->namespace . ':' . $this->dbKey . ']';
135 if ( $this->wikiId ) {
136 $s .= '@' . $this->wikiId;
139 return $s;