MediaWiki master
NamespaceAwareForeignTitleFactory.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Title;
8
21 private $foreignNamespacesFlipped;
22
28 private function normalizeNamespaceName( $name ) {
29 return strtolower( str_replace( ' ', '_', $name ) );
30 }
31
39 public function __construct( $foreignNamespaces ) {
40 $this->foreignNamespaces = $foreignNamespaces;
41 if ( $foreignNamespaces !== null ) {
42 $this->foreignNamespacesFlipped = [];
43 foreach ( $foreignNamespaces as $id => $name ) {
44 $newKey = self::normalizeNamespaceName( $name );
45 $this->foreignNamespacesFlipped[$newKey] = $id;
46 }
47 }
48 }
49
61 public function createForeignTitle( $title, $ns = null ) {
62 // Export schema version 0.5 and earlier (MW 1.18 and earlier) does not
63 // contain a <ns> tag, so we need to be able to handle that case.
64 if ( $ns === null ) {
65 return self::parseTitleNoNs( $title );
66 } else {
67 return self::parseTitleWithNs( $title, $ns );
68 }
69 }
70
77 protected function parseTitleNoNs( $title ) {
78 $pieces = explode( ':', $title, 2 );
79 $key = self::normalizeNamespaceName( $pieces[0] );
80
81 // Does the part before the colon match a known namespace? Check the
82 // foreign namespaces
83 $isNamespacePartValid = isset( $this->foreignNamespacesFlipped[$key] );
84
85 if ( count( $pieces ) === 2 && $isNamespacePartValid ) {
86 [ $namespaceName, $pageName ] = $pieces;
87 $ns = $this->foreignNamespacesFlipped[$key];
88 } else {
89 $namespaceName = '';
90 $pageName = $title;
91 $ns = 0;
92 }
93
94 return new ForeignTitle( $ns, $namespaceName, $pageName );
95 }
96
104 protected function parseTitleWithNs( $title, $ns ) {
105 $pieces = explode( ':', $title, 2 );
106
107 // Is $title of the form Namespace:Title (true), or just Title (false)?
108 $titleIncludesNamespace = ( $ns != '0' && count( $pieces ) === 2 );
109
110 if ( isset( $this->foreignNamespaces[$ns] ) ) {
111 $namespaceName = $this->foreignNamespaces[$ns];
112 } else {
113 // If the foreign wiki is misconfigured, XML dumps can contain a page with
114 // a non-zero namespace ID, but whose title doesn't contain a colon
115 // (T114115). In those cases, output a made-up namespace name to avoid
116 // collisions. The ImportTitleFactory might replace this with something
117 // more appropriate.
118 $namespaceName = $titleIncludesNamespace ? $pieces[0] : "Ns$ns";
119 }
120
121 // We assume that the portion of the page title before the colon is the
122 // namespace name, except in the case of namespace 0.
123 if ( $titleIncludesNamespace ) {
124 $pageName = $pieces[1];
125 } else {
126 $pageName = $title;
127 }
128
129 return new ForeignTitle( $ns, $namespaceName, $pageName );
130 }
131}
132
134class_alias( NamespaceAwareForeignTitleFactory::class, 'NamespaceAwareForeignTitleFactory' );
A simple, immutable structure to hold the title of a page on a foreign MediaWiki installation.
A parser that translates page titles on a foreign wiki into ForeignTitle objects, using information a...
parseTitleWithNs( $title, $ns)
Helper function to parse the title when the namespace value is known.
createForeignTitle( $title, $ns=null)
Create a ForeignTitle object.
parseTitleNoNs( $title)
Helper function to parse the title when the namespace ID is not specified.
A parser that translates page titles into ForeignTitle objects.