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