* Split off DB load monitoring logic into a LoadMonitor class hierarchy, to allow...
[lhc/web/wiklou.git] / maintenance / importDump.php
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 Maintenance
23 */
24
25 $optionsWithArgs = array( 'report' );
26
27 require_once( 'commandLine.inc' );
28
29 /**
30 * @ingroup Maintenance
31 */
32 class BackupReader {
33 var $reportingInterval = 100;
34 var $reporting = true;
35 var $pageCount = 0;
36 var $revCount = 0;
37 var $dryRun = false;
38 var $debug = false;
39 var $uploads = false;
40
41 function BackupReader() {
42 $this->stderr = fopen( "php://stderr", "wt" );
43 }
44
45 function reportPage( $page ) {
46 $this->pageCount++;
47 }
48
49 function handleRevision( $rev ) {
50 $title = $rev->getTitle();
51 if (!$title) {
52 $this->progress( "Got bogus revision with null title!" );
53 return;
54 }
55 #$timestamp = $rev->getTimestamp();
56 #$display = $title->getPrefixedText();
57 #echo "$display $timestamp\n";
58
59 $this->revCount++;
60 $this->report();
61
62 if( !$this->dryRun ) {
63 call_user_func( $this->importCallback, $rev );
64 }
65 }
66
67 function handleUpload( $revision ) {
68 if( $this->uploads ) {
69 $this->uploadCount++;
70 //$this->report();
71 $this->progress( "upload: " . $revision->getFilename() );
72
73 if( !$this->dryRun ) {
74 // bluuuh hack
75 //call_user_func( $this->uploadCallback, $revision );
76 $dbw = wfGetDB( DB_MASTER );
77 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
78 }
79 }
80 }
81
82 function report( $final = false ) {
83 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
84 $this->showReport();
85 }
86 }
87
88 function showReport() {
89 if( $this->reporting ) {
90 $delta = wfTime() - $this->startTime;
91 if( $delta ) {
92 $rate = sprintf("%.2f", $this->pageCount / $delta);
93 $revrate = sprintf("%.2f", $this->revCount / $delta);
94 } else {
95 $rate = '-';
96 $revrate = '-';
97 }
98 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
99 }
100 wfWaitForSlaves(5);
101 }
102
103 function progress( $string ) {
104 fwrite( $this->stderr, $string . "\n" );
105 }
106
107 function importFromFile( $filename ) {
108 if( preg_match( '/\.gz$/', $filename ) ) {
109 $filename = 'compress.zlib://' . $filename;
110 }
111 $file = fopen( $filename, 'rt' );
112 return $this->importFromHandle( $file );
113 }
114
115 function importFromStdin() {
116 $file = fopen( 'php://stdin', 'rt' );
117 return $this->importFromHandle( $file );
118 }
119
120 function importFromHandle( $handle ) {
121 $this->startTime = wfTime();
122
123 $source = new ImportStreamSource( $handle );
124 $importer = new WikiImporter( $source );
125
126 $importer->setDebug( $this->debug );
127 $importer->setPageCallback( array( &$this, 'reportPage' ) );
128 $this->importCallback = $importer->setRevisionCallback(
129 array( &$this, 'handleRevision' ) );
130 $this->uploadCallback = $importer->setUploadCallback(
131 array( &$this, 'handleUpload' ) );
132
133 return $importer->doImport();
134 }
135 }
136
137 if( wfReadOnly() ) {
138 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
139 }
140
141 $reader = new BackupReader();
142 if( isset( $options['quiet'] ) ) {
143 $reader->reporting = false;
144 }
145 if( isset( $options['report'] ) ) {
146 $reader->reportingInterval = intval( $options['report'] );
147 }
148 if( isset( $options['dry-run'] ) ) {
149 $reader->dryRun = true;
150 }
151 if( isset( $options['debug'] ) ) {
152 $reader->debug = true;
153 }
154 if( isset( $options['uploads'] ) ) {
155 $reader->uploads = true; // experimental!
156 }
157
158 if( isset( $args[0] ) ) {
159 $result = $reader->importFromFile( $args[0] );
160 } else {
161 $result = $reader->importFromStdin();
162 }
163
164 if( WikiError::isError( $result ) ) {
165 echo $result->getMessage() . "\n";
166 } else {
167 echo "Done!\n";
168 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
169 echo "the recentchanges page.\n";
170 }
171
172