From: jenkins-bot Date: Wed, 8 Mar 2017 05:07:56 +0000 (+0000) Subject: Merge "Handle missing namespace prefix in XML dumps more gracefully" X-Git-Tag: 1.31.0-rc.0~3843 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=22806b0a4509e97b56fb52b387e17e3c80fb7eb2;hp=cbc23bd4dae818680950d3ddb59939e1eb3b0074 Merge "Handle missing namespace prefix in XML dumps more gracefully" --- diff --git a/includes/export/XmlDumpWriter.php b/includes/export/XmlDumpWriter.php index 52bf0f0910..5a1f92c4cc 100644 --- a/includes/export/XmlDumpWriter.php +++ b/includes/export/XmlDumpWriter.php @@ -433,6 +433,9 @@ class XmlDumpWriter { global $wgContLang; $prefix = $wgContLang->getFormattedNsText( $title->getNamespace() ); + // @todo Emit some kind of warning to the user if $title->getNamespace() !== + // NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace) + if ( $prefix !== '' ) { $prefix .= ':'; } diff --git a/includes/title/NamespaceAwareForeignTitleFactory.php b/includes/title/NamespaceAwareForeignTitleFactory.php index 2d67a2877c..4d24cb850c 100644 --- a/includes/title/NamespaceAwareForeignTitleFactory.php +++ b/includes/title/NamespaceAwareForeignTitleFactory.php @@ -115,15 +115,23 @@ class NamespaceAwareForeignTitleFactory implements ForeignTitleFactory { protected function parseTitleWithNs( $title, $ns ) { $pieces = explode( ':', $title, 2 ); + // Is $title of the form Namespace:Title (true), or just Title (false)? + $titleIncludesNamespace = ( $ns != '0' && count( $pieces ) === 2 ); + if ( isset( $this->foreignNamespaces[$ns] ) ) { $namespaceName = $this->foreignNamespaces[$ns]; } else { - $namespaceName = $ns == '0' ? '' : $pieces[0]; + // If the foreign wiki is misconfigured, XML dumps can contain a page with + // a non-zero namespace ID, but whose title doesn't contain a colon + // (T114115). In those cases, output a made-up namespace name to avoid + // collisions. The ImportTitleFactory might replace this with something + // more appropriate. + $namespaceName = $titleIncludesNamespace ? $pieces[0] : "Ns$ns"; } // 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' ) { + // namespace name, except in the case of namespace 0. + if ( $titleIncludesNamespace ) { $pageName = $pieces[1]; } else { $pageName = $title; diff --git a/tests/phpunit/includes/title/NamespaceAwareForeignTitleFactoryTest.php b/tests/phpunit/includes/title/NamespaceAwareForeignTitleFactoryTest.php index 76cedc60ea..520108aac1 100644 --- a/tests/phpunit/includes/title/NamespaceAwareForeignTitleFactoryTest.php +++ b/tests/phpunit/includes/title/NamespaceAwareForeignTitleFactoryTest.php @@ -36,10 +36,18 @@ class NamespaceAwareForeignTitleFactoryTest extends MediaWikiTestCase { 'MainNamespaceArticle', null, new ForeignTitle( 0, '', 'MainNamespaceArticle' ), ], + [ + 'Magic:_The_Gathering', 0, + new ForeignTitle( 0, '', 'Magic:_The_Gathering' ), + ], [ 'Talk:Nice_talk', 1, new ForeignTitle( 1, 'Talk', 'Nice_talk' ), ], + [ + 'Talk:Magic:_The_Gathering', 1, + new ForeignTitle( 1, 'Talk', 'Magic:_The_Gathering' ), + ], [ 'Bogus:Nice_talk', 0, new ForeignTitle( 0, '', 'Bogus:Nice_talk' ), @@ -56,6 +64,11 @@ class NamespaceAwareForeignTitleFactoryTest extends MediaWikiTestCase { 'Bogus:Nice_talk', 1, new ForeignTitle( 1, 'Talk', 'Nice_talk' ), ], + // Misconfigured wiki with unregistered namespace (T114115) + [ + 'Nice_talk', 1234, + new ForeignTitle( 1234, 'Ns1234', 'Nice_talk' ), + ], ]; }