saper has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/173222

Change subject: Use ImportStringSource for simple import sources
......................................................................

Use ImportStringSource for simple import sources

Bug: 73283
Change-Id: I9e43d7ea0c1a4553d71bac29ca18285f3f028421
---
M includes/Import.php
M tests/phpunit/includes/ImportTest.php
2 files changed, 28 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/22/173222/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 fc7a1ae..4a3015b 100644
--- a/tests/phpunit/includes/ImportTest.php
+++ b/tests/phpunit/includes/ImportTest.php
@@ -21,6 +21,7 @@
         */
        public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
                $source = $this->getDataSource( $xml );
+               wfDebugLog( "wikiimportertest", "Testing for <" . var_export( 
$redirectTitle, true) . ">" );
 
                $redirect = null;
                $callback = function ( $title, $origTitle, $revCount, 
$sRevCount, $pageInfo ) use ( &$redirect ) {
@@ -32,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/173222
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9e43d7ea0c1a4553d71bac29ca18285f3f028421
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

Reply via email to