9b2ff8929511738b3d4ad6e4981ae8ff182a7b3a
[lhc/web/wiklou.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 $name String
71 * @param $class String: name of output filter plugin class
72 */
73 function registerOutput( $name, $class ) {
74 $this->outputTypes[$name] = $class;
75 }
76
77 /**
78 * @param $name String
79 * @param $class String: 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 *
88 * @param $class String: name of plugin class; must have a static 'register'
89 * method that takes a BackupDumper as a parameter.
90 * @param $file String: full or relative path to the PHP file to load, or empty
91 */
92 function loadPlugin( $class, $file ) {
93 if ( $file != '' ) {
94 require_once( $file );
95 }
96 $register = array( $class, 'register' );
97 call_user_func_array( $register, array( &$this ) );
98 }
99
100 /**
101 * @param $args Array
102 * @return Array
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 wfDl( "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 $history Integer: 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 * @todo Fixme: the --server parameter is currently not respected, as it
237 * doesn't seem terribly easy to ask the load balancer for a particular
238 * connection by name.
239 */
240 function backupDb() {
241 $this->lb = wfGetLBFactory()->newMainLB();
242 $db = $this->lb->getConnection( DB_SLAVE, 'backup' );
243
244 // Discourage the server from disconnecting us if it takes a long time
245 // to read out the big ol' batch query.
246 $db->setTimeout( 3600 * 24 );
247
248 return $db;
249 }
250
251 function __destruct() {
252 if ( isset( $this->lb ) ) {
253 $this->lb->closeAll();
254 }
255 }
256
257 function backupServer() {
258 global $wgDBserver;
259 return $this->server
260 ? $this->server
261 : $wgDBserver;
262 }
263
264 function reportPage() {
265 $this->pageCount++;
266 }
267
268 function revCount() {
269 $this->revCount++;
270 $this->report();
271 }
272
273 function report( $final = false ) {
274 if ( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
275 $this->showReport();
276 }
277 }
278
279 function showReport() {
280 if ( $this->reporting ) {
281 $delta = wfTime() - $this->startTime;
282 $now = wfTimestamp( TS_DB );
283 if ( $delta ) {
284 $rate = $this->pageCount / $delta;
285 $revrate = $this->revCount / $delta;
286 $portion = $this->revCount / $this->maxCount;
287 $eta = $this->startTime + $delta / $portion;
288 $etats = wfTimestamp( TS_DB, intval( $eta ) );
289 } else {
290 $rate = '-';
291 $revrate = '-';
292 $etats = '-';
293 }
294 $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
295 $now, wfWikiID(), $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
296 }
297 }
298
299 function progress( $string ) {
300 fwrite( $this->stderr, $string . "\n" );
301 }
302 }
303
304 class ExportProgressFilter extends DumpFilter {
305 function ExportProgressFilter( &$sink, &$progress ) {
306 parent::DumpFilter( $sink );
307 $this->progress = $progress;
308 }
309
310 function writeClosePage( $string ) {
311 parent::writeClosePage( $string );
312 $this->progress->reportPage();
313 }
314
315 function writeRevision( $rev, $string ) {
316 parent::writeRevision( $rev, $string );
317 $this->progress->revCount();
318 }
319 }