Added experimental support for restoration
[mediawiki.git] / maintenance / backup.inc
blob6b7150dacad2500a67855451232e2787e7ccc442
1 <?php
2 /**
3  * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4  * http://www.mediawiki.org/
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  * http://www.gnu.org/copyleft/gpl.html
20  *
21  * @package MediaWiki
22  * @subpackage SpecialPage
23  */
26 class BackupDumper {
27         var $reportingInterval = 100;
28         var $reporting = true;
29         var $pageCount = 0;
30         var $revCount  = 0;
31         var $server    = null; // use default
32         var $pages     = null; // all pages
33         var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
34         var $skipFooter = false; // don't output </mediawiki>
35         var $startId    = 0;
36         var $endId      = 0;
37         var $sink       = null; // Output filters
38         var $stubText   = false; // include rev_text_id instead of text; for 2-pass dump
40         function BackupDumper( $args ) {
41                 $this->stderr = fopen( "php://stderr", "wt" );
43                 // Built-in output and filter plugins
44                 $this->registerOutput( 'file', 'DumpFileOutput' );
45                 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
46                 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
47                 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
49                 $this->registerFilter( 'latest', 'DumpLatestFilter' );
50                 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
51                 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
53                 $this->sink = $this->processArgs( $args );
54         }
56         /**
57          * @param string $name
58          * @param string $class name of output filter plugin class
59          */
60         function registerOutput( $name, $class ) {
61                 $this->outputTypes[$name] = $class;
62         }
64         /**
65          * @param string $name
66          * @param string $class name of filter plugin class
67          */
68         function registerFilter( $name, $class ) {
69                 $this->filterTypes[$name] = $class;
70         }
72         /**
73          * Load a plugin and register it
74          * @param string $class Name of plugin class; must have a static 'register'
75          *                      method that takes a BackupDumper as a parameter.
76          * @param string $file Full or relative path to the PHP file to load, or empty
77          */
78         function loadPlugin( $class, $file ) {
79                 if( $file != '' ) {
80                         require_once( $file );
81                 }
82                 $register = array( $class, 'register' );
83                 call_user_func_array( $register, array( &$this ) );
84         }
86         /**
87          * @param array $args
88          * @return array
89          * @static
90          */
91         function processArgs( $args ) {
92                 $sink = null;
93                 $sinks = array();
94                 foreach( $args as $arg ) {
95                         if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
96                                 @list( $full, $opt, $val, $param ) = $matches;
97                                 switch( $opt ) {
98                                 case "plugin":
99                                         $this->loadPlugin( $val, $param );
100                                         break;
101                                 case "output":
102                                         if( !is_null( $sink ) ) {
103                                                 $sinks[] = $sink;
104                                         }
105                                         if( !isset( $this->outputTypes[$val] ) ) {
106                                                 wfDie( "Unrecognized output sink type '$val'\n" );
107                                         }
108                                         $type = $this->outputTypes[$val];
109                                         $sink = new $type( $param );
110                                         break;
111                                 case "filter":
112                                         if( is_null( $sink ) ) {
113                                                 $this->progress( "Warning: assuming stdout for filter output\n" );
114                                                 $sink = new DumpOutput();
115                                         }
116                                         if( !isset( $this->filterTypes[$val] ) ) {
117                                                 wfDie( "Unrecognized filter type '$val'\n" );
118                                         }
119                                         $type = $this->filterTypes[$val];
120                                         $filter = new $type( $sink, $param );
122                                         // references are lame in php...
123                                         unset( $sink );
124                                         $sink = $filter;
126                                         break;
127                                 case "report":
128                                         $this->reportingInterval = intval( $val );
129                                         break;
130                                 case "server":
131                                         $this->server = $val;
132                                         break;
133                                 default:
134                                         $this->processOption( $opt, $val, $param );
135                                 }
136                         }
137                 }
139                 if( is_null( $sink ) ) {
140                         $sink = new DumpOutput();
141                 }
142                 $sinks[] = $sink;
144                 if( count( $sinks ) > 1 ) {
145                         return new DumpMultiWriter( $sinks );
146                 } else {
147                         return $sink;
148                 }
149         }
151         function processOption( $opt, $val, $param ) {
152                 // extension point for subclasses to add options
153         }
155         function dump( $history, $text = MW_EXPORT_TEXT ) {
156                 # This shouldn't happen if on console... ;)
157                 header( 'Content-type: text/html; charset=UTF-8' );
159                 # Notice messages will foul up your XML output even if they're
160                 # relatively harmless.
161                 ini_set( 'display_errors', false );
163                 $this->initProgress( $history );
165                 $db =& $this->backupDb();
166                 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM, $text );
168                 $wrapper = new ExportProgressFilter( $this->sink, $this );
169                 $exporter->setOutputSink( $wrapper );
171                 if( !$this->skipHeader )
172                         $exporter->openStream();
174                 if( is_null( $this->pages ) ) {
175                         if( $this->startId || $this->endId ) {
176                                 $exporter->pagesByRange( $this->startId, $this->endId );
177                         } else {
178                                 $exporter->allPages();
179                         }
180                 } else {
181                         $exporter->pagesByName( $this->pages );
182                 }
184                 if( !$this->skipFooter )
185                         $exporter->closeStream();
187                 $this->report( true );
188         }
189         
190         /**
191          * Initialise starting time and maximum revision count.
192          * We'll make ETA calculations based an progress, assuming relatively
193          * constant per-revision rate.
194          * @param int $history MW_EXPORT_CURRENT or MW_EXPORT_FULL
195          */
196         function initProgress( $history = MW_EXPORT_FULL ) {
197                 $table = ($history == MW_EXPORT_CURRENT) ? 'page' : 'revision';
198                 $field = ($history == MW_EXPORT_CURRENT) ? 'page_id' : 'rev_id';
199                 
200                 $dbr =& wfGetDB( DB_SLAVE );
201                 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', 'BackupDumper::dump' );
202                 $this->startTime = wfTime();
203         }
205         function &backupDb() {
206                 global $wgDBadminuser, $wgDBadminpassword;
207                 global $wgDBname, $wgDebugDumpSql;
208                 $flags = ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT; // god-damn hack
209                 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname, false, $flags );
210                 $timeout = 3600 * 24;
211                 $db->query( "SET net_read_timeout=$timeout" );
212                 $db->query( "SET net_write_timeout=$timeout" );
213                 return $db;
214         }
216         function backupServer() {
217                 global $wgDBserver;
218                 return $this->server
219                         ? $this->server
220                         : $wgDBserver;
221         }
223         function reportPage() {
224                 $this->pageCount++;
225         }
227         function revCount() {
228                 $this->revCount++;
229                 $this->report();
230         }
232         function report( $final = false ) {
233                 if( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
234                         $this->showReport();
235                 }
236         }
238         function showReport() {
239                 if( $this->reporting ) {
240                         $delta = wfTime() - $this->startTime;
241                         $now = wfTimestamp( TS_DB );
242                         if( $delta ) {
243                                 $rate = $this->pageCount / $delta;
244                                 $revrate = $this->revCount / $delta;
245                                 $portion = $this->revCount / $this->maxCount;
246                                 $eta = $this->startTime + $delta / $portion;
247                                 $etats = wfTimestamp( TS_DB, intval( $eta ) );
248                         } else {
249                                 $rate = '-';
250                                 $revrate = '-';
251                                 $etats = '-';
252                         }
253                         global $wgDBname;
254                         $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
255                                 $now, $wgDBname, $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
256                 }
257         }
259         function progress( $string ) {
260                 fwrite( $this->stderr, $string . "\n" );
261         }
264 class ExportProgressFilter extends DumpFilter {
265         function ExportProgressFilter( &$sink, &$progress ) {
266                 parent::DumpFilter( $sink );
267                 $this->progress = $progress;
268         }
270         function writeClosePage( $string ) {
271                 parent::writeClosePage( $string );
272                 $this->progress->reportPage();
273         }
275         function writeRevision( $rev, $string ) {
276                 parent::writeRevision( $rev, $string );
277                 $this->progress->revCount();
278         }