Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
PageIdentityValue
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
6 / 6
7
100.00% covered (success)
100.00%
1 / 1
 tryNew
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 localIdentity
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 exists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 canExist
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Page;
8
9use InvalidArgumentException;
10use Wikimedia\Assert\Assert;
11
12/**
13 * Immutable value object representing a page identity.
14 *
15 * Instances of this class are expected to always represent proper pages, that is,
16 * pages that can at least potentially exist as editable pages on the wiki.
17 * This class cannot represent Special pages, interwiki links, section links, etc.
18 *
19 * Code that deserializes instances of PageIdentityValue must ensure that the original
20 * meaning of the "local" Wiki ID is preserved: When an instance of PageIdentityValue
21 * is created with self::LOCAL as the Wiki ID on one wiki, gets serialized,
22 * stored, and later read and unserialized on another wiki, the value of the Wiki ID
23 * must be adjusted to refer to the original wiki.
24 *
25 * @see https://www.mediawiki.org/wiki/Manual:Modeling_pages
26 *
27 * @since 1.36
28 */
29class PageIdentityValue extends PageReferenceValue implements ProperPageIdentity {
30
31    /** @var int */
32    private $pageId;
33
34    /**
35     * Constructs a PageIdentityValue, or returns null if the parameters are not valid.
36     *
37     * @note This does not perform any normalization, and only basic validation.
38     * For full normalization and validation, use TitleParser::makeTitleValueSafe()
39     * together with PageLookup::getPageForLink().
40     *
41     * @param int $pageId The ID of this page, or 0 if the page does not exist.
42     * @param int $namespace A valid namespace ID. Validation is the caller's responsibility!
43     * @param string $dbKey A valid DB key. Validation is the caller's responsibility!
44     * @param string|false $wikiId The Id of the wiki this page belongs to,
45     *        or self::LOCAL for the local wiki. The method {@link PageIdentityValue::localIdentity}
46     *        is available as a shorthand for local wikis (only requires 3 parameters).
47     */
48    public static function tryNew( int $pageId, int $namespace, string $dbKey, $wikiId ): ?static {
49        try {
50            return new static( $pageId, $namespace, $dbKey, $wikiId );
51        } catch ( InvalidArgumentException ) {
52            return null;
53        }
54    }
55
56    /**
57     * @param int $pageId The ID of this page, or 0 if the page does not exist.
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 $pageId, int $namespace, string $dbKey, $wikiId ) {
64        Assert::parameter( $pageId >= 0, '$pageId', 'must not be negative' );
65        Assert::parameter( $namespace >= 0, '$namespace', 'must not be negative' );
66
67        // Not full validation, intended to help detect lack of validation in the caller.
68        Assert::parameter(
69            !preg_match( '/[#|]/', $dbKey ),
70            '$dbKey',
71            'must not contain pipes or hashes: ' . $dbKey
72        );
73
74        parent::__construct( $namespace, $dbKey, $wikiId );
75
76        $this->pageId = $pageId;
77    }
78
79    /**
80     * Create PageIdentity for a local page.
81     *
82     * @param int $pageId
83     * @param int $namespace
84     * @param string $dbKey
85     * @return PageIdentityValue
86     */
87    public static function localIdentity( int $pageId, int $namespace, string $dbKey ): self {
88        return new self( $pageId, $namespace, $dbKey, self::LOCAL );
89    }
90
91    /**
92     * The numerical page ID provided to the constructor.
93     *
94     * @param string|false $wikiId The wiki ID expected by the caller.
95     *        Omit if expecting the local wiki.
96     *
97     * @return int
98     */
99    public function getId( $wikiId = self::LOCAL ): int {
100        $this->assertWiki( $wikiId );
101        return $this->pageId;
102    }
103
104    /**
105     * Returns whether the page currently exists.
106     * Returns true if getId() returns a value greater than zero.
107     */
108    public function exists(): bool {
109        return $this->getId( $this->getWikiId() ) > 0;
110    }
111
112    /**
113     * @return bool always true
114     */
115    public function canExist(): bool {
116        return true;
117    }
118
119}