Remove unused XMLReader2 class
[lhc/web/wiklou.git] / includes / Import.php
index 743037c..2eb5667 100644 (file)
@@ -40,12 +40,14 @@ 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();
 
-               stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
+               if ( !in_array( 'uploadsource', stream_get_wrappers() ) ) {
+                       stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
+               }
                $id = UploadSourceAdapter::registerSource( $source );
                if ( defined( 'LIBXML_PARSEHUGE' ) ) {
                        $this->reader->open( "uploadsource://$id", null, LIBXML_PARSEHUGE );
@@ -263,7 +265,7 @@ class WikiImporter {
         * @return bool
         */
        public function importRevision( $revision ) {
-               if ( !$revision->getContent()->getContentHandler()->canBeUsedOn( $revision->getTitle() ) ) {
+               if ( !$revision->getContentHandler()->canBeUsedOn( $revision->getTitle() ) ) {
                        $this->notice( 'import-error-bad-location',
                                $revision->getTitle()->getPrefixedText(),
                                $revision->getID(),
@@ -391,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
@@ -522,7 +533,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;
                }
@@ -616,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:
+                               // <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 );
@@ -694,9 +716,6 @@ class WikiImporter {
                if ( isset( $revisionInfo['id'] ) ) {
                        $revision->setID( $revisionInfo['id'] );
                }
-               if ( isset( $revisionInfo['text'] ) ) {
-                       $revision->setText( $revisionInfo['text'] );
-               }
                if ( isset( $revisionInfo['model'] ) ) {
                        $revision->setModel( $revisionInfo['model'] );
                }
@@ -705,6 +724,14 @@ class WikiImporter {
                }
                $revision->setTitle( $pageInfo['_title'] );
 
+               if ( isset( $revisionInfo['text'] ) ) {
+                       $handler = $revision->getContentHandler();
+                       $text = $handler->importTransform(
+                               $revisionInfo['text'],
+                               $revision->getFormat() );
+
+                       $revision->setText( $text );
+               }
                if ( isset( $revisionInfo['timestamp'] ) ) {
                        $revision->setTimestamp( $revisionInfo['timestamp'] );
                } else {
@@ -917,10 +944,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;
@@ -1023,30 +1050,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
@@ -1087,6 +1090,9 @@ class WikiRevision {
        /** @var Content */
        protected $content = null;
 
+       /** @var ContentHandler */
+       protected $contentHandler = null;
+
        /** @var string */
        public $comment = "";
 
@@ -1318,18 +1324,24 @@ class WikiRevision {
                return $this->text;
        }
 
+       /**
+        * @return ContentHandler
+        */
+       function getContentHandler() {
+               if ( is_null( $this->contentHandler ) ) {
+                       $this->contentHandler = ContentHandler::getForModelID( $this->getModel() );
+               }
+
+               return $this->contentHandler;
+       }
+
        /**
         * @return Content
         */
        function getContent() {
                if ( is_null( $this->content ) ) {
-                       $this->content =
-                               ContentHandler::makeContent(
-                                       $this->text,
-                                       $this->getTitle(),
-                                       $this->getModel(),
-                                       $this->getFormat()
-                               );
+                       $handler = $this->getContentHandler();
+                       $this->content = $handler->unserializeContent( $this->text, $this->getFormat() );
                }
 
                return $this->content;
@@ -1350,8 +1362,8 @@ class WikiRevision {
         * @return string
         */
        function getFormat() {
-               if ( is_null( $this->model ) ) {
-                       $this->format = ContentHandler::getForTitle( $this->getTitle() )->getDefaultFormat();
+               if ( is_null( $this->format ) ) {
+                       $this->format = $this->getContentHandler()->getDefaultFormat();
                }
 
                return $this->format;