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