jenkins-bot has submitted this change and it was merged.
Change subject: Correctly parse 'redirect' XML tag during Special:Import.
......................................................................
Correctly parse 'redirect' XML tag during Special:Import.
Bug: 65481
Change-Id: Id9b3b7878b2e7b6fc7a06b163e5bac60e700490e
---
M includes/Import.php
A tests/phpunit/includes/ImportTest.php
2 files changed, 129 insertions(+), 9 deletions(-)
Approvals:
Daniel Kinzler: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/Import.php b/includes/Import.php
index 4940c19..0f88082 100644
--- a/includes/Import.php
+++ b/includes/Import.php
@@ -394,6 +394,15 @@
}
/**
+ * 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
* no sub-elements or such scary things.
@@ -618,17 +627,28 @@
&$pageInfo ) ) ) {
// Do nothing
} elseif ( in_array( $tag, $normalFields ) ) {
- $pageInfo[$tag] = $this->nodeContents();
- if ( $tag == 'title' ) {
- $title = $this->processTitle(
$pageInfo['title'] );
+ // An XML snippet:
+ // <page>
+ // <id>123</id>
+ // <title>Page</title>
+ // <redirect title="NewTitle"/>
+ // ...
+ // 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 0000000..8895403
--- /dev/null
+++ b/tests/phpunit/includes/ImportTest.php
@@ -0,0 +1,100 @@
+<?php
+
+/**
+ * Test class for Import methods.
+ *
+ * @group Database
+ *
+ * @author Sebastian Brückner < [email protected]
>
+ */
+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
+<mediawiki>
+ <page>
+ <title>Test</title>
+ <ns>0</ns>
+ <id>21</id>
+ <redirect title="Test22"/>
+ <revision>
+ <id>20</id>
+ <timestamp>2014-05-27T10:00:00Z</timestamp>
+ <contributor>
+ <username>Admin</username>
+ <id>10</id>
+ </contributor>
+ <comment>Admin moved page [[Test]] to
[[Test22]]</comment>
+ <text xml:space="preserve" bytes="20">#REDIRECT
[[Test22]]</text>
+ <sha1>tq456o9x3abm7r9ozi6km8yrbbc56o6</sha1>
+ <model>wikitext</model>
+ <format>text/x-wiki</format>
+ </revision>
+ </page>
+</mediawiki>
+EOF
+ ,
+ 'Test22'
+ ),
+ array(
+ <<< EOF
+<mediawiki>
+ <page>
+ <title>Test</title>
+ <ns>0</ns>
+ <id>42</id>
+ <revision>
+ <id>421</id>
+ <timestamp>2014-05-27T11:00:00Z</timestamp>
+ <contributor>
+ <username>Admin</username>
+ <id>10</id>
+ </contributor>
+ <text xml:space="preserve" bytes="4">Abcd</text>
+ <sha1>n7uomjq96szt60fy5w3x7ahf7q8m8rh</sha1>
+ <model>wikitext</model>
+ <format>text/x-wiki</format>
+ </revision>
+ </page>
+</mediawiki>
+EOF
+ ,
+ NULL
+ ),
+ );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/134079
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Id9b3b7878b2e7b6fc7a06b163e5bac60e700490e
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Nik <[email protected]>
Gerrit-Reviewer: Alexander.lehmann
<[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Manybubbles <[email protected]>
Gerrit-Reviewer: Nik <[email protected]>
Gerrit-Reviewer: TTO <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits