Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
16 / 24
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ForeignTitle
69.57% covered (warning)
69.57%
16 / 23
71.43% covered (warning)
71.43%
5 / 7
14.41
0.00% covered (danger)
0.00%
0 / 1
 __construct
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 isNamespaceIdKnown
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNamespaceId
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getNamespaceName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getText
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFullText
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 __toString
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * A structure to hold the title of a page on a foreign MediaWiki installation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author This, that and the other
22 */
23
24namespace MediaWiki\Title;
25
26use RuntimeException;
27use Stringable;
28
29/**
30 * A simple, immutable structure to hold the title of a page on a foreign
31 * MediaWiki installation.
32 */
33class ForeignTitle implements Stringable {
34    /**
35     * @var int|null
36     * Null if we don't know the namespace ID (e.g. interwiki links)
37     */
38    private $namespaceId;
39    /** @var string */
40    private $namespaceName;
41    /** @var string */
42    private $pageName;
43
44    /**
45     * Creates a new ForeignTitle object.
46     *
47     * @param int|null $namespaceId Null if the namespace ID is unknown (e.g.
48     * interwiki links)
49     * @param string $namespaceName
50     * @param string $pageName
51     */
52    public function __construct( $namespaceId, $namespaceName, $pageName ) {
53        if ( $namespaceId === null ) {
54            $this->namespaceId = null;
55        } else {
56            $this->namespaceId = intval( $namespaceId );
57        }
58        $this->namespaceName = str_replace( ' ', '_', $namespaceName );
59        $this->pageName = str_replace( ' ', '_', $pageName );
60    }
61
62    /**
63     * Do we know the namespace ID of the page on the foreign wiki?
64     * @return bool
65     */
66    public function isNamespaceIdKnown() {
67        return $this->namespaceId !== null;
68    }
69
70    /**
71     * @note Callers should make sure that isNamespaceIdKnown() is true before calling this method.
72     * @return int
73     */
74    public function getNamespaceId() {
75        if ( $this->namespaceId === null ) {
76            throw new RuntimeException(
77                "Attempted to call getNamespaceId when the namespace ID is not known" );
78        }
79        return $this->namespaceId;
80    }
81
82    /** @return string */
83    public function getNamespaceName() {
84        return $this->namespaceName;
85    }
86
87    /** @return string */
88    public function getText() {
89        return $this->pageName;
90    }
91
92    /** @return string */
93    public function getFullText() {
94        $result = '';
95        if ( $this->namespaceName ) {
96            $result .= $this->namespaceName . ':';
97        }
98        $result .= $this->pageName;
99        return $result;
100    }
101
102    /**
103     * Returns a string representation of the title, for logging. This is purely
104     * informative and must not be used programmatically. Use the appropriate
105     * ImportTitleFactory to generate the correct string representation for a
106     * given use.
107     *
108     * @return string
109     */
110    public function __toString() {
111        $name = '';
112        if ( $this->isNamespaceIdKnown() ) {
113            $name .= '{ns' . $this->namespaceId . '}';
114        } else {
115            $name .= '{ns??}';
116        }
117        $name .= $this->namespaceName . ':' . $this->pageName;
118
119        return $name;
120    }
121}
122
123/** @deprecated class alias since 1.41 */
124class_alias( ForeignTitle::class, 'ForeignTitle' );