Drop "left in" debugging var_dump from WikiImporter
[lhc/web/wiklou.git] / includes / Import.php
index 1a3e05a..3880e25 100644 (file)
@@ -40,12 +40,12 @@ class WikiImporter {
 
        /**
         * Creates an ImportXMLReader drawing from the source provided
-        * @param string $source
+        * @param ImportStreamSource $source
         */
-       function __construct( $source ) {
+       function __construct( ImportStreamSource $source ) {
                $this->reader = new XMLReader();
 
-               if ( !in_array(  'uploadsource', stream_get_wrappers() ) ) {
+               if ( !in_array( 'uploadsource', stream_get_wrappers() ) ) {
                        stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
                }
                $id = UploadSourceAdapter::registerSource( $source );
@@ -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
@@ -420,53 +429,12 @@ class WikiImporter {
                return '';
        }
 
-       # --------------
-
-       /** Left in for debugging */
-       private function dumpElement() {
-               static $lookup = null;
-               if ( !$lookup ) {
-                       $xmlReaderConstants = array(
-                               "NONE",
-                               "ELEMENT",
-                               "ATTRIBUTE",
-                               "TEXT",
-                               "CDATA",
-                               "ENTITY_REF",
-                               "ENTITY",
-                               "PI",
-                               "COMMENT",
-                               "DOC",
-                               "DOC_TYPE",
-                               "DOC_FRAGMENT",
-                               "NOTATION",
-                               "WHITESPACE",
-                               "SIGNIFICANT_WHITESPACE",
-                               "END_ELEMENT",
-                               "END_ENTITY",
-                               "XML_DECLARATION",
-                       );
-                       $lookup = array();
-
-                       foreach ( $xmlReaderConstants as $name ) {
-                               $lookup[constant( "XmlReader::$name" )] = $name;
-                       }
-               }
-
-               print var_dump(
-                       $lookup[$this->reader->nodeType],
-                       $this->reader->name,
-                       $this->reader->value
-               ) . "\n\n";
-       }
-
        /**
         * Primary entry point
         * @throws MWException
         * @return bool
         */
        public function doImport() {
-
                // Calls to reader->read need to be wrapped in calls to
                // libxml_disable_entity_loader() to avoid local file
                // inclusion attacks (bug 46932).
@@ -524,7 +492,7 @@ class WikiImporter {
        private function handleSiteInfo() {
                // Site info is useful, but not actually used for dump imports.
                // Includes a quick short-circuit to save performance.
-               if ( ! $this->mSiteInfoCallback ) {
+               if ( !$this->mSiteInfoCallback ) {
                        $this->reader->next();
                        return true;
                }
@@ -618,17 +586,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:
+                               // <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 );
@@ -924,10 +903,10 @@ class UploadSourceAdapter {
        private $mPosition;
 
        /**
-        * @param string $source
+        * @param ImportStreamSource $source
         * @return string
         */
-       static function registerSource( $source ) {
+       static function registerSource( ImportStreamSource $source ) {
                $id = wfRandomString();
 
                self::$sourceRegistrations[$id] = $source;
@@ -1030,30 +1009,6 @@ class UploadSourceAdapter {
        }
 }
 
-class XMLReader2 extends XMLReader {
-
-       /**
-        * @return bool|string
-        */
-       function nodeContents() {
-               if ( $this->isEmptyElement ) {
-                       return "";
-               }
-               $buffer = "";
-               while ( $this->read() ) {
-                       switch ( $this->nodeType ) {
-                       case XmlReader::TEXT:
-                       case XmlReader::SIGNIFICANT_WHITESPACE:
-                               $buffer .= $this->value;
-                               break;
-                       case XmlReader::END_ELEMENT:
-                               return $buffer;
-                       }
-               }
-               return $this->close();
-       }
-}
-
 /**
  * @todo document (e.g. one-sentence class description).
  * @ingroup SpecialPage
@@ -1483,6 +1438,7 @@ class WikiRevision {
                $linkCache->clear();
 
                $page = WikiPage::factory( $this->title );
+               $page->loadPageData( 'fromdbmaster' );
                if ( !$page->exists() ) {
                        # must create the page...
                        $pageId = $page->insertOn( $dbw );