summaryrefslogtreecommitdiff
path: root/includes/title
diff options
context:
space:
mode:
Diffstat (limited to 'includes/title')
-rw-r--r--includes/title/ForeignTitle.php117
-rw-r--r--includes/title/ForeignTitleFactory.php36
-rw-r--r--includes/title/ImportTitleFactory.php36
-rw-r--r--includes/title/MalformedTitleException.php1
-rw-r--r--includes/title/MediaWikiPageLinkRenderer.php1
-rw-r--r--includes/title/MediaWikiTitleCodec.php34
-rw-r--r--includes/title/NaiveForeignTitleFactory.php71
-rw-r--r--includes/title/NaiveImportTitleFactory.php65
-rw-r--r--includes/title/NamespaceAwareForeignTitleFactory.php134
-rw-r--r--includes/title/NamespaceImportTitleFactory.php52
-rw-r--r--includes/title/PageLinkRenderer.php1
-rw-r--r--includes/title/SubpageImportTitleFactory.php55
-rw-r--r--includes/title/TitleFormatter.php1
-rw-r--r--includes/title/TitleParser.php1
-rw-r--r--includes/title/TitleValue.php1
15 files changed, 604 insertions, 2 deletions
diff --git a/includes/title/ForeignTitle.php b/includes/title/ForeignTitle.php
new file mode 100644
index 00000000..ed96d17c
--- /dev/null
+++ b/includes/title/ForeignTitle.php
@@ -0,0 +1,117 @@
+<?php
+/**
+ * A structure to hold the title of a page on a foreign MediaWiki installation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author This, that and the other
+ */
+
+/**
+ * A simple, immutable structure to hold the title of a page on a foreign
+ * MediaWiki installation.
+ */
+class ForeignTitle {
+ /**
+ * @var int|null
+ * Null if we don't know the namespace ID (e.g. interwiki links)
+ */
+ protected $namespaceId;
+ /** @var string */
+ protected $namespaceName;
+ /** @var string */
+ protected $pageName;
+
+ /**
+ * Creates a new ForeignTitle object.
+ *
+ * @param int|null $namespaceId Null if the namespace ID is unknown (e.g.
+ * interwiki links)
+ * @param string $namespaceName
+ * @param string $pageName
+ */
+ public function __construct( $namespaceId, $namespaceName, $pageName ) {
+ if ( is_null( $namespaceId ) ) {
+ $this->namespaceId = null;
+ } else {
+ $this->namespaceId = intval( $namespaceId );
+ }
+ $this->namespaceName = str_replace( ' ', '_', $namespaceName );
+ $this->pageName = str_replace( ' ', '_', $pageName );
+ }
+
+ /**
+ * Do we know the namespace ID of the page on the foreign wiki?
+ * @return bool
+ */
+ public function isNamespaceIdKnown() {
+ return !is_null( $this->namespaceId );
+ }
+
+ /**
+ * @return int
+ * @throws MWException If isNamespaceIdKnown() is false, it does not make
+ * sense to call this function.
+ */
+ public function getNamespaceId() {
+ if ( is_null( $this->namespaceId ) ) {
+ throw new MWException(
+ "Attempted to call getNamespaceId when the namespace ID is not known" );
+ }
+ return $this->namespaceId;
+ }
+
+ /** @return string */
+ public function getNamespaceName() {
+ return $this->namespaceName;
+ }
+
+ /** @return string */
+ public function getText() {
+ return $this->pageName;
+ }
+
+ /** @return string */
+ public function getFullText() {
+ $result = '';
+ if ( $this->namespaceName ) {
+ $result .= $this->namespaceName . ':';
+ }
+ $result .= $this->pageName;
+ return $result;
+ }
+
+ /**
+ * Returns a string representation of the title, for logging. This is purely
+ * informative and must not be used programmatically. Use the appropriate
+ * ImportTitleFactory to generate the correct string representation for a
+ * given use.
+ *
+ * @return string
+ */
+ public function __toString() {
+ $name = '';
+ if ( $this->isNamespaceIdKnown() ) {
+ $name .= '{ns' . $this->namespaceId . '}';
+ } else {
+ $name .= '{ns??}';
+ }
+ $name .= $this->namespaceName . ':' . $this->pageName;
+
+ return $name;
+ }
+}
diff --git a/includes/title/ForeignTitleFactory.php b/includes/title/ForeignTitleFactory.php
new file mode 100644
index 00000000..427afdf3
--- /dev/null
+++ b/includes/title/ForeignTitleFactory.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @license GPL 2+
+ */
+
+/**
+ * A parser that translates page titles into ForeignTitle objects.
+ */
+interface ForeignTitleFactory {
+ /**
+ * Creates a ForeignTitle object based on the page title, and optionally the
+ * namespace ID, of a page on a foreign wiki. These values could be, for
+ * example, the <title> and <ns> attributes found in an XML dump.
+ *
+ * @param string $title The page title
+ * @param int|null $ns The namespace ID, or null if this data is not available
+ * @return ForeignTitle
+ */
+ public function createForeignTitle( $title, $ns = null );
+}
diff --git a/includes/title/ImportTitleFactory.php b/includes/title/ImportTitleFactory.php
new file mode 100644
index 00000000..629616d8
--- /dev/null
+++ b/includes/title/ImportTitleFactory.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @license GPL 2+
+ */
+
+/**
+ * Represents an object that can convert page titles on a foreign wiki
+ * (ForeignTitle objects) into page titles on the local wiki (Title objects).
+ */
+interface ImportTitleFactory {
+ /**
+ * Determines which local title best corresponds to the given foreign title.
+ * If such a title can't be found or would be locally invalid, null is
+ * returned.
+ *
+ * @param ForeignTitle $foreignTitle The ForeignTitle to convert
+ * @return Title|null
+ */
+ public function createTitleFromForeignTitle( ForeignTitle $foreignTitle );
+}
diff --git a/includes/title/MalformedTitleException.php b/includes/title/MalformedTitleException.php
index a8a5d754..a9e58b3e 100644
--- a/includes/title/MalformedTitleException.php
+++ b/includes/title/MalformedTitleException.php
@@ -27,6 +27,7 @@
*
* @license GPL 2+
* @author Daniel Kinzler
+ * @since 1.23
*/
class MalformedTitleException extends Exception {
}
diff --git a/includes/title/MediaWikiPageLinkRenderer.php b/includes/title/MediaWikiPageLinkRenderer.php
index f46cb5e3..9ee48419 100644
--- a/includes/title/MediaWikiPageLinkRenderer.php
+++ b/includes/title/MediaWikiPageLinkRenderer.php
@@ -26,6 +26,7 @@
* A service for generating links from page titles.
*
* @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
+ * @since 1.23
*/
class MediaWikiPageLinkRenderer implements PageLinkRenderer {
/**
diff --git a/includes/title/MediaWikiTitleCodec.php b/includes/title/MediaWikiTitleCodec.php
index 6ca0799c..20034b74 100644
--- a/includes/title/MediaWikiTitleCodec.php
+++ b/includes/title/MediaWikiTitleCodec.php
@@ -31,6 +31,7 @@
* via parseTitle() or from a (semi)trusted source, such as the database.
*
* @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
+ * @since 1.23
*/
class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
/**
@@ -229,7 +230,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
);
$dbkey = trim( $dbkey, '_' );
- if ( strpos( $dbkey, UTF8_REPLACEMENT ) !== false ) {
+ if ( strpos( $dbkey, UtfNormal\Constants::UTF8_REPLACEMENT ) !== false ) {
# Contained illegal UTF-8 sequences or forbidden Unicode chars.
throw new MalformedTitleException( 'Bad UTF-8 sequences found in title: ' . $text );
}
@@ -322,7 +323,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
}
# Reject illegal characters.
- $rxTc = Title::getTitleInvalidRegex();
+ $rxTc = self::getTitleInvalidRegex();
if ( preg_match( $rxTc, $dbkey ) ) {
throw new MalformedTitleException( 'Illegal characters found in title: ' . $text );
}
@@ -397,4 +398,33 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
return $parts;
}
+
+ /**
+ * Returns a simple regex that will match on characters and sequences invalid in titles.
+ * Note that this doesn't pick up many things that could be wrong with titles, but that
+ * replacing this regex with something valid will make many titles valid.
+ * Previously Title::getTitleInvalidRegex()
+ *
+ * @return string Regex string
+ * @since 1.25
+ */
+ public static function getTitleInvalidRegex() {
+ static $rxTc = false;
+ if ( !$rxTc ) {
+ # Matching titles will be held as illegal.
+ $rxTc = '/' .
+ # Any character not allowed is forbidden...
+ '[^' . Title::legalChars() . ']' .
+ # URL percent encoding sequences interfere with the ability
+ # to round-trip titles -- you can't link to them consistently.
+ '|%[0-9A-Fa-f]{2}' .
+ # XML/HTML character references produce similar issues.
+ '|&[A-Za-z0-9\x80-\xff]+;' .
+ '|&#[0-9]+;' .
+ '|&#x[0-9A-Fa-f]+;' .
+ '/S';
+ }
+
+ return $rxTc;
+ }
}
diff --git a/includes/title/NaiveForeignTitleFactory.php b/includes/title/NaiveForeignTitleFactory.php
new file mode 100644
index 00000000..6c8bcc04
--- /dev/null
+++ b/includes/title/NaiveForeignTitleFactory.php
@@ -0,0 +1,71 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @license GPL 2+
+ */
+
+/**
+ * A parser that translates page titles on a foreign wiki into ForeignTitle
+ * objects, with no knowledge of the namespace setup on the foreign site.
+ */
+class NaiveForeignTitleFactory implements ForeignTitleFactory {
+ /**
+ * Creates a ForeignTitle object based on the page title, and optionally the
+ * namespace ID, of a page on a foreign wiki. These values could be, for
+ * example, the <title> and <ns> attributes found in an XML dump.
+ *
+ * Although exported XML dumps have contained a map of namespace IDs to names
+ * since MW 1.5, the importer used to completely ignore the <siteinfo> tag
+ * before MW 1.25. It is therefore possible that custom XML dumps (i.e. not
+ * generated by Special:Export) have been created without this metadata.
+ * As a result, this code falls back to using namespace data for the local
+ * wiki (similar to buggy pre-1.25 behaviour) if $ns is not supplied.
+ *
+ * @param string $title The page title
+ * @param int|null $ns The namespace ID, or null if this data is not available
+ * @return ForeignTitle
+ */
+ public function createForeignTitle( $title, $ns = null ) {
+ $pieces = explode( ':', $title, 2 );
+
+ global $wgContLang;
+
+ // Can we assume that the part of the page title before the colon is a
+ // namespace name?
+ //
+ // XML export schema version 0.5 and earlier (MW 1.18 and earlier) does not
+ // contain a <ns> tag, so we need to be able to handle that case.
+ //
+ // If we know the namespace ID, we assume a non-zero namespace ID means
+ // the ':' sets off a valid namespace name. If we don't know the namespace
+ // ID, we fall back to using the local wiki's namespace names to resolve
+ // this -- better than nothing, and mimics the old crappy behavior
+ $isNamespacePartValid = is_null( $ns ) ?
+ ( $wgContLang->getNsIndex( $pieces[0] ) !== false ) :
+ $ns != 0;
+
+ if ( count( $pieces ) === 2 && $isNamespacePartValid ) {
+ list( $namespaceName, $pageName ) = $pieces;
+ } else {
+ $namespaceName = '';
+ $pageName = $title;
+ }
+
+ return new ForeignTitle( $ns, $namespaceName, $pageName );
+ }
+}
diff --git a/includes/title/NaiveImportTitleFactory.php b/includes/title/NaiveImportTitleFactory.php
new file mode 100644
index 00000000..43c662e7
--- /dev/null
+++ b/includes/title/NaiveImportTitleFactory.php
@@ -0,0 +1,65 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @license GPL 2+
+ */
+
+/**
+ * A class to convert page titles on a foreign wiki (ForeignTitle objects) into
+ * page titles on the local wiki (Title objects), using a default namespace
+ * mapping.
+ *
+ * For built-in namespaces (0 <= ID < 100), we try to find a local namespace
+ * with the same namespace ID as the foreign page. If no such namespace exists,
+ * or the namespace ID is unknown or > 100, we look for a local namespace with
+ * a matching namespace name. If that can't be found, we dump the page in the
+ * main namespace as a last resort.
+ */
+class NaiveImportTitleFactory implements ImportTitleFactory {
+ /**
+ * Determines which local title best corresponds to the given foreign title.
+ * If such a title can't be found or would be locally invalid, null is
+ * returned.
+ *
+ * @param ForeignTitle $foreignTitle The ForeignTitle to convert
+ * @return Title|null
+ */
+ public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
+ global $wgContLang;
+
+ if ( $foreignTitle->isNamespaceIdKnown() ) {
+ $foreignNs = $foreignTitle->getNamespaceId();
+
+ // For built-in namespaces (0 <= ID < 100), we try to find a local NS with
+ // the same namespace ID
+ if ( $foreignNs < 100 && MWNamespace::exists( $foreignNs ) ) {
+ return Title::makeTitleSafe( $foreignNs, $foreignTitle->getText() );
+ }
+ }
+
+ // Do we have a local namespace by the same name as the foreign
+ // namespace?
+ $targetNs = $wgContLang->getNsIndex( $foreignTitle->getNamespaceName() );
+ if ( $targetNs !== false ) {
+ return Title::makeTitleSafe( $targetNs, $foreignTitle->getText() );
+ }
+
+ // Otherwise, just fall back to main namespace
+ return Title::makeTitleSafe( 0, $foreignTitle->getFullText() );
+ }
+}
diff --git a/includes/title/NamespaceAwareForeignTitleFactory.php b/includes/title/NamespaceAwareForeignTitleFactory.php
new file mode 100644
index 00000000..bf97e2cd
--- /dev/null
+++ b/includes/title/NamespaceAwareForeignTitleFactory.php
@@ -0,0 +1,134 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @license GPL 2+
+ */
+
+/**
+ * A parser that translates page titles on a foreign wiki into ForeignTitle
+ * objects, using information about the namespace setup on the foreign site.
+ */
+class NamespaceAwareForeignTitleFactory implements ForeignTitleFactory {
+ /**
+ * @var array
+ */
+ protected $foreignNamespaces;
+ /**
+ * @var array
+ */
+ private $foreignNamespacesFlipped;
+
+ /**
+ * Normalizes an array name for $foreignNamespacesFlipped.
+ * @param string $name
+ * @return string
+ */
+ private function normalizeNamespaceName( $name ) {
+ return strtolower( str_replace( ' ', '_', $name ) );
+ }
+
+ /**
+ * @param array|null $foreignNamespaces An array 'id' => 'name' which contains
+ * the complete namespace setup of the foreign wiki. Such data could be
+ * obtained from siteinfo/namespaces in an XML dump file, or by an action API
+ * query such as api.php?action=query&meta=siteinfo&siprop=namespaces. If
+ * this data is unavailable, use NaiveForeignTitleFactory instead.
+ */
+ public function __construct( $foreignNamespaces ) {
+ $this->foreignNamespaces = $foreignNamespaces;
+ if ( !is_null( $foreignNamespaces ) ) {
+ $this->foreignNamespacesFlipped = array();
+ foreach ( $foreignNamespaces as $id => $name ) {
+ $newKey = self::normalizeNamespaceName( $name );
+ $this->foreignNamespacesFlipped[$newKey] = $id;
+ }
+ }
+ }
+
+ /**
+ * Creates a ForeignTitle object based on the page title, and optionally the
+ * namespace ID, of a page on a foreign wiki. These values could be, for
+ * example, the <title> and <ns> attributes found in an XML dump.
+ *
+ * @param string $title The page title
+ * @param int|null $ns The namespace ID, or null if this data is not available
+ * @return ForeignTitle
+ */
+ public function createForeignTitle( $title, $ns = null ) {
+ // Export schema version 0.5 and earlier (MW 1.18 and earlier) does not
+ // contain a <ns> tag, so we need to be able to handle that case.
+ if ( is_null( $ns ) ) {
+ return self::parseTitleNoNs( $title );
+ } else {
+ return self::parseTitleWithNs( $title, $ns );
+ }
+ }
+
+ /**
+ * Helper function to parse the title when the namespace ID is not specified.
+ *
+ * @param string $title
+ * @return ForeignTitle
+ */
+ protected function parseTitleNoNs( $title ) {
+ $pieces = explode( ':', $title, 2 );
+ $key = self::normalizeNamespaceName( $pieces[0] );
+
+ // Does the part before the colon match a known namespace? Check the
+ // foreign namespaces
+ $isNamespacePartValid = isset( $this->foreignNamespacesFlipped[$key] );
+
+ if ( count( $pieces ) === 2 && $isNamespacePartValid ) {
+ list( $namespaceName, $pageName ) = $pieces;
+ $ns = $this->foreignNamespacesFlipped[$key];
+ } else {
+ $namespaceName = '';
+ $pageName = $title;
+ $ns = 0;
+ }
+
+ return new ForeignTitle( $ns, $namespaceName, $pageName );
+ }
+
+ /**
+ * Helper function to parse the title when the namespace value is known.
+ *
+ * @param string $title
+ * @param int $ns
+ * @return ForeignTitle
+ */
+ protected function parseTitleWithNs( $title, $ns ) {
+ $pieces = explode( ':', $title, 2 );
+
+ if ( isset( $this->foreignNamespaces[$ns] ) ) {
+ $namespaceName = $this->foreignNamespaces[$ns];
+ } else {
+ $namespaceName = $ns == '0' ? '' : $pieces[0];
+ }
+
+ // We assume that the portion of the page title before the colon is the
+ // namespace name, except in the case of namespace 0
+ if ( $ns != '0' ) {
+ $pageName = $pieces[1];
+ } else {
+ $pageName = $title;
+ }
+
+ return new ForeignTitle( $ns, $namespaceName, $pageName );
+ }
+}
diff --git a/includes/title/NamespaceImportTitleFactory.php b/includes/title/NamespaceImportTitleFactory.php
new file mode 100644
index 00000000..0c1d0c40
--- /dev/null
+++ b/includes/title/NamespaceImportTitleFactory.php
@@ -0,0 +1,52 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @license GPL 2+
+ */
+
+/**
+ * A class to convert page titles on a foreign wiki (ForeignTitle objects) into
+ * page titles on the local wiki (Title objects), placing all pages in a fixed
+ * local namespace.
+ */
+class NamespaceImportTitleFactory implements ImportTitleFactory {
+ /** @var int */
+ protected $ns;
+
+ /**
+ * @param int $ns The namespace to use for all pages
+ */
+ public function __construct( $ns ) {
+ if ( !MWNamespace::exists( $ns ) ) {
+ throw new MWException( "Namespace $ns doesn't exist on this wiki" );
+ }
+ $this->ns = $ns;
+ }
+
+ /**
+ * Determines which local title best corresponds to the given foreign title.
+ * If such a title can't be found or would be locally invalid, null is
+ * returned.
+ *
+ * @param ForeignTitle $foreignTitle The ForeignTitle to convert
+ * @return Title|null
+ */
+ public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
+ return Title::makeTitleSafe( $this->ns, $foreignTitle->getText() );
+ }
+}
diff --git a/includes/title/PageLinkRenderer.php b/includes/title/PageLinkRenderer.php
index fb1096e0..ca91f583 100644
--- a/includes/title/PageLinkRenderer.php
+++ b/includes/title/PageLinkRenderer.php
@@ -29,6 +29,7 @@
* URLs, and how links are encoded in a given output format.
*
* @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
+ * @since 1.23
*/
interface PageLinkRenderer {
/**
diff --git a/includes/title/SubpageImportTitleFactory.php b/includes/title/SubpageImportTitleFactory.php
new file mode 100644
index 00000000..b0be7afa
--- /dev/null
+++ b/includes/title/SubpageImportTitleFactory.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @license GPL 2+
+ */
+
+/**
+ * A class to convert page titles on a foreign wiki (ForeignTitle objects) into
+ * page titles on the local wiki (Title objects), placing all pages as subpages
+ * of a given root page.
+ */
+class SubpageImportTitleFactory implements ImportTitleFactory {
+ /** @var Title */
+ protected $rootPage;
+
+ /**
+ * @param Title $rootPage The root page under which all pages should be
+ * created
+ */
+ public function __construct( Title $rootPage ) {
+ if ( !MWNamespace::hasSubpages( $rootPage->getNamespace() ) ) {
+ throw new MWException( "The root page you specified, $rootPage, is in a " .
+ "namespace where subpages are not allowed" );
+ }
+ $this->rootPage = $rootPage;
+ }
+
+ /**
+ * Determines which local title best corresponds to the given foreign title.
+ * If such a title can't be found or would be locally invalid, null is
+ * returned.
+ *
+ * @param ForeignTitle $foreignTitle The ForeignTitle to convert
+ * @return Title|null
+ */
+ public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
+ return Title::newFromText( $this->rootPage->getPrefixedDBkey() . '/' .
+ $foreignTitle->getFullText() );
+ }
+}
diff --git a/includes/title/TitleFormatter.php b/includes/title/TitleFormatter.php
index 7c71ef5e..aad83769 100644
--- a/includes/title/TitleFormatter.php
+++ b/includes/title/TitleFormatter.php
@@ -29,6 +29,7 @@
* forms to be used in the database, in urls, in wikitext, etc.
*
* @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
+ * @since 1.23
*/
interface TitleFormatter {
/**
diff --git a/includes/title/TitleParser.php b/includes/title/TitleParser.php
index 0635ee86..381b1d09 100644
--- a/includes/title/TitleParser.php
+++ b/includes/title/TitleParser.php
@@ -29,6 +29,7 @@
* forms to be used in the database, in urls, in wikitext, etc.
*
* @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
+ * @since 1.23
*/
interface TitleParser {
/**
diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php
index 402247c2..5cac3470 100644
--- a/includes/title/TitleValue.php
+++ b/includes/title/TitleValue.php
@@ -32,6 +32,7 @@
* It does not represent a link, and does not support interwiki prefixes etc.
*
* @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
+ * @since 1.23
*/
class TitleValue {
/**