Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / page / PageIdentityValue.php
blob42c806a7f331fe2241e3459f6f4172d3a7392dcc
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 InvalidArgumentException;
24 use Wikimedia\Assert\Assert;
26 /**
27 * Immutable value object representing a page identity.
29 * Instances of this class are expected to always represent proper pages, that is,
30 * pages that can at least potentially exist as editable pages on the wiki.
31 * This class cannot represent Special pages, interwiki links, section links, etc.
33 * Code that deserializes instances of PageIdentityValue must ensure that the original
34 * meaning of the "local" Wiki ID is preserved: When an instance of PageIdentityValue
35 * is created with self::LOCAL as the Wiki ID on one wiki, gets serialized,
36 * stored, and later read and unserialized on another wiki, the value of the Wiki ID
37 * must be adjusted to refer to the original wiki.
39 * @see https://www.mediawiki.org/wiki/Manual:Modeling_pages
41 * @since 1.36
43 class PageIdentityValue extends PageReferenceValue implements ProperPageIdentity {
45 /** @var int */
46 private $pageId;
48 /**
49 * Constructs a PageIdentityValue, or returns null if the parameters are not valid.
51 * @note This does not perform any normalization, and only basic validation.
52 * For full normalization and validation, use TitleParser::makeTitleValueSafe()
53 * together with PageLookup::getPageForLink().
55 * @param int $pageId The ID of this page, or 0 if the page does not exist.
56 * @param int $namespace A valid namespace ID. Validation is the caller's responsibility!
57 * @param string $dbKey A valid DB key. Validation is the caller's responsibility!
58 * @param string|false $wikiId The Id of the wiki this page belongs to,
59 * or self::LOCAL for the local wiki.
61 * @return PageIdentityValue|null
63 public static function tryNew( int $pageId, int $namespace, string $dbKey, $wikiId ) {
64 try {
65 return new static( $pageId, $namespace, $dbKey, $wikiId );
66 } catch ( InvalidArgumentException $ex ) {
67 return null;
71 /**
72 * @param int $pageId The ID of this page, or 0 if the page does not exist.
73 * @param int $namespace A valid namespace ID. Validation is the caller's responsibility!
74 * @param string $dbKey A valid DB key. Validation is the caller's responsibility!
75 * @param string|false $wikiId The Id of the wiki this page belongs to,
76 * or self::LOCAL for the local wiki.
78 public function __construct( int $pageId, int $namespace, string $dbKey, $wikiId ) {
79 Assert::parameter( $pageId >= 0, '$pageId', 'must not be negative' );
80 Assert::parameter( $namespace >= 0, '$namespace', 'must not be negative' );
82 // Not full validation, intended to help detect lack of validation in the caller.
83 Assert::parameter(
84 !preg_match( '/[#|]/', $dbKey ),
85 '$dbKey',
86 'must not contain pipes or hashes: ' . $dbKey
89 parent::__construct( $namespace, $dbKey, $wikiId );
91 $this->pageId = $pageId;
94 /**
95 * Create PageIdentity for a local page.
97 * @param int $pageId
98 * @param int $namespace
99 * @param string $dbKey
100 * @return PageIdentityValue
102 public static function localIdentity( int $pageId, int $namespace, string $dbKey ): self {
103 return new self( $pageId, $namespace, $dbKey, self::LOCAL );
107 * The numerical page ID provided to the constructor.
109 * @param string|false $wikiId The wiki ID expected by the caller.
110 * Omit if expecting the local wiki.
112 * @return int
114 public function getId( $wikiId = self::LOCAL ): int {
115 $this->assertWiki( $wikiId );
116 return $this->pageId;
120 * Returns whether the page currently exists.
121 * Returns true if getId() returns a value greater than zero.
122 * @return bool
124 public function exists(): bool {
125 return $this->getId( $this->getWikiId() ) > 0;
129 * @return bool always true
131 public function canExist(): bool {
132 return true;