* Rewrote ObjectCache.php to conform to the modern coding style, and to be less convo...
[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( dirname( __FILE__ ) . '/commandLine.inc' );
28
29 /**
30 * @ingroup Maintenance
31 * @todo port to Maintenance class
32 */
33 class BackupReader {
34 var $reportingInterval = 100;
35 var $reporting = true;
36 var $pageCount = 0;
37 var $revCount = 0;
38 var $dryRun = false;
39 var $debug = false;
40 var $uploads = false;
41
42 function __construct() {
43 $this->stderr = fopen( "php://stderr", "wt" );
44 }
45
46 function reportPage( $page ) {
47 $this->pageCount++;
48 }
49
50 function handleRevision( $rev ) {
51 $title = $rev->getTitle();
52 if ( !$title ) {
53 $this->progress( "Got bogus revision with null title!" );
54 return;
55 }
56
57 $this->revCount++;
58 $this->report();
59
60 if ( !$this->dryRun ) {
61 call_user_func( $this->importCallback, $rev );
62 }
63 }
64
65 function handleUpload( $revision ) {
66 if ( $this->uploads ) {
67 $this->uploadCount++;
68 // $this->report();
69 $this->progress( "upload: " . $revision->getFilename() );
70
71 if ( !$this->dryRun ) {
72 // bluuuh hack
73 // call_user_func( $this->uploadCallback, $revision );
74 $dbw = wfGetDB( DB_MASTER );
75 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
76 }
77 }
78 }
79
80 function handleLogItem( $rev ) {
81 $this->revCount++;
82 $this->report();
83
84 if ( !$this->dryRun ) {
85 call_user_func( $this->logItemCallback, $rev );
86 }
87 }
88
89 function report( $final = false ) {
90 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
91 $this->showReport();
92 }
93 }
94
95 function showReport() {
96 if ( $this->reporting ) {
97 $delta = wfTime() - $this->startTime;
98 if ( $delta ) {
99 $rate = sprintf( "%.2f", $this->pageCount / $delta );
100 $revrate = sprintf( "%.2f", $this->revCount / $delta );
101 } else {
102 $rate = '-';
103 $revrate = '-';
104 }
105 # Logs dumps don't have page tallies
106 if ( $this->pageCount )
107 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
108 else
109 $this->progress( "$this->revCount ($revrate revs/sec)" );
110 }
111 wfWaitForSlaves( 5 );
112 }
113
114 function progress( $string ) {
115 fwrite( $this->stderr, $string . "\n" );
116 }
117
118 function importFromFile( $filename ) {
119 if ( preg_match( '/\.gz$/', $filename ) ) {
120 $filename = 'compress.zlib://' . $filename;
121 }
122 elseif ( preg_match( '/\.bz2$/', $filename ) ) {
123 $filename = 'compress.bzip2://' . $filename;
124 }
125 elseif ( preg_match( '/\.7z$/', $filename ) ) {
126 $filename = 'mediawiki.compress.7z://' . $filename;
127 }
128
129 $file = fopen( $filename, 'rt' );
130 return $this->importFromHandle( $file );
131 }
132
133 function importFromStdin() {
134 $file = fopen( 'php://stdin', 'rt' );
135 if( posix_isatty( $file ) ) {
136 $this->showHelp();
137 exit();
138 }
139 return $this->importFromHandle( $file );
140 }
141
142 function importFromHandle( $handle ) {
143 $this->startTime = wfTime();
144
145 $source = new ImportStreamSource( $handle );
146 $importer = new WikiImporter( $source );
147
148 $importer->setDebug( $this->debug );
149 $importer->setPageCallback( array( &$this, 'reportPage' ) );
150 $this->importCallback = $importer->setRevisionCallback(
151 array( &$this, 'handleRevision' ) );
152 $this->uploadCallback = $importer->setUploadCallback(
153 array( &$this, 'handleUpload' ) );
154 $this->logItemCallback = $importer->setLogItemCallback(
155 array( &$this, 'handleLogItem' ) );
156
157 if ( $this->dryRun ) {
158 $importer->setPageOutCallback( null );
159 }
160
161 return $importer->doImport();
162 }
163
164 function showHelp() {
165 $gz = in_array('compress.zlib', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP zlib module)';
166 $bz2 = in_array('compress.bzip2', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP bzip2 module)';
167 echo "This script reads pages from an XML file as produced from Special:Export\n";
168 echo "or dumpBackup.php, and saves them into the current wiki.\n";
169 echo "\n";
170 echo "Note that for very large data sets, importDump.php may be slow; there are\n";
171 echo "alternate methods which can be much faster for full site restoration:\n";
172 echo "http://www.mediawiki.org/wiki/Manual:Importing_XML_dumps\n";
173 echo "\n";
174 echo "Usage: php importDump.php [<options>] [<file>]\n";
175 echo "If no file is listed, input may be piped from stdin.\n";
176 echo "\n";
177 echo "Options:\n";
178 echo " --quiet Don't dump status reports to stderr.\n";
179 echo " --report=n Report position and speed after every n pages processed.\n";
180 echo " --dry-run Parse dump without actually importing pages.\n";
181 echo " --debug Output extra verbose debug information\n";
182 echo " --uploads Process file upload data if included (experimental)\n";
183 echo "\n";
184 echo "Compressed XML files may be read directly:\n";
185 echo " .gz $gz\n";
186 echo " .bz2 $bz2\n";
187 echo " .7z (if 7za executable is in PATH)\n";
188 echo "\n";
189 }
190 }
191
192 if ( wfReadOnly() ) {
193 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
194 }
195
196 $reader = new BackupReader();
197 if ( isset( $options['quiet'] ) ) {
198 $reader->reporting = false;
199 }
200 if ( isset( $options['report'] ) ) {
201 $reader->reportingInterval = intval( $options['report'] );
202 }
203 if ( isset( $options['dry-run'] ) ) {
204 $reader->dryRun = true;
205 }
206 if ( isset( $options['debug'] ) ) {
207 $reader->debug = true;
208 }
209 if ( isset( $options['uploads'] ) ) {
210 $reader->uploads = true; // experimental!
211 }
212
213 if ( isset( $options['help'] ) ) {
214 $reader->showHelp();
215 exit();
216 } elseif ( isset( $args[0] ) ) {
217 $result = $reader->importFromFile( $args[0] );
218 } else {
219 $result = $reader->importFromStdin();
220 }
221
222 echo "Done!\n";
223 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
224 echo "the recentchanges page.\n";