Merge "objectcache: Add BagOStuff::READ_VERIFIED flag to get()"
[mediawiki.git] / includes / title / ForeignTitle.php
blobed96d17cde4cc0835e29007716a0182fd2974841
1 <?php
2 /**
3 * A structure to hold the title of a page on a foreign MediaWiki installation
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.
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.
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
20 * @file
21 * @author This, that and the other
24 /**
25 * A simple, immutable structure to hold the title of a page on a foreign
26 * MediaWiki installation.
28 class ForeignTitle {
29 /**
30 * @var int|null
31 * Null if we don't know the namespace ID (e.g. interwiki links)
33 protected $namespaceId;
34 /** @var string */
35 protected $namespaceName;
36 /** @var string */
37 protected $pageName;
39 /**
40 * Creates a new ForeignTitle object.
42 * @param int|null $namespaceId Null if the namespace ID is unknown (e.g.
43 * interwiki links)
44 * @param string $namespaceName
45 * @param string $pageName
47 public function __construct( $namespaceId, $namespaceName, $pageName ) {
48 if ( is_null( $namespaceId ) ) {
49 $this->namespaceId = null;
50 } else {
51 $this->namespaceId = intval( $namespaceId );
53 $this->namespaceName = str_replace( ' ', '_', $namespaceName );
54 $this->pageName = str_replace( ' ', '_', $pageName );
57 /**
58 * Do we know the namespace ID of the page on the foreign wiki?
59 * @return bool
61 public function isNamespaceIdKnown() {
62 return !is_null( $this->namespaceId );
65 /**
66 * @return int
67 * @throws MWException If isNamespaceIdKnown() is false, it does not make
68 * sense to call this function.
70 public function getNamespaceId() {
71 if ( is_null( $this->namespaceId ) ) {
72 throw new MWException(
73 "Attempted to call getNamespaceId when the namespace ID is not known" );
75 return $this->namespaceId;
78 /** @return string */
79 public function getNamespaceName() {
80 return $this->namespaceName;
83 /** @return string */
84 public function getText() {
85 return $this->pageName;
88 /** @return string */
89 public function getFullText() {
90 $result = '';
91 if ( $this->namespaceName ) {
92 $result .= $this->namespaceName . ':';
94 $result .= $this->pageName;
95 return $result;
98 /**
99 * Returns a string representation of the title, for logging. This is purely
100 * informative and must not be used programmatically. Use the appropriate
101 * ImportTitleFactory to generate the correct string representation for a
102 * given use.
104 * @return string
106 public function __toString() {
107 $name = '';
108 if ( $this->isNamespaceIdKnown() ) {
109 $name .= '{ns' . $this->namespaceId . '}';
110 } else {
111 $name .= '{ns??}';
113 $name .= $this->namespaceName . ':' . $this->pageName;
115 return $name;