Followup r100905, cleanup per CR and fixes the overriding of the main RequestContext
[lhc/web/wiklou.git] / includes / Export.php
index cc14e41..489a928 100644 (file)
@@ -354,10 +354,6 @@ class WikiExporter {
  * @ingroup Dump
  */
 class XmlDumpWriter {
-       var $firstPageWritten = 0;
-       var $lastPageWritten = 0;
-       var $pageInProgress = 0;
-
        /**
         * Returns the export schema version.
         * @return string
@@ -461,7 +457,6 @@ class XmlDumpWriter {
                $title = Title::makeTitle( $row->page_namespace, $row->page_title );
                $out .= '    ' . Xml::elementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
                $out .= '    ' . Xml::element( 'id', array(), strval( $row->page_id ) ) . "\n";
-               $this->pageInProgress = $row->page_id;
                if ( $row->page_is_redirect ) {
                        $out .= '    ' . Xml::element( 'redirect', array() ) . "\n";
                }
@@ -482,10 +477,6 @@ class XmlDumpWriter {
         */
        function closePage() {
                return "  </page>\n";
-               if (! $this->firstPageWritten) {
-                       $this->firstPageWritten = $this->pageInProgress;
-               }
-               $this->lastPageWritten = $this->pageInProgress;
        }
 
        /**
@@ -632,7 +623,7 @@ class XmlDumpWriter {
         */
        function writeUpload( $file, $dumpContents = false ) {
                if ( $file->isOld() ) {
-                       $archiveName = "      " . 
+                       $archiveName = "      " .
                                Xml::element( 'archivename', null, $file->getArchiveName() ) . "\n";
                } else {
                        $archiveName = '';
@@ -640,7 +631,7 @@ class XmlDumpWriter {
                if ( $dumpContents ) {
                        # Dump file as base64
                        # Uses only XML-safe characters, so does not need escaping
-                       $contents = '      <contents encoding="base64">' . 
+                       $contents = '      <contents encoding="base64">' .
                                chunk_split( base64_encode( file_get_contents( $file->getPath() ) ) ) .
                                "      </contents>\n";
                } else {
@@ -651,7 +642,7 @@ class XmlDumpWriter {
                        $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
                        "      " . Xml::elementClean( 'comment', null, $file->getDescription() ) . "\n" .
                        "      " . Xml::element( 'filename', null, $file->getName() ) . "\n" .
-                       $archiveName . 
+                       $archiveName .
                        "      " . Xml::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
                        "      " . Xml::element( 'size', null, $file->getSize() ) . "\n" .
                        "      " . Xml::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
@@ -700,15 +691,33 @@ class DumpOutput {
                print $string;
        }
 
+       /**
+        * Close the old file, move it to a specified name,
+        * and reopen new file with the old name. Use this
+        * for writing out a file in multiple pieces
+        * at specified checkpoints (e.g. every n hours).
+        * @param $newname mixed File name. May be a string or an array with one element
+        */
        function closeRenameAndReopen( $newname ) {
                return;
        }
 
-       function rename( $newname ) {
+       /**
+        * Close the old file, and move it to a specified name.
+        * Use this for the last piece of a file written out
+        * at specified checkpoints (e.g. every n hours).
+        * @param $newname mixed File name. May be a string or an array with one element
+        * @param $open bool If true, a new file with the old filename will be opened again for writing (default: false)
+        */
+       function closeAndRename( $newname, $open = false ) {
                return;
        }
 
-       function getFilename() {
+       /**
+        * Returns the name of the file or files which are
+        * being written to, if there are any.
+        */
+       function getFilenames() {
                return NULL;
        }
 }
@@ -718,8 +727,7 @@ class DumpOutput {
  * @ingroup Dump
  */
 class DumpFileOutput extends DumpOutput {
-       var $handle;
-       var $filename;
+       protected $handle, $filename;
 
        function __construct( $file ) {
                $this->handle = fopen( $file, "wt" );
@@ -730,43 +738,30 @@ class DumpFileOutput extends DumpOutput {
                fputs( $this->handle, $string );
        }
 
-       /**
-        * Close the old file, move it to a specified name, 
-        * and reopen new file with the old name. Use this
-        * for writing out a file in multiple pieces
-        * at specified checkpoints (e.g. every n hours).
-        */
        function closeRenameAndReopen( $newname ) {
-               if ( is_array($newname) ) {
-                       if (count($newname) > 1) {
-                               WfDie("Export closeRenameAndReopen: passed multiple argumnts for rename of single file\n");
-                       }
-                       else {
+               $this->closeAndRename( $newname, true );
+       }
+
+       function closeAndRename( $newname, $open = false ) {
+               if ( is_array( $newname ) ) {
+                       if ( count( $newname ) > 1 ) {
+                               throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
+                       } else {
                                $newname = $newname[0];
                        }
                }
                if ( $newname ) {
                        fclose( $this->handle );
-                       rename( $this->filename, $newname );
-                       $this->handle = fopen( $this->filename, "wt" );
-               }
-       }
-
-       function rename( $newname ) {
-               if ( is_array($newname) ) {
-                       if (count($newname) > 1) {
-                               WfDie("Export closeRenameAndReopen: passed multiple argumnts for rename of single file\n");
+                       if (! rename( $this->filename, $newname ) ) {
+                               throw new MWException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
                        }
-                       else {
-                               $newname = $newname[0];
+                       elseif ( $open ) {
+                               $this->handle = fopen( $this->filename, "wt" );
                        }
                }
-               if ( $newname ) {
-                       rename( $this->filename, $newname );
-               }
        }
 
-       function getFilename() {
+       function getFilenames() {
                return $this->filename;
        }
 }
@@ -778,52 +773,53 @@ class DumpFileOutput extends DumpOutput {
  * @ingroup Dump
  */
 class DumpPipeOutput extends DumpFileOutput {
-       var $command;
+       protected $command, $filename;
 
        function __construct( $command, $file = null ) {
                if ( !is_null( $file ) ) {
                        $command .=  " > " . wfEscapeShellArg( $file );
                }
-               $this->handle = popen( $command, "w" );
+
+               $this->startCommand( $command );
                $this->command = $command;
                $this->filename = $file;
        }
 
-       /**
-        * Close the old file, move it to a specified name, 
-        * and reopen new file with the old name. 
-        */
+       function startCommand( $command ) {
+               $spec = array(
+                       0 => array( "pipe", "r" ),
+               );
+               $pipes = array();
+               $this->procOpenResource = proc_open( $command, $spec, $pipes );
+               $this->handle = $pipes[0];
+       }
+
        function closeRenameAndReopen( $newname ) {
-               if ( is_array($newname) ) {
-                       if (count($newname) > 1) {
-                               WfDie("Export closeRenameAndReopen: passed multiple argumnts for rename of single file\n");
-                       }
-                       else {
-                               $newname = $newname[0];
-                       }
-               }
-               if ( $newname ) {
-                       pclose( $this->handle );
-                       rename( $this->filename, $newname );
-                       $command = $this->command;
-                       $command .=  " > " . wfEscapeShellArg( $this->filename );
-                       $this->handle = popen( $command, "w" );
-               }
+               $this->closeAndRename( $newname, true );
        }
 
-       function rename( $newname ) {
-               if ( is_array($newname) ) {
-                       if (count($newname) > 1) {
-                               WfDie("Export closeRenameAndReopen: passed multiple argumnts for rename of single file\n");
-                       }
-                       else {
+       function closeAndRename( $newname, $open = false ) {
+               if ( is_array( $newname ) ) {
+                       if ( count( $newname ) > 1 ) {
+                               throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
+                       } else {
                                $newname = $newname[0];
                        }
                }
                if ( $newname ) {
-                       rename( $this->filename, $newname );
+                       fclose( $this->handle );
+                       proc_close( $this->procOpenResource );
+                       if (! rename( $this->filename, $newname ) ) {
+                               throw new MWException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
+                       }
+                       elseif ( $open ) {
+                               $command = $this->command;
+                               $command .=  " > " . wfEscapeShellArg( $this->filename );
+                               $this->startCommand( $command );
+                       }
                }
        }
+
 }
 
 /**
@@ -851,46 +847,39 @@ class DumpBZip2Output extends DumpPipeOutput {
  * @ingroup Dump
  */
 class Dump7ZipOutput extends DumpPipeOutput {
-       var $filename;
+       protected $filename;
 
        function __construct( $file ) {
-               $command = "7za a -bd -si " . wfEscapeShellArg( $file );
-               // Suppress annoying useless crap from p7zip
-               // Unfortunately this could suppress real error messages too
-               $command .= ' >' . wfGetNull() . ' 2>&1';
+               $command = setup7zCommand( $file );
                parent::__construct( $command );
                $this->filename = $file;
        }
 
        function closeRenameAndReopen( $newname ) {
-               if ( is_array($newname) ) {
-                       if (count($newname) > 1) {
-                               WfDie("Export closeRenameAndReopen: passed multiple argumnts for rename of single file\n");
-                       }
-                       else {
-                               $newname = $newname[0];
-                       }
-               }
-               if ( $newname ) {
-                       pclose( $this->handle );
-                       rename( $this->filename, $newname );
-                       $command = "7za a -bd -si " . wfEscapeShellArg( $file );
-                       $command .= ' >' . wfGetNull() . ' 2>&1';
-                       $this->handle = popen( $command, "w" );
-               }
+               $this->closeAndRename( $newname, true );
        }
 
-       function rename( $newname ) {
-               if ( is_array($newname) ) {
-                       if (count($newname) > 1) {
-                               WfDie("Export closeRenameAndReopen: passed multiple argumnts for rename of single file\n");
-                       }
-                       else {
+       function closeAndRename( $newname, $open = false ) {
+               if ( is_array( $newname ) ) {
+                       if ( count( $newname ) > 1 ) {
+                               throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
+                       } else {
                                $newname = $newname[0];
                        }
                }
                if ( $newname ) {
-                       rename( $this->filename, $newname );
+                       fclose( $this->handle );
+                       proc_close( $this->procOpenResource );
+                       if (! rename( $this->filename, $newname ) ) {
+                               throw new MWException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
+                       }
+                       elseif ( $open ) {
+                               $command = "7za a -bd -si " . wfEscapeShellArg( $file );
+                               // Suppress annoying useless crap from p7zip
+                               // Unfortunately this could suppress real error messages too
+                               $command .= ' >' . wfGetNull() . ' 2>&1';
+                               $this->startCommand( $command );
+                       }
                }
        }
 }
@@ -944,12 +933,12 @@ class DumpFilter {
                $this->sink->closeRenameAndReopen( $newname );
        }
 
-       function rename( $newname ) {
-               $this->sink->rename( $newname );
+       function closeAndRename( $newname, $open = false ) {
+               $this->sink->closeAndRename( $newname, $open );
        }
 
-       function getFilename() {
-               return $this->sink->getFilename();
+       function getFilenames() {
+               return $this->sink->getFilenames();
        }
 
        /**
@@ -1101,21 +1090,19 @@ class DumpMultiWriter {
        }
 
        function closeRenameAndReopen( $newnames ) {
-               for( $i = 0; $i < $this->count; $i++ ) {
-                       $this->sinks[$i]->closeRenameAndReopen( $newnames[$i] );
-               }
+               $this->closeAndRename( $newnames, true );
        }
 
-       function rename( $newnames ) {
-               for( $i = 0; $i < $this->count; $i++ ) {
-                       $this->sinks[$i]->rename( $newnames[$i] );
+       function closeAndRename( $newnames, $open = false ) {
+               for ( $i = 0; $i < $this->count; $i++ ) {
+                       $this->sinks[$i]->closeAndRename( $newnames[$i], $open );
                }
        }
 
-       function getFilename() {
+       function getFilenames() {
                $filenames = array();
-               for( $i = 0; $i < $this->count; $i++ ) {
-                       $filenames[] =  $this->sinks[$i]->getFilename();
+               for ( $i = 0; $i < $this->count; $i++ ) {
+                       $filenames[] =  $this->sinks[$i]->getFilenames();
                }
                return $filenames;
        }