3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
23 * A parser that translates page titles on a foreign wiki into ForeignTitle
24 * objects, using information about the namespace setup on the foreign site.
26 class NamespaceAwareForeignTitleFactory
implements ForeignTitleFactory
{
30 protected $foreignNamespaces;
34 private $foreignNamespacesFlipped;
37 * Normalizes an array name for $foreignNamespacesFlipped.
41 private function normalizeNamespaceName( $name ) {
42 return strtolower( str_replace( ' ', '_', $name ) );
46 * @param array|null $foreignNamespaces An array 'id' => 'name' which contains
47 * the complete namespace setup of the foreign wiki. Such data could be
48 * obtained from siteinfo/namespaces in an XML dump file, or by an action API
49 * query such as api.php?action=query&meta=siteinfo&siprop=namespaces. If
50 * this data is unavailable, use NaiveForeignTitleFactory instead.
52 public function __construct( $foreignNamespaces ) {
53 $this->foreignNamespaces
= $foreignNamespaces;
54 if ( !is_null( $foreignNamespaces ) ) {
55 $this->foreignNamespacesFlipped
= [];
56 foreach ( $foreignNamespaces as $id => $name ) {
57 $newKey = self
::normalizeNamespaceName( $name );
58 $this->foreignNamespacesFlipped
[$newKey] = $id;
64 * Creates a ForeignTitle object based on the page title, and optionally the
65 * namespace ID, of a page on a foreign wiki. These values could be, for
66 * example, the <title> and <ns> attributes found in an XML dump.
68 * @param string $title The page title
69 * @param int|null $ns The namespace ID, or null if this data is not available
70 * @return ForeignTitle
72 public function createForeignTitle( $title, $ns = null ) {
73 // Export schema version 0.5 and earlier (MW 1.18 and earlier) does not
74 // contain a <ns> tag, so we need to be able to handle that case.
75 if ( is_null( $ns ) ) {
76 return self
::parseTitleNoNs( $title );
78 return self
::parseTitleWithNs( $title, $ns );
83 * Helper function to parse the title when the namespace ID is not specified.
85 * @param string $title
86 * @return ForeignTitle
88 protected function parseTitleNoNs( $title ) {
89 $pieces = explode( ':', $title, 2 );
90 $key = self
::normalizeNamespaceName( $pieces[0] );
92 // Does the part before the colon match a known namespace? Check the
94 $isNamespacePartValid = isset( $this->foreignNamespacesFlipped
[$key] );
96 if ( count( $pieces ) === 2 && $isNamespacePartValid ) {
97 list( $namespaceName, $pageName ) = $pieces;
98 $ns = $this->foreignNamespacesFlipped
[$key];
105 return new ForeignTitle( $ns, $namespaceName, $pageName );
109 * Helper function to parse the title when the namespace value is known.
111 * @param string $title
113 * @return ForeignTitle
115 protected function parseTitleWithNs( $title, $ns ) {
116 $pieces = explode( ':', $title, 2 );
118 if ( isset( $this->foreignNamespaces
[$ns] ) ) {
119 $namespaceName = $this->foreignNamespaces
[$ns];
121 $namespaceName = $ns == '0' ?
'' : $pieces[0];
124 // We assume that the portion of the page title before the colon is the
125 // namespace name, except in the case of namespace 0
127 $pageName = $pieces[1];
132 return new ForeignTitle( $ns, $namespaceName, $pageName );