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
21 * @author This, that and the other
24 namespace MediaWiki\Title
;
30 * A simple, immutable structure to hold the title of a page on a foreign
31 * MediaWiki installation.
33 class ForeignTitle
implements Stringable
{
36 * Null if we don't know the namespace ID (e.g. interwiki links)
40 private $namespaceName;
45 * Creates a new ForeignTitle object.
47 * @param int|null $namespaceId Null if the namespace ID is unknown (e.g.
49 * @param string $namespaceName
50 * @param string $pageName
52 public function __construct( $namespaceId, $namespaceName, $pageName ) {
53 if ( $namespaceId === null ) {
54 $this->namespaceId
= null;
56 $this->namespaceId
= intval( $namespaceId );
58 $this->namespaceName
= str_replace( ' ', '_', $namespaceName );
59 $this->pageName
= str_replace( ' ', '_', $pageName );
63 * Do we know the namespace ID of the page on the foreign wiki?
66 public function isNamespaceIdKnown() {
67 return $this->namespaceId
!== null;
71 * @note Callers should make sure that isNamespaceIdKnown() is true before calling this method.
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" );
79 return $this->namespaceId
;
83 public function getNamespaceName() {
84 return $this->namespaceName
;
88 public function getText() {
89 return $this->pageName
;
93 public function getFullText() {
95 if ( $this->namespaceName
) {
96 $result .= $this->namespaceName
. ':';
98 $result .= $this->pageName
;
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
110 public function __toString() {
112 if ( $this->isNamespaceIdKnown() ) {
113 $name .= '{ns' . $this->namespaceId
. '}';
117 $name .= $this->namespaceName
. ':' . $this->pageName
;
123 /** @deprecated class alias since 1.41 */
124 class_alias( ForeignTitle
::class, 'ForeignTitle' );