Make sure the TOC is there when toggleToc is called. Although the function calling...
[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 $this->progress( "Warning: assuming stdout for filter output\n" );
130 $sink = new DumpOutput();
131 }
132 if ( !isset( $this->filterTypes[$val] ) ) {
133 wfDie( "Unrecognized filter type '$val'\n" );
134 }
135 $type = $this->filterTypes[$val];
136 $filter = new $type( $sink, $param );
137
138 // references are lame in php...
139 unset( $sink );
140 $sink = $filter;
141
142 break;
143 case "report":
144 $this->reportingInterval = intval( $val );
145 break;
146 case "server":
147 $this->server = $val;
148 break;
149 case "force-normal":
150 if ( !function_exists( 'utf8_normalize' ) ) {
151 wfDl( "php_utfnormal.so" );
152 if ( !function_exists( 'utf8_normalize' ) ) {
153 wfDie( "Failed to load UTF-8 normalization extension. " .
154 "Install or remove --force-normal parameter to use slower code.\n" );
155 }
156 }
157 break;
158 default:
159 $this->processOption( $opt, $val, $param );
160 }
161 }
162 }
163
164 if ( is_null( $sink ) ) {
165 $sink = new DumpOutput();
166 }
167 $sinks[] = $sink;
168
169 if ( count( $sinks ) > 1 ) {
170 return new DumpMultiWriter( $sinks );
171 } else {
172 return $sink;
173 }
174 }
175
176 function processOption( $opt, $val, $param ) {
177 // extension point for subclasses to add options
178 }
179
180 function dump( $history, $text = WikiExporter::TEXT ) {
181 # Notice messages will foul up your XML output even if they're
182 # relatively harmless.
183 if ( ini_get( 'display_errors' ) )
184 ini_set( 'display_errors', 'stderr' );
185
186 $this->initProgress( $history );
187
188 $db = $this->backupDb();
189 $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
190 $exporter->dumpUploads = $this->dumpUploads;
191
192 $wrapper = new ExportProgressFilter( $this->sink, $this );
193 $exporter->setOutputSink( $wrapper );
194
195 if ( !$this->skipHeader )
196 $exporter->openStream();
197 # Log item dumps: all or by range
198 if ( $history & WikiExporter::LOGS ) {
199 if ( $this->startId || $this->endId ) {
200 $exporter->logsByRange( $this->startId, $this->endId );
201 } else {
202 $exporter->allLogs();
203 }
204 # Page dumps: all or by page ID range
205 } else if ( is_null( $this->pages ) ) {
206 if ( $this->startId || $this->endId ) {
207 $exporter->pagesByRange( $this->startId, $this->endId );
208 } else {
209 $exporter->allPages();
210 }
211 # Dump of specific pages
212 } else {
213 $exporter->pagesByName( $this->pages );
214 }
215
216 if ( !$this->skipFooter )
217 $exporter->closeStream();
218
219 $this->report( true );
220 }
221
222 /**
223 * Initialise starting time and maximum revision count.
224 * We'll make ETA calculations based an progress, assuming relatively
225 * constant per-revision rate.
226 * @param $history Integer: WikiExporter::CURRENT or WikiExporter::FULL
227 */
228 function initProgress( $history = WikiExporter::FULL ) {
229 $table = ( $history == WikiExporter::CURRENT ) ? 'page' : 'revision';
230 $field = ( $history == WikiExporter::CURRENT ) ? 'page_id' : 'rev_id';
231
232 $dbr = wfGetDB( DB_SLAVE );
233 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', __METHOD__ );
234 $this->startTime = wfTime();
235 }
236
237 /**
238 * @todo Fixme: the --server parameter is currently not respected, as it
239 * doesn't seem terribly easy to ask the load balancer for a particular
240 * connection by name.
241 */
242 function backupDb() {
243 $this->lb = wfGetLBFactory()->newMainLB();
244 $db = $this->lb->getConnection( DB_SLAVE, 'backup' );
245
246 // Discourage the server from disconnecting us if it takes a long time
247 // to read out the big ol' batch query.
248 $db->setTimeout( 3600 * 24 );
249
250 return $db;
251 }
252
253 function __destruct() {
254 if ( isset( $this->lb ) ) {
255 $this->lb->closeAll();
256 }
257 }
258
259 function backupServer() {
260 global $wgDBserver;
261 return $this->server
262 ? $this->server
263 : $wgDBserver;
264 }
265
266 function reportPage() {
267 $this->pageCount++;
268 }
269
270 function revCount() {
271 $this->revCount++;
272 $this->report();
273 }
274
275 function report( $final = false ) {
276 if ( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
277 $this->showReport();
278 }
279 }
280
281 function showReport() {
282 if ( $this->reporting ) {
283 $delta = wfTime() - $this->startTime;
284 $now = wfTimestamp( TS_DB );
285 if ( $delta ) {
286 $rate = $this->pageCount / $delta;
287 $revrate = $this->revCount / $delta;
288 $portion = $this->revCount / $this->maxCount;
289 $eta = $this->startTime + $delta / $portion;
290 $etats = wfTimestamp( TS_DB, intval( $eta ) );
291 } else {
292 $rate = '-';
293 $revrate = '-';
294 $etats = '-';
295 }
296 $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
297 $now, wfWikiID(), $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
298 }
299 }
300
301 function progress( $string ) {
302 fwrite( $this->stderr, $string . "\n" );
303 }
304 }
305
306 class ExportProgressFilter extends DumpFilter {
307 function ExportProgressFilter( &$sink, &$progress ) {
308 parent::__construct( $sink );
309 $this->progress = $progress;
310 }
311
312 function writeClosePage( $string ) {
313 parent::writeClosePage( $string );
314 $this->progress->reportPage();
315 }
316
317 function writeRevision( $rev, $string ) {
318 parent::writeRevision( $rev, $string );
319 $this->progress->revCount();
320 }
321 }