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