]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/backup.inc
MediaWiki 1.11.0
[autoinstalls/mediawiki.git] / maintenance / backup.inc
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  * @addtogroup SpecialPage
22  */
23
24 class DumpDBZip2Output extends DumpPipeOutput {
25         function DumpDBZip2Output( $file ) {
26                 parent::DumpPipeOutput( "dbzip2", $file );
27         }
28 }
29
30 class BackupDumper {
31         var $reportingInterval = 100;
32         var $reporting = true;
33         var $pageCount = 0;
34         var $revCount  = 0;
35         var $server    = null; // use default
36         var $pages     = null; // all pages
37         var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
38         var $skipFooter = false; // don't output </mediawiki>
39         var $startId    = 0;
40         var $endId      = 0;
41         var $sink       = null; // Output filters
42         var $stubText   = false; // include rev_text_id instead of text; for 2-pass dump
43
44         function BackupDumper( $args ) {
45                 $this->stderr = fopen( "php://stderr", "wt" );
46
47                 // Built-in output and filter plugins
48                 $this->registerOutput( 'file', 'DumpFileOutput' );
49                 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
50                 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
51                 $this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
52                 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
53
54                 $this->registerFilter( 'latest', 'DumpLatestFilter' );
55                 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
56                 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
57
58                 $this->sink = $this->processArgs( $args );
59         }
60
61         /**
62          * @param string $name
63          * @param string $class name of output filter plugin class
64          */
65         function registerOutput( $name, $class ) {
66                 $this->outputTypes[$name] = $class;
67         }
68
69         /**
70          * @param string $name
71          * @param string $class name of filter plugin class
72          */
73         function registerFilter( $name, $class ) {
74                 $this->filterTypes[$name] = $class;
75         }
76
77         /**
78          * Load a plugin and register it
79          * @param string $class Name of plugin class; must have a static 'register'
80          *                      method that takes a BackupDumper as a parameter.
81          * @param string $file Full or relative path to the PHP file to load, or empty
82          */
83         function loadPlugin( $class, $file ) {
84                 if( $file != '' ) {
85                         require_once( $file );
86                 }
87                 $register = array( $class, 'register' );
88                 call_user_func_array( $register, array( &$this ) );
89         }
90
91         /**
92          * @param array $args
93          * @return array
94          * @static
95          */
96         function processArgs( $args ) {
97                 $sink = null;
98                 $sinks = array();
99                 foreach( $args as $arg ) {
100                         $matches = array();
101                         if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
102                                 @list( /* $full */ , $opt, $val, $param ) = $matches;
103                                 switch( $opt ) {
104                                 case "plugin":
105                                         $this->loadPlugin( $val, $param );
106                                         break;
107                                 case "output":
108                                         if( !is_null( $sink ) ) {
109                                                 $sinks[] = $sink;
110                                         }
111                                         if( !isset( $this->outputTypes[$val] ) ) {
112                                                 wfDie( "Unrecognized output sink type '$val'\n" );
113                                         }
114                                         $type = $this->outputTypes[$val];
115                                         $sink = new $type( $param );
116                                         break;
117                                 case "filter":
118                                         if( is_null( $sink ) ) {
119                                                 $this->progress( "Warning: assuming stdout for filter output\n" );
120                                                 $sink = new DumpOutput();
121                                         }
122                                         if( !isset( $this->filterTypes[$val] ) ) {
123                                                 wfDie( "Unrecognized filter type '$val'\n" );
124                                         }
125                                         $type = $this->filterTypes[$val];
126                                         $filter = new $type( $sink, $param );
127
128                                         // references are lame in php...
129                                         unset( $sink );
130                                         $sink = $filter;
131
132                                         break;
133                                 case "report":
134                                         $this->reportingInterval = intval( $val );
135                                         break;
136                                 case "server":
137                                         $this->server = $val;
138                                         break;
139                                 case "force-normal":
140                                         if( !function_exists( 'utf8_normalize' ) ) {
141                                                 dl( "php_utfnormal.so" );
142                                                 if( !function_exists( 'utf8_normalize' ) ) {
143                                                         wfDie( "Failed to load UTF-8 normalization extension. " .
144                                                                 "Install or remove --force-normal parameter to use slower code.\n" );
145                                                 }
146                                         }
147                                         break;
148                                 default:
149                                         $this->processOption( $opt, $val, $param );
150                                 }
151                         }
152                 }
153
154                 if( is_null( $sink ) ) {
155                         $sink = new DumpOutput();
156                 }
157                 $sinks[] = $sink;
158
159                 if( count( $sinks ) > 1 ) {
160                         return new DumpMultiWriter( $sinks );
161                 } else {
162                         return $sink;
163                 }
164         }
165
166         function processOption( $opt, $val, $param ) {
167                 // extension point for subclasses to add options
168         }
169
170         function dump( $history, $text = MW_EXPORT_TEXT ) {
171                 # Notice messages will foul up your XML output even if they're
172                 # relatively harmless.
173                 ini_set( 'display_errors', false );
174
175                 $this->initProgress( $history );
176
177                 $db = $this->backupDb();
178                 $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
179
180                 $wrapper = new ExportProgressFilter( $this->sink, $this );
181                 $exporter->setOutputSink( $wrapper );
182
183                 if( !$this->skipHeader )
184                         $exporter->openStream();
185
186                 if( is_null( $this->pages ) ) {
187                         if( $this->startId || $this->endId ) {
188                                 $exporter->pagesByRange( $this->startId, $this->endId );
189                         } else {
190                                 $exporter->allPages();
191                         }
192                 } else {
193                         $exporter->pagesByName( $this->pages );
194                 }
195
196                 if( !$this->skipFooter )
197                         $exporter->closeStream();
198
199                 $this->report( true );
200         }
201         
202         /**
203          * Initialise starting time and maximum revision count.
204          * We'll make ETA calculations based an progress, assuming relatively
205          * constant per-revision rate.
206          * @param int $history WikiExporter::CURRENT or WikiExporter::FULL
207          */
208         function initProgress( $history = WikiExporter::FULL ) {
209                 $table = ($history == WikiExporter::CURRENT) ? 'page' : 'revision';
210                 $field = ($history == WikiExporter::CURRENT) ? 'page_id' : 'rev_id';
211                 
212                 $dbr = wfGetDB( DB_SLAVE );
213                 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', 'BackupDumper::dump' );
214                 $this->startTime = wfTime();
215         }
216
217         function backupDb() {
218                 global $wgDBadminuser, $wgDBadminpassword;
219                 global $wgDBname, $wgDebugDumpSql, $wgDBtype;
220                 $flags = ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT; // god-damn hack
221
222                 $class = 'Database' . ucfirst($wgDBtype);
223                 $db = new $class( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname, false, $flags );
224                 
225                 // Discourage the server from disconnecting us if it takes a long time
226                 // to read out the big ol' batch query.
227                 $db->setTimeout( 3600 * 24 );
228                 
229                 return $db;
230         }
231
232         function backupServer() {
233                 global $wgDBserver;
234                 return $this->server
235                         ? $this->server
236                         : $wgDBserver;
237         }
238
239         function reportPage() {
240                 $this->pageCount++;
241         }
242
243         function revCount() {
244                 $this->revCount++;
245                 $this->report();
246         }
247
248         function report( $final = false ) {
249                 if( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
250                         $this->showReport();
251                 }
252         }
253
254         function showReport() {
255                 if( $this->reporting ) {
256                         $delta = wfTime() - $this->startTime;
257                         $now = wfTimestamp( TS_DB );
258                         if( $delta ) {
259                                 $rate = $this->pageCount / $delta;
260                                 $revrate = $this->revCount / $delta;
261                                 $portion = $this->revCount / $this->maxCount;
262                                 $eta = $this->startTime + $delta / $portion;
263                                 $etats = wfTimestamp( TS_DB, intval( $eta ) );
264                         } else {
265                                 $rate = '-';
266                                 $revrate = '-';
267                                 $etats = '-';
268                         }
269                         $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
270                                 $now, wfWikiID(), $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
271                 }
272         }
273
274         function progress( $string ) {
275                 fwrite( $this->stderr, $string . "\n" );
276         }
277 }
278
279 class ExportProgressFilter extends DumpFilter {
280         function ExportProgressFilter( &$sink, &$progress ) {
281                 parent::DumpFilter( $sink );
282                 $this->progress = $progress;
283         }
284
285         function writeClosePage( $string ) {
286                 parent::writeClosePage( $string );
287                 $this->progress->reportPage();
288         }
289
290         function writeRevision( $rev, $string ) {
291                 parent::writeRevision( $rev, $string );
292                 $this->progress->revCount();
293         }
294 }
295
296 ?>