From f6d65e533c62f6deb21342d4901ece24497b433e Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Thu, 4 Jun 2015 07:31:04 +0200 Subject: Update to MediaWiki 1.25.1 --- includes/title/ForeignTitle.php | 117 ++++++++++++++++++ includes/title/ForeignTitleFactory.php | 36 ++++++ includes/title/ImportTitleFactory.php | 36 ++++++ includes/title/MalformedTitleException.php | 1 + includes/title/MediaWikiPageLinkRenderer.php | 1 + includes/title/MediaWikiTitleCodec.php | 34 +++++- includes/title/NaiveForeignTitleFactory.php | 71 +++++++++++ includes/title/NaiveImportTitleFactory.php | 65 ++++++++++ .../title/NamespaceAwareForeignTitleFactory.php | 134 +++++++++++++++++++++ includes/title/NamespaceImportTitleFactory.php | 52 ++++++++ includes/title/PageLinkRenderer.php | 1 + includes/title/SubpageImportTitleFactory.php | 55 +++++++++ includes/title/TitleFormatter.php | 1 + includes/title/TitleParser.php | 1 + includes/title/TitleValue.php | 1 + 15 files changed, 604 insertions(+), 2 deletions(-) create mode 100644 includes/title/ForeignTitle.php create mode 100644 includes/title/ForeignTitleFactory.php create mode 100644 includes/title/ImportTitleFactory.php create mode 100644 includes/title/NaiveForeignTitleFactory.php create mode 100644 includes/title/NaiveImportTitleFactory.php create mode 100644 includes/title/NamespaceAwareForeignTitleFactory.php create mode 100644 includes/title/NamespaceImportTitleFactory.php create mode 100644 includes/title/SubpageImportTitleFactory.php (limited to 'includes/title') 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 @@ +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 @@ + and 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 @@ + and 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 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 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 @@ + 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 @@ + '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 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 { /** -- cgit v1.2.2