(bug 32827) "[Regression] Block log for IP ranges not shown on Special:Block"
[lhc/web/wiklou.git] / includes / Export.php
index 3dcd3ba..35a1b5b 100644 (file)
@@ -56,7 +56,7 @@ class WikiExporter {
         * make additional queries to pull source data while the
         * main query is still running.
         *
-        * @param $db Database
+        * @param $db DatabaseBase
         * @param $history Mixed: one of WikiExporter::FULL, WikiExporter::CURRENT,
         *                 WikiExporter::RANGE or WikiExporter::STABLE,
         *                 or an associative array:
@@ -380,7 +380,7 @@ class XmlDumpWriter {
         * @return string
         */
        function schemaVersion() {
-               return "0.5";
+               return "0.6";
        }
 
        /**
@@ -476,11 +476,23 @@ class XmlDumpWriter {
        function openPage( $row ) {
                $out = "  <page>\n";
                $title = Title::makeTitle( $row->page_namespace, $row->page_title );
-               $out .= '    ' . Xml::elementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
+               $out .= '    ' . Xml::elementClean( 'title', array(), self::canonicalTitle( $title ) ) . "\n";
+               $out .= '    ' . Xml::element( 'ns', array(), strval( $row->page_namespace) ) . "\n";
                $out .= '    ' . Xml::element( 'id', array(), strval( $row->page_id ) ) . "\n";
                if ( $row->page_is_redirect ) {
-                       $out .= '    ' . Xml::element( 'redirect', array() ) . "\n";
+                       $page = WikiPage::factory( $title );
+                       $redirect = $page->getRedirectTarget();
+                       if ( $redirect instanceOf Title && $redirect->isValidRedirectTarget() ) {
+                               $out .= '    ' . Xml::element( 'redirect', array( 'title' => self::canonicalTitle( $redirect ) ) ) . "\n";
+                       }
+               }
+
+               if ( $row->rev_sha1 ) {
+                       $out .= "      " . Xml::element('sha1', null, strval( $row->rev_sha1 ) ) . "\n";
+               } else {
+                       $out .= "      <sha1/>\n";
                }
+
                if ( $row->page_restrictions != '' ) {
                        $out .= '    ' . Xml::element( 'restrictions', array(),
                                strval( $row->page_restrictions ) ) . "\n";
@@ -495,6 +507,7 @@ class XmlDumpWriter {
         * Closes a <page> section on the output stream.
         *
         * @access private
+        * @return string
         */
        function closePage() {
                return "  </page>\n";
@@ -538,12 +551,12 @@ class XmlDumpWriter {
                        // Raw text from the database may have invalid chars
                        $text = strval( Revision::getRevisionText( $row ) );
                        $out .= "      " . Xml::elementClean( 'text',
-                               array( 'xml:space' => 'preserve', 'bytes' => $row->rev_len ),
+                               array( 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len ) ),
                                strval( $text ) ) . "\n";
                } else {
                        // Stub output
                        $out .= "      " . Xml::element( 'text',
-                               array( 'id' => $row->rev_text_id, 'bytes' => $row->rev_len ),
+                               array( 'id' => $row->rev_text_id, 'bytes' => intval( $row->rev_len ) ),
                                "" ) . "\n";
                }
 
@@ -590,7 +603,7 @@ class XmlDumpWriter {
                        $out .= "      " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
                } else {
                        $title = Title::makeTitle( $row->log_namespace, $row->log_title );
-                       $out .= "      " . Xml::elementClean( 'logtitle', null, $title->getPrefixedText() ) . "\n";
+                       $out .= "      " . Xml::elementClean( 'logtitle', null, self::canonicalTitle( $title ) ) . "\n";
                        $out .= "      " . Xml::elementClean( 'params',
                                array( 'xml:space' => 'preserve' ),
                                strval( $row->log_params ) ) . "\n";
@@ -609,7 +622,7 @@ class XmlDumpWriter {
 
        function writeContributor( $id, $text ) {
                $out = "      <contributor>\n";
-               if ( $id ) {
+               if ( $id || !IP::isValid( $text ) ) {
                        $out .= "        " . Xml::elementClean( 'username', null, strval( $text ) ) . "\n";
                        $out .= "        " . Xml::element( 'id', null, strval( $id ) ) . "\n";
                } else {
@@ -621,6 +634,7 @@ class XmlDumpWriter {
 
        /**
         * Warning! This data is potentially inconsistent. :(
+        * @return string
         */
        function writeUploads( $row, $dumpContents = false ) {
                if ( $row->page_namespace == NS_IMAGE ) {
@@ -672,6 +686,30 @@ class XmlDumpWriter {
                        "    </upload>\n";
        }
 
+       /**
+        * Return prefixed text form of title, but using the content language's
+        * canonical namespace. This skips any special-casing such as gendered
+        * user namespaces -- which while useful, are not yet listed in the
+        * XML <siteinfo> data so are unsafe in export.
+        *
+        * @param Title $title
+        * @return string
+        * @since 1.18
+        */
+       public static function canonicalTitle( Title $title ) {
+               if ( $title->getInterwiki() ) {
+                       return $title->getPrefixedText();
+               }
+
+               global $wgContLang;
+               $prefix = str_replace( '_', ' ', $wgContLang->getNsText( $title->getNamespace() ) );
+
+               if ( $prefix !== '' ) {
+                       $prefix .= ':';
+               }
+
+               return $prefix . $title->getText();
+       }
 }
 
 
@@ -737,6 +775,7 @@ class DumpOutput {
        /**
         * Returns the name of the file or files which are
         * being written to, if there are any.
+        * @return null
         */
        function getFilenames() {
                return NULL;
@@ -763,7 +802,13 @@ class DumpFileOutput extends DumpOutput {
                $this->closeAndRename( $newname, true );
        }
 
-       function closeAndRename( $newname, $open = false ) {
+       function renameOrException( $newname ) {
+                       if (! rename( $this->filename, $newname ) ) {
+                               throw new MWException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
+                       }
+       }
+
+       function checkRenameArgCount( $newname ) {
                if ( is_array( $newname ) ) {
                        if ( count( $newname ) > 1 ) {
                                throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
@@ -771,12 +816,15 @@ class DumpFileOutput extends DumpOutput {
                                $newname = $newname[0];
                        }
                }
+               return $newname;
+       }
+
+       function closeAndRename( $newname, $open = false ) {
+               $newname = $this->checkRenameArgCount( $newname );
                if ( $newname ) {
                        fclose( $this->handle );
-                       if (! rename( $this->filename, $newname ) ) {
-                               throw new MWException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
-                       }
-                       elseif ( $open ) {
+                       $this->renameOrException( $newname );
+                       if ( $open ) {
                                $this->handle = fopen( $this->filename, "wt" );
                        }
                }
@@ -820,20 +868,12 @@ class DumpPipeOutput extends DumpFileOutput {
        }
 
        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];
-                       }
-               }
+               $newname = $this->checkRenameArgCount( $newname );
                if ( $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 ) {
+                       $this->renameOrException( $newname );
+                       if ( $open ) {
                                $command = $this->command;
                                $command .=  " > " . wfEscapeShellArg( $this->filename );
                                $this->startCommand( $command );
@@ -868,37 +908,28 @@ class DumpBZip2Output extends DumpPipeOutput {
  * @ingroup Dump
  */
 class Dump7ZipOutput extends DumpPipeOutput {
-       protected $filename;
-
        function __construct( $file ) {
-               $command = setup7zCommand( $file );
+               $command = $this->setup7zCommand( $file );
                parent::__construct( $command );
                $this->filename = $file;
        }
 
-       function closeRenameAndReopen( $newname ) {
-               $this->closeAndRename( $newname, true );
+       function setup7zCommand( $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';
+               return( $command );
        }
 
        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];
-                       }
-               }
+               $newname = $this->checkRenameArgCount( $newname );
                if ( $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->renameOrException( $newname );
+                       if ( $open ) {
+                               $command = $this->setup7zCommand( $this->filename );
                                $this->startCommand( $command );
                        }
                }