Merge "Remove extra unneeded whitespace"
[lhc/web/wiklou.git] / includes / Export.php
index 0d1aeef..c201c97 100644 (file)
@@ -49,6 +49,15 @@ class WikiExporter {
        const TEXT = 0;
        const STUB = 1;
 
+       var $buffer;
+
+       var $text;
+
+       /**
+        * @var DumpOutput
+        */
+       var $sink;
+
        /**
         * If using WikiExporter::STREAM to stream a large amount of data,
         * provide a database connection which is not managed by
@@ -103,7 +112,7 @@ class WikiExporter {
         * the most recent version.
         */
        public function allPages() {
-               return $this->dumpFrom( '' );
+               $this->dumpFrom( '' );
        }
 
        /**
@@ -118,7 +127,7 @@ class WikiExporter {
                if ( $end ) {
                        $condition .= ' AND page_id < ' . intval( $end );
                }
-               return $this->dumpFrom( $condition );
+               $this->dumpFrom( $condition );
        }
 
        /**
@@ -133,27 +142,34 @@ class WikiExporter {
                if ( $end ) {
                        $condition .= ' AND rev_id < ' . intval( $end );
                }
-               return $this->dumpFrom( $condition );
+               $this->dumpFrom( $condition );
        }
 
        /**
         * @param $title Title
         */
        public function pageByTitle( $title ) {
-               return $this->dumpFrom(
+               $this->dumpFrom(
                        'page_namespace=' . $title->getNamespace() .
                        ' AND page_title=' . $this->db->addQuotes( $title->getDBkey() ) );
        }
 
+       /**
+        * @param $name string
+        * @throws MWException
+        */
        public function pageByName( $name ) {
                $title = Title::newFromText( $name );
                if ( is_null( $title ) ) {
                        throw new MWException( "Can't export invalid title" );
                } else {
-                       return $this->pageByTitle( $title );
+                       $this->pageByTitle( $title );
                }
        }
 
+       /**
+        * @param $names array
+        */
        public function pagesByName( $names ) {
                foreach ( $names as $name ) {
                        $this->pageByName( $name );
@@ -161,20 +177,28 @@ class WikiExporter {
        }
 
        public function allLogs() {
-               return $this->dumpFrom( '' );
+               $this->dumpFrom( '' );
        }
 
+       /**
+        * @param $start int
+        * @param $end int
+        */
        public function logsByRange( $start, $end ) {
                $condition = 'log_id >= ' . intval( $start );
                if ( $end ) {
                        $condition .= ' AND log_id < ' . intval( $end );
                }
-               return $this->dumpFrom( $condition );
+               $this->dumpFrom( $condition );
        }
 
-       # Generates the distinct list of authors of an article
-       # Not called by default (depends on $this->list_authors)
-       # Can be set by Special:Export when not exporting whole history
+       /**
+        * Generates the distinct list of authors of an article
+        * Not called by default (depends on $this->list_authors)
+        * Can be set by Special:Export when not exporting whole history
+        *
+        * @param $cond
+        */
        protected function do_list_authors( $cond ) {
                wfProfileIn( __METHOD__ );
                $this->author_list = "<contributors>";
@@ -205,13 +229,15 @@ class WikiExporter {
                wfProfileOut( __METHOD__ );
        }
 
+       /**
+        * @param $cond string
+        * @throws MWException
+        * @throws Exception
+        */
        protected function dumpFrom( $cond = '' ) {
                wfProfileIn( __METHOD__ );
                # For logging dumps...
                if ( $this->history & self::LOGS ) {
-                       if ( $this->buffer == WikiExporter::STREAM ) {
-                               $prev = $this->db->bufferResults( false );
-                       }
                        $where = array( 'user_id = log_user' );
                        # Hide private logs
                        $hideLogs = LogEventsList::getExcludeClause( $this->db );
@@ -220,16 +246,49 @@ class WikiExporter {
                        if ( $cond ) $where[] = $cond;
                        # Get logging table name for logging.* clause
                        $logging = $this->db->tableName( 'logging' );
-                       $result = $this->db->select( array( 'logging', 'user' ),
-                               array( "{$logging}.*", 'user_name' ), // grab the user name
-                               $where,
-                               __METHOD__,
-                               array( 'ORDER BY' => 'log_id', 'USE INDEX' => array( 'logging' => 'PRIMARY' ) )
-                       );
-                       $wrapper = $this->db->resultObject( $result );
-                       $this->outputLogStream( $wrapper );
+
                        if ( $this->buffer == WikiExporter::STREAM ) {
-                               $this->db->bufferResults( $prev );
+                               $prev = $this->db->bufferResults( false );
+                       }
+                       $wrapper = null; // Assuring $wrapper is not undefined, if exception occurs early
+                       try {
+                               $result = $this->db->select( array( 'logging', 'user' ),
+                                       array( "{$logging}.*", 'user_name' ), // grab the user name
+                                       $where,
+                                       __METHOD__,
+                                       array( 'ORDER BY' => 'log_id', 'USE INDEX' => array( 'logging' => 'PRIMARY' ) )
+                               );
+                               $wrapper = $this->db->resultObject( $result );
+                               $this->outputLogStream( $wrapper );
+                               if ( $this->buffer == WikiExporter::STREAM ) {
+                                       $this->db->bufferResults( $prev );
+                               }
+                       } catch ( Exception $e ) {
+                               // Throwing the exception does not reliably free the resultset, and
+                               // would also leave the connection in unbuffered mode.
+
+                               // Freeing result
+                               try {
+                                       if ( $wrapper ) {
+                                               $wrapper->free();
+                                       }
+                               } catch ( Exception $e2 ) {
+                                       // Already in panic mode -> ignoring $e2 as $e has
+                                       // higher priority
+                               }
+
+                               // Putting database back in previous buffer mode
+                               try {
+                                       if ( $this->buffer == WikiExporter::STREAM ) {
+                                               $this->db->bufferResults( $prev );
+                                       }
+                               } catch ( Exception $e2 ) {
+                                       // Already in panic mode -> ignoring $e2 as $e has
+                                       // higher priority
+                               }
+
+                               // Inform caller about problem
+                               throw $e;
                        }
                # For page dumps...
                } else {
@@ -279,7 +338,7 @@ class WikiExporter {
                        } elseif ( $this->history & WikiExporter::RANGE ) {
                                # Dump of revisions within a specified range
                                $join['revision'] = array( 'INNER JOIN', 'page_id=rev_page' );
-                               $opts['ORDER BY'] = 'rev_page ASC, rev_id ASC';
+                               $opts['ORDER BY'] = array( 'rev_page ASC', 'rev_id ASC' );
                        } else {
                                # Uknown history specification parameter?
                                wfProfileOut( __METHOD__ );
@@ -300,17 +359,46 @@ class WikiExporter {
                                $prev = $this->db->bufferResults( false );
                        }
 
-                       wfRunHooks( 'ModifyExportQuery',
+                       $wrapper = null; // Assuring $wrapper is not undefined, if exception occurs early
+                       try {
+                               wfRunHooks( 'ModifyExportQuery',
                                                array( $this->db, &$tables, &$cond, &$opts, &$join ) );
 
-                       # Do the query!
-                       $result = $this->db->select( $tables, '*', $cond, __METHOD__, $opts, $join );
-                       $wrapper = $this->db->resultObject( $result );
-                       # Output dump results
-                       $this->outputPageStream( $wrapper );
+                               # Do the query!
+                               $result = $this->db->select( $tables, '*', $cond, __METHOD__, $opts, $join );
+                               $wrapper = $this->db->resultObject( $result );
+                               # Output dump results
+                               $this->outputPageStream( $wrapper );
 
-                       if ( $this->buffer == WikiExporter::STREAM ) {
-                               $this->db->bufferResults( $prev );
+                               if ( $this->buffer == WikiExporter::STREAM ) {
+                                       $this->db->bufferResults( $prev );
+                               }
+                       } catch ( Exception $e ) {
+                               // Throwing the exception does not reliably free the resultset, and
+                               // would also leave the connection in unbuffered mode.
+
+                               // Freeing result
+                               try {
+                                       if ( $wrapper ) {
+                                               $wrapper->free();
+                                       }
+                               } catch ( Exception $e2 ) {
+                                       // Already in panic mode -> ignoring $e2 as $e has
+                                       // higher priority
+                               }
+
+                               // Putting database back in previous buffer mode
+                               try {
+                                       if ( $this->buffer == WikiExporter::STREAM ) {
+                                               $this->db->bufferResults( $prev );
+                                       }
+                               } catch ( Exception $e2 ) {
+                                       // Already in panic mode -> ignoring $e2 as $e has
+                                       // higher priority
+                               }
+
+                               // Inform caller about problem
+                               throw $e;
                        }
                }
                wfProfileOut( __METHOD__ );
@@ -321,7 +409,7 @@ class WikiExporter {
         * The result set should be sorted/grouped by page to avoid duplicate
         * page records in the output.
         *
-        * The result set will be freed once complete. Should be safe for
+        * Should be safe for
         * streaming (non-buffered) queries, as long as it was made on a
         * separate database connection not managed by LoadBalancer; some
         * blob storage types will make queries to pull source data.
@@ -360,6 +448,9 @@ class WikiExporter {
                }
        }
 
+       /**
+        * @param $resultset array
+        */
        protected function outputLogStream( $resultset ) {
                foreach ( $resultset as $row ) {
                        $output = $this->writer->writeLogItem( $row );
@@ -405,6 +496,9 @@ class XmlDumpWriter {
                        $this->siteInfo();
        }
 
+       /**
+        * @return string
+        */
        function siteInfo() {
                $info = array(
                        $this->sitename(),
@@ -417,20 +511,32 @@ class XmlDumpWriter {
                        "\n  </siteinfo>\n";
        }
 
+       /**
+        * @return string
+        */
        function sitename() {
                global $wgSitename;
                return Xml::element( 'sitename', array(), $wgSitename );
        }
 
+       /**
+        * @return string
+        */
        function generator() {
                global $wgVersion;
                return Xml::element( 'generator', array(), "MediaWiki $wgVersion" );
        }
 
+       /**
+        * @return string
+        */
        function homelink() {
                return Xml::element( 'base', array(), Title::newMainPage()->getCanonicalUrl() );
        }
 
+       /**
+        * @return string
+        */
        function caseSetting() {
                global $wgCapitalLinks;
                // "case-insensitive" option is reserved for future
@@ -438,6 +544,9 @@ class XmlDumpWriter {
                return Xml::element( 'case', array(), $sensitivity );
        }
 
+       /**
+        * @return string
+        */
        function namespaces() {
                global $wgContLang;
                $spaces = "<namespaces>\n";
@@ -532,7 +641,7 @@ class XmlDumpWriter {
                if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
                        $out .= "      " . Xml::element( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
                } elseif ( $row->rev_comment != '' ) {
-                       $out .= "      " . Xml::elementClean( 'comment', null, strval( $row->rev_comment ) ) . "\n";
+                       $out .= "      " . Xml::elementClean( 'comment', array(), strval( $row->rev_comment ) ) . "\n";
                }
 
                $text = '';
@@ -612,11 +721,20 @@ class XmlDumpWriter {
                return $out;
        }
 
+       /**
+        * @param $timestamp string
+        * @return string
+        */
        function writeTimestamp( $timestamp ) {
                $ts = wfTimestamp( TS_ISO_8601, $timestamp );
                return "      " . Xml::element( 'timestamp', null, $ts ) . "\n";
        }
 
+       /**
+        * @param $id
+        * @param $text string
+        * @return string
+        */
        function writeContributor( $id, $text ) {
                $out = "      <contributor>\n";
                if ( $id || !IP::isValid( $text ) ) {
@@ -631,6 +749,8 @@ class XmlDumpWriter {
 
        /**
         * Warning! This data is potentially inconsistent. :(
+        * @param $row
+        * @param $dumpContents bool
         * @return string
         */
        function writeUploads( $row, $dumpContents = false ) {
@@ -715,32 +835,55 @@ class XmlDumpWriter {
  * @ingroup Dump
  */
 class DumpOutput {
+
+       /**
+        * @param $string string
+        */
        function writeOpenStream( $string ) {
                $this->write( $string );
        }
 
+       /**
+        * @param $string string
+        */
        function writeCloseStream( $string ) {
                $this->write( $string );
        }
 
+       /**
+        * @param $page
+        * @param $string string
+        */
        function writeOpenPage( $page, $string ) {
                $this->write( $string );
        }
 
+       /**
+        * @param $string string
+        */
        function writeClosePage( $string ) {
                $this->write( $string );
        }
 
+       /**
+        * @param $rev
+        * @param $string string
+        */
        function writeRevision( $rev, $string ) {
                $this->write( $string );
        }
 
+       /**
+        * @param $rev
+        * @param $string string
+        */
        function writeLogItem( $rev, $string ) {
                $this->write( $string );
        }
 
        /**
         * Override to write to a different stream type.
+        * @param $string string
         * @return bool
         */
        function write( $string ) {
@@ -786,11 +929,17 @@ class DumpOutput {
 class DumpFileOutput extends DumpOutput {
        protected $handle = false, $filename;
 
+       /**
+        * @param $file
+        */
        function __construct( $file ) {
                $this->handle = fopen( $file, "wt" );
                $this->filename = $file;
        }
 
+       /**
+        * @param $string string
+        */
        function writeCloseStream( $string ) {
                parent::writeCloseStream( $string );
                if ( $this->handle ) {
@@ -799,20 +948,35 @@ class DumpFileOutput extends DumpOutput {
                }
        }
 
+       /**
+        * @param $string string
+        */
        function write( $string ) {
                fputs( $this->handle, $string );
        }
 
+       /**
+        * @param $newname
+        */
        function closeRenameAndReopen( $newname ) {
                $this->closeAndRename( $newname, true );
        }
 
+       /**
+        * @param $newname
+        * @throws MWException
+        */
        function renameOrException( $newname ) {
                        if (! rename( $this->filename, $newname ) ) {
                                throw new MWException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
                        }
        }
 
+       /**
+        * @param $newname array
+        * @return mixed
+        * @throws MWException
+        */
        function checkRenameArgCount( $newname ) {
                if ( is_array( $newname ) ) {
                        if ( count( $newname ) > 1 ) {
@@ -824,6 +988,10 @@ class DumpFileOutput extends DumpOutput {
                return $newname;
        }
 
+       /**
+        * @param $newname mixed
+        * @param $open bool
+        */
        function closeAndRename( $newname, $open = false ) {
                $newname = $this->checkRenameArgCount( $newname );
                if ( $newname ) {
@@ -838,6 +1006,9 @@ class DumpFileOutput extends DumpOutput {
                }
        }
 
+       /**
+        * @return string|null
+        */
        function getFilenames() {
                return $this->filename;
        }
@@ -851,8 +1022,12 @@ class DumpFileOutput extends DumpOutput {
  */
 class DumpPipeOutput extends DumpFileOutput {
        protected $command, $filename;
-       private $procOpenResource = false;
+       protected $procOpenResource = false;
 
+       /**
+        * @param $command
+        * @param $file null
+        */
        function __construct( $command, $file = null ) {
                if ( !is_null( $file ) ) {
                        $command .=  " > " . wfEscapeShellArg( $file );
@@ -863,6 +1038,9 @@ class DumpPipeOutput extends DumpFileOutput {
                $this->filename = $file;
        }
 
+       /**
+        * @param $string string
+        */
        function writeCloseStream( $string ) {
                parent::writeCloseStream( $string );
                if ( $this->procOpenResource ) {
@@ -871,6 +1049,9 @@ class DumpPipeOutput extends DumpFileOutput {
                }
        }
 
+       /**
+        * @param $command
+        */
        function startCommand( $command ) {
                $spec = array(
                        0 => array( "pipe", "r" ),
@@ -880,10 +1061,17 @@ class DumpPipeOutput extends DumpFileOutput {
                $this->handle = $pipes[0];
        }
 
+       /**
+        * @param mixed $newname
+        */
        function closeRenameAndReopen( $newname ) {
                $this->closeAndRename( $newname, true );
        }
 
+       /**
+        * @param $newname mixed
+        * @param $open bool
+        */
        function closeAndRename( $newname, $open = false ) {
                $newname = $this->checkRenameArgCount( $newname );
                if ( $newname ) {
@@ -911,6 +1099,10 @@ class DumpPipeOutput extends DumpFileOutput {
  * @ingroup Dump
  */
 class DumpGZipOutput extends DumpPipeOutput {
+
+       /**
+        * @param $file string
+        */
        function __construct( $file ) {
                parent::__construct( "gzip", $file );
        }
@@ -921,6 +1113,10 @@ class DumpGZipOutput extends DumpPipeOutput {
  * @ingroup Dump
  */
 class DumpBZip2Output extends DumpPipeOutput {
+
+       /**
+        * @param $file string
+        */
        function __construct( $file ) {
                parent::__construct( "bzip2", $file );
        }
@@ -931,12 +1127,20 @@ class DumpBZip2Output extends DumpPipeOutput {
  * @ingroup Dump
  */
 class Dump7ZipOutput extends DumpPipeOutput {
+
+       /**
+        * @param $file string
+        */
        function __construct( $file ) {
                $command = $this->setup7zCommand( $file );
                parent::__construct( $command );
                $this->filename = $file;
        }
 
+       /**
+        * @param $file string
+        * @return string
+        */
        function setup7zCommand( $file ) {
                $command = "7za a -bd -si " . wfEscapeShellArg( $file );
                // Suppress annoying useless crap from p7zip
@@ -945,6 +1149,10 @@ class Dump7ZipOutput extends DumpPipeOutput {
                return( $command );
        }
 
+       /**
+        * @param $newname string
+        * @param $open bool
+        */
        function closeAndRename( $newname, $open = false ) {
                $newname = $this->checkRenameArgCount( $newname );
                if ( $newname ) {
@@ -959,8 +1167,6 @@ class Dump7ZipOutput extends DumpPipeOutput {
        }
 }
 
-
-
 /**
  * Dump output filter class.
  * This just does output filtering and streaming; XML formatting is done
@@ -968,18 +1174,44 @@ class Dump7ZipOutput extends DumpPipeOutput {
  * @ingroup Dump
  */
 class DumpFilter {
+
+       /**
+        * @var DumpOutput
+        * FIXME will need to be made protected whenever legacy code
+        * is updated.
+        */
+       public $sink;
+
+       /**
+        * @var bool
+        */
+       protected $sendingThisPage;
+
+       /**
+        * @param $sink DumpOutput
+        */
        function __construct( &$sink ) {
                $this->sink =& $sink;
        }
 
+       /**
+        * @param $string string
+        */
        function writeOpenStream( $string ) {
                $this->sink->writeOpenStream( $string );
        }
 
+       /**
+        * @param $string string
+        */
        function writeCloseStream( $string ) {
                $this->sink->writeCloseStream( $string );
        }
 
+       /**
+        * @param $page
+        * @param $string string
+        */
        function writeOpenPage( $page, $string ) {
                $this->sendingThisPage = $this->pass( $page, $string );
                if ( $this->sendingThisPage ) {
@@ -987,6 +1219,9 @@ class DumpFilter {
                }
        }
 
+       /**
+        * @param $string string
+        */
        function writeClosePage( $string ) {
                if ( $this->sendingThisPage ) {
                        $this->sink->writeClosePage( $string );
@@ -994,30 +1229,49 @@ class DumpFilter {
                }
        }
 
+       /**
+        * @param $rev
+        * @param $string string
+        */
        function writeRevision( $rev, $string ) {
                if ( $this->sendingThisPage ) {
                        $this->sink->writeRevision( $rev, $string );
                }
        }
 
+       /**
+        * @param $rev
+        * @param $string string
+        */
        function writeLogItem( $rev, $string ) {
                $this->sink->writeRevision( $rev, $string );
        }
 
+       /**
+        * @param $newname string
+        */
        function closeRenameAndReopen( $newname ) {
                $this->sink->closeRenameAndReopen( $newname );
        }
 
+       /**
+        * @param $newname string
+        * @param $open bool
+        */
        function closeAndRename( $newname, $open = false ) {
                $this->sink->closeAndRename( $newname, $open );
        }
 
+       /**
+        * @return array
+        */
        function getFilenames() {
                return $this->sink->getFilenames();
        }
 
        /**
         * Override for page-based filter types.
+        * @param $page
         * @return bool
         */
        function pass( $page ) {
@@ -1030,6 +1284,11 @@ class DumpFilter {
  * @ingroup Dump
  */
 class DumpNotalkFilter extends DumpFilter {
+
+       /**
+        * @param $page
+        * @return bool
+        */
        function pass( $page ) {
                return !MWNamespace::isTalk( $page->page_namespace );
        }
@@ -1043,6 +1302,10 @@ class DumpNamespaceFilter extends DumpFilter {
        var $invert = false;
        var $namespaces = array();
 
+       /**
+        * @param $sink DumpOutput
+        * @param $param
+        */
        function __construct( &$sink, $param ) {
                parent::__construct( $sink );
 
@@ -1085,6 +1348,10 @@ class DumpNamespaceFilter extends DumpFilter {
                }
        }
 
+       /**
+        * @param $page
+        * @return bool
+        */
        function pass( $page ) {
                $match = isset( $this->namespaces[$page->page_namespace] );
                return $this->invert xor $match;
@@ -1099,11 +1366,18 @@ class DumpNamespaceFilter extends DumpFilter {
 class DumpLatestFilter extends DumpFilter {
        var $page, $pageString, $rev, $revString;
 
+       /**
+        * @param $page
+        * @param $string string
+        */
        function writeOpenPage( $page, $string ) {
                $this->page = $page;
                $this->pageString = $string;
        }
 
+       /**
+        * @param $string string
+        */
        function writeClosePage( $string ) {
                if ( $this->rev ) {
                        $this->sink->writeOpenPage( $this->page, $this->pageString );
@@ -1116,6 +1390,10 @@ class DumpLatestFilter extends DumpFilter {
                $this->pageString = null;
        }
 
+       /**
+        * @param $rev
+        * @param $string string
+        */
        function writeRevision( $rev, $string ) {
                if ( $rev->rev_id == $this->page->page_latest ) {
                        $this->rev = $rev;
@@ -1129,51 +1407,82 @@ class DumpLatestFilter extends DumpFilter {
  * @ingroup Dump
  */
 class DumpMultiWriter {
+
+       /**
+        * @param $sinks
+        */
        function __construct( $sinks ) {
                $this->sinks = $sinks;
                $this->count = count( $sinks );
        }
 
+       /**
+        * @param $string string
+        */
        function writeOpenStream( $string ) {
                for ( $i = 0; $i < $this->count; $i++ ) {
                        $this->sinks[$i]->writeOpenStream( $string );
                }
        }
 
+       /**
+        * @param $string string
+        */
        function writeCloseStream( $string ) {
                for ( $i = 0; $i < $this->count; $i++ ) {
                        $this->sinks[$i]->writeCloseStream( $string );
                }
        }
 
+       /**
+        * @param $page
+        * @param $string string
+        */
        function writeOpenPage( $page, $string ) {
                for ( $i = 0; $i < $this->count; $i++ ) {
                        $this->sinks[$i]->writeOpenPage( $page, $string );
                }
        }
 
+       /**
+        * @param $string
+        */
        function writeClosePage( $string ) {
                for ( $i = 0; $i < $this->count; $i++ ) {
                        $this->sinks[$i]->writeClosePage( $string );
                }
        }
 
+       /**
+        * @param $rev
+        * @param $string
+        */
        function writeRevision( $rev, $string ) {
                for ( $i = 0; $i < $this->count; $i++ ) {
                        $this->sinks[$i]->writeRevision( $rev, $string );
                }
        }
 
+       /**
+        * @param $newnames
+        */
        function closeRenameAndReopen( $newnames ) {
                $this->closeAndRename( $newnames, true );
        }
 
+       /**
+        * @param $newnames array
+        * @param bool $open
+        */
        function closeAndRename( $newnames, $open = false ) {
                for ( $i = 0; $i < $this->count; $i++ ) {
                        $this->sinks[$i]->closeAndRename( $newnames[$i], $open );
                }
        }
 
+       /**
+        * @return array
+        */
        function getFilenames() {
                $filenames = array();
                for ( $i = 0; $i < $this->count; $i++ ) {
@@ -1184,6 +1493,10 @@ class DumpMultiWriter {
 
 }
 
+/**
+ * @param $string string
+ * @return string
+ */
 function xmlsafe( $string ) {
        wfProfileIn( __FUNCTION__ );