From: Sebastian Brückner Date: Mon, 19 May 2014 12:23:32 +0000 (+0200) Subject: Correctly parse 'redirect' XML tag during Special:Import. X-Git-Tag: 1.31.0-rc.0~15552^2 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=85b695c2ee1d9479ee94bdd4de85a82e02789b62;p=lhc%2Fweb%2Fwiklou.git Correctly parse 'redirect' XML tag during Special:Import. Bug: 65481 Change-Id: Id9b3b7878b2e7b6fc7a06b163e5bac60e700490e --- diff --git a/includes/Import.php b/includes/Import.php index 4940c19e7a..0f88082146 100644 --- a/includes/Import.php +++ b/includes/Import.php @@ -393,6 +393,15 @@ class WikiImporter { } } + /** + * Retrieves the contents of the named attribute of the current element. + * @param string $attr the name of the attribute + * @return string the value of the attribute or an empty string if it is not set in the current element. + */ + public function nodeAttribute( $attr ) { + return $this->reader->getAttribute( $attr ); + } + /** * Shouldn't something like this be built-in to XMLReader? * Fetches text contents of the current element, assuming @@ -618,17 +627,28 @@ class WikiImporter { &$pageInfo ) ) ) { // Do nothing } elseif ( in_array( $tag, $normalFields ) ) { - $pageInfo[$tag] = $this->nodeContents(); - if ( $tag == 'title' ) { - $title = $this->processTitle( $pageInfo['title'] ); + // An XML snippet: + // + // 123 + // Page + // + // ... + // Because the redirect tag is built differently, we need special handling for that case. + if ( $tag == 'redirect' ) { + $pageInfo[$tag] = $this->nodeAttribute( 'title' ); + } else { + $pageInfo[$tag] = $this->nodeContents(); + if ( $tag == 'title' ) { + $title = $this->processTitle( $pageInfo['title'] ); - if ( !$title ) { - $badTitle = true; - $skip = true; - } + if ( !$title ) { + $badTitle = true; + $skip = true; + } - $this->pageCallback( $title ); - list( $pageInfo['_title'], $origTitle ) = $title; + $this->pageCallback( $title ); + list( $pageInfo['_title'], $origTitle ) = $title; + } } } elseif ( $tag == 'revision' ) { $this->handleRevision( $pageInfo ); diff --git a/tests/phpunit/includes/ImportTest.php b/tests/phpunit/includes/ImportTest.php new file mode 100644 index 0000000000..889540381a --- /dev/null +++ b/tests/phpunit/includes/ImportTest.php @@ -0,0 +1,100 @@ + + */ +class ImportTest extends MediaWikiLangTestCase { + + private function getInputStreamSource( $xml ) { + $file = 'data:application/xml,' . $xml; + $status = ImportStreamSource::newFromFile( $file ); + if ( !$status->isGood() ) { + throw new MWException( "Cannot create InputStreamSource." ); + } + return $status->value; + } + + /** + * @covers WikiImporter::handlePage + * @dataProvider getRedirectXML + * @param string $xml + */ + public function testHandlePageContainsRedirect( $xml, $redirectTitle ) { + $source = $this->getInputStreamSource( $xml ); + + $redirect = NULL; + $callback = function( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) use ( &$redirect ) { + if ( array_key_exists( 'redirect', $pageInfo ) ) { + $redirect = $pageInfo['redirect']; + } + }; + + $importer = new WikiImporter( $source ); + $importer->setPageOutCallback( $callback ); + $importer->doImport(); + + $this->assertEquals( $redirectTitle, $redirect ); + } + + public function getRedirectXML() { + return array( + array( + <<< EOF + + + Test + 0 + 21 + + + 20 + 2014-05-27T10:00:00Z + + Admin + 10 + + Admin moved page [[Test]] to [[Test22]] + #REDIRECT [[Test22]] + tq456o9x3abm7r9ozi6km8yrbbc56o6 + wikitext + text/x-wiki + + + +EOF + , + 'Test22' + ), + array( + <<< EOF + + + Test + 0 + 42 + + 421 + 2014-05-27T11:00:00Z + + Admin + 10 + + Abcd + n7uomjq96szt60fy5w3x7ahf7q8m8rh + wikitext + text/x-wiki + + + +EOF + , + NULL + ), + ); + } + +}