saper has uploaded a new change for review.
https://gerrit.wikimedia.org/r/173223
Change subject: Use ImportStringSource for simple import sources
......................................................................
Use ImportStringSource for simple import sources
Bug: 73283
Change-Id: I61c94202467ae6729ae52684a6477f7cbd8e00b7
---
M includes/Import.php
M tests/phpunit/includes/ImportTest.php
2 files changed, 31 insertions(+), 18 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/23/173223/1
diff --git a/includes/Import.php b/includes/Import.php
index 4eb8e97..9c33496 100644
--- a/includes/Import.php
+++ b/includes/Import.php
@@ -42,10 +42,10 @@
/**
* Creates an ImportXMLReader drawing from the source provided
- * @param ImportStreamSource $source
+ * @param WikiImportSource $source
* @param Config $config
*/
- function __construct( ImportStreamSource $source, Config $config = null
) {
+ function __construct( WikiImportSource $source, Config $config = null )
{
$this->reader = new XMLReader();
if ( !$config ) {
wfDeprecated( __METHOD__ . ' without a Config
instance', '1.25' );
@@ -62,6 +62,7 @@
} else {
$this->reader->open( "uploadsource://$id" );
}
+ wfDebugLog( "wikiimporter", "Source $id registered" );
// Default callbacks
$this->setRevisionCallback( array( $this, "importRevision" ) );
@@ -913,7 +914,7 @@
* @param ImportStreamSource $source
* @return string
*/
- static function registerSource( ImportStreamSource $source ) {
+ static function registerSource( WikiImportSource $source ) {
$id = wfRandomString();
self::$sourceRegistrations[$id] = $source;
@@ -1652,13 +1653,31 @@
}
/**
+ * Generic interface for various import sources
+ */
+interface WikiImportSource {
+ /**
+ * Is import source fully consumed?
+ * @return bool
+ */
+ function atEnd();
+ /**
+ * Get next chunk of data or false if already consumed
+ * @return bool|string
+ */
+ function readChunk();
+}
+
+
+/**
* @todo document (e.g. one-sentence class description).
* @ingroup SpecialPage
*/
-class ImportStringSource {
+class ImportStringSource implements WikiImportSource {
function __construct( $string ) {
$this->mString = $string;
$this->mRead = false;
+ wfDebugLog( "wikiimporter", "ImportStringSource created" );
}
/**
@@ -1673,6 +1692,7 @@
*/
function readChunk() {
if ( $this->atEnd() ) {
+ wfDebugLog( "wikiimporter", "attempt to read past
string data" );
return false;
}
$this->mRead = true;
@@ -1684,7 +1704,7 @@
* @todo document (e.g. one-sentence class description).
* @ingroup SpecialPage
*/
-class ImportStreamSource {
+class ImportStreamSource implements WikiImportSource {
function __construct( $handle ) {
$this->mHandle = $handle;
}
@@ -1767,11 +1787,7 @@
# as the Wikimedia cluster, etc.
$data = Http::request( $method, $url, array( 'followRedirects'
=> true ) );
if ( $data !== false ) {
- $file = tmpfile();
- fwrite( $file, $data );
- fflush( $file );
- fseek( $file, 0 );
- return Status::newGood( new ImportStreamSource( $file )
);
+ return Status::newGood( new ImportStringSource( $data )
);
} else {
return Status::newFatal( 'importcantopen' );
}
diff --git a/tests/phpunit/includes/ImportTest.php
b/tests/phpunit/includes/ImportTest.php
index 678c89b..4a3015b 100644
--- a/tests/phpunit/includes/ImportTest.php
+++ b/tests/phpunit/includes/ImportTest.php
@@ -9,13 +9,8 @@
*/
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;
+ private function getDataSource( $xml ) {
+ return new ImportStringSource( $xml );
}
/**
@@ -25,7 +20,8 @@
* @param string|null $redirectTitle
*/
public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
- $source = $this->getInputStreamSource( $xml );
+ $source = $this->getDataSource( $xml );
+ wfDebugLog( "wikiimportertest", "Testing for <" . var_export(
$redirectTitle, true) . ">" );
$redirect = null;
$callback = function ( $title, $origTitle, $revCount,
$sRevCount, $pageInfo ) use ( &$redirect ) {
@@ -37,6 +33,7 @@
$importer = new WikiImporter( $source,
ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
$importer->setPageOutCallback( $callback );
$importer->doImport();
+ wfDebugLog( "wikiimportertest", "Redirect value received is <"
. var_export( $redirect, true ) . ">" );
$this->assertEquals( $redirectTitle, $redirect );
}
--
To view, visit https://gerrit.wikimedia.org/r/173223
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I61c94202467ae6729ae52684a6477f7cbd8e00b7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: saper <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits