]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/title/ForeignTitle.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / title / ForeignTitle.php
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
24 /**
25  * A simple, immutable structure to hold the title of a page on a foreign
26  * MediaWiki installation.
27  */
28 class ForeignTitle {
29         /**
30          * @var int|null
31          * Null if we don't know the namespace ID (e.g. interwiki links)
32          */
33         private $namespaceId;
34         /** @var string */
35         private $namespaceName;
36         /** @var string */
37         private $pageName;
38
39         /**
40          * Creates a new ForeignTitle object.
41          *
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
46          */
47         public function __construct( $namespaceId, $namespaceName, $pageName ) {
48                 if ( is_null( $namespaceId ) ) {
49                         $this->namespaceId = null;
50                 } else {
51                         $this->namespaceId = intval( $namespaceId );
52                 }
53                 $this->namespaceName = str_replace( ' ', '_', $namespaceName );
54                 $this->pageName = str_replace( ' ', '_', $pageName );
55         }
56
57         /**
58          * Do we know the namespace ID of the page on the foreign wiki?
59          * @return bool
60          */
61         public function isNamespaceIdKnown() {
62                 return !is_null( $this->namespaceId );
63         }
64
65         /**
66          * @return int
67          * @throws MWException If isNamespaceIdKnown() is false, it does not make
68          * sense to call this function.
69          */
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" );
74                 }
75                 return $this->namespaceId;
76         }
77
78         /** @return string */
79         public function getNamespaceName() {
80                 return $this->namespaceName;
81         }
82
83         /** @return string */
84         public function getText() {
85                 return $this->pageName;
86         }
87
88         /** @return string */
89         public function getFullText() {
90                 $result = '';
91                 if ( $this->namespaceName ) {
92                         $result .= $this->namespaceName . ':';
93                 }
94                 $result .= $this->pageName;
95                 return $result;
96         }
97
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.
103          *
104          * @return string
105          */
106         public function __toString() {
107                 $name = '';
108                 if ( $this->isNamespaceIdKnown() ) {
109                         $name .= '{ns' . $this->namespaceId . '}';
110                 } else {
111                         $name .= '{ns??}';
112                 }
113                 $name .= $this->namespaceName . ':' . $this->pageName;
114
115                 return $name;
116         }
117 }