Ugly bug 24375 hack
[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', 'namespaces' );
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 var $nsFilter = false;
42
43 function __construct() {
44 $this->stderr = fopen( "php://stderr", "wt" );
45 }
46
47 function setNsfilter( array $namespaces ) {
48 if ( count( $namespaces ) == 0 ) {
49 $this->nsFilter = false;
50 return;
51 }
52 $this->nsFilter = array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
53 }
54
55 private function getNsIndex( $namespace ) {
56 global $wgContLang;
57 if ( ( $result = $wgContLang->getNsIndex( $namespace ) ) !== false ) {
58 return $result;
59 }
60 $ns = intval( $namespace );
61 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
62 return $ns;
63 }
64 wfDie( "Unknown namespace text / index specified: $namespace\n" );
65 }
66
67 private function skippedNamespace( $obj ) {
68 if ( $obj instanceof Title ) {
69 $ns = $obj->getNamespace();
70 } elseif ( $obj instanceof Revision ) {
71 $ns = $obj->getTitle()->getNamespace();
72 } elseif ( $obj instanceof WikiRevision ) {
73 $ns = $obj->title->getNamespace();
74 } else {
75 echo wfBacktrace();
76 wfDie( "Cannot get namespace of object in " . __METHOD__ . "\n" );
77 }
78 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
79 }
80
81 function reportPage( $page ) {
82 $this->pageCount++;
83 }
84
85 function handleRevision( $rev ) {
86 $title = $rev->getTitle();
87 if ( !$title ) {
88 $this->progress( "Got bogus revision with null title!" );
89 return;
90 }
91
92 if ( $this->skippedNamespace( $title ) ) {
93 return;
94 }
95
96 $this->revCount++;
97 $this->report();
98
99 if ( !$this->dryRun ) {
100 call_user_func( $this->importCallback, $rev );
101 }
102 }
103
104 function handleUpload( $revision ) {
105 if ( $this->uploads ) {
106 if ( $this->skippedNamespace( $revision ) ) {
107 return;
108 }
109 $this->uploadCount++;
110 // $this->report();
111 $this->progress( "upload: " . $revision->getFilename() );
112
113 if ( !$this->dryRun ) {
114 // bluuuh hack
115 // call_user_func( $this->uploadCallback, $revision );
116 $dbw = wfGetDB( DB_MASTER );
117 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
118 }
119 }
120 }
121
122 function handleLogItem( $rev ) {
123 if ( $this->skippedNamespace( $rev ) ) {
124 return;
125 }
126 $this->revCount++;
127 $this->report();
128
129 if ( !$this->dryRun ) {
130 call_user_func( $this->logItemCallback, $rev );
131 }
132 }
133
134 function report( $final = false ) {
135 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
136 $this->showReport();
137 }
138 }
139
140 function showReport() {
141 if ( $this->reporting ) {
142 $delta = wfTime() - $this->startTime;
143 if ( $delta ) {
144 $rate = sprintf( "%.2f", $this->pageCount / $delta );
145 $revrate = sprintf( "%.2f", $this->revCount / $delta );
146 } else {
147 $rate = '-';
148 $revrate = '-';
149 }
150 # Logs dumps don't have page tallies
151 if ( $this->pageCount ) {
152 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
153 } else {
154 $this->progress( "$this->revCount ($revrate revs/sec)" );
155 }
156 }
157 wfWaitForSlaves( 5 );
158 // XXX: Don't let deferred jobs array get absurdly large (bug 24375)
159 wfDoUpdates( 'commit' );
160 }
161
162 function progress( $string ) {
163 fwrite( $this->stderr, $string . "\n" );
164 }
165
166 function importFromFile( $filename ) {
167 if ( preg_match( '/\.gz$/', $filename ) ) {
168 $filename = 'compress.zlib://' . $filename;
169 }
170 elseif ( preg_match( '/\.bz2$/', $filename ) ) {
171 $filename = 'compress.bzip2://' . $filename;
172 }
173 elseif ( preg_match( '/\.7z$/', $filename ) ) {
174 $filename = 'mediawiki.compress.7z://' . $filename;
175 }
176
177 $file = fopen( $filename, 'rt' );
178 return $this->importFromHandle( $file );
179 }
180
181 function importFromStdin() {
182 $file = fopen( 'php://stdin', 'rt' );
183 if( posix_isatty( $file ) ) {
184 $this->showHelp();
185 exit();
186 }
187 return $this->importFromHandle( $file );
188 }
189
190 function importFromHandle( $handle ) {
191 $this->startTime = wfTime();
192
193 $source = new ImportStreamSource( $handle );
194 $importer = new WikiImporter( $source );
195
196 $importer->setDebug( $this->debug );
197 $importer->setPageCallback( array( &$this, 'reportPage' ) );
198 $this->importCallback = $importer->setRevisionCallback(
199 array( &$this, 'handleRevision' ) );
200 $this->uploadCallback = $importer->setUploadCallback(
201 array( &$this, 'handleUpload' ) );
202 $this->logItemCallback = $importer->setLogItemCallback(
203 array( &$this, 'handleLogItem' ) );
204
205 if ( $this->dryRun ) {
206 $importer->setPageOutCallback( null );
207 }
208
209 return $importer->doImport();
210 }
211
212 function showHelp() {
213 $gz = in_array('compress.zlib', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP zlib module)';
214 $bz2 = in_array('compress.bzip2', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP bzip2 module)';
215 echo "This script reads pages from an XML file as produced from Special:Export\n";
216 echo "or dumpBackup.php, and saves them into the current wiki.\n";
217 echo "\n";
218 echo "Note that for very large data sets, importDump.php may be slow; there are\n";
219 echo "alternate methods which can be much faster for full site restoration:\n";
220 echo "http://www.mediawiki.org/wiki/Manual:Importing_XML_dumps\n";
221 echo "\n";
222 echo "Usage: php importDump.php [<options>] [<file>]\n";
223 echo "If no file is listed, input may be piped from stdin.\n";
224 echo "\n";
225 echo "Options:\n";
226 echo " --quiet Don't dump status reports to stderr.\n";
227 echo " --report=n Report position and speed after every n pages processed.\n";
228 echo " --namespaces=a|b|..|z Import only the pages from namespaces belonging to\n";
229 echo " the list of pipe-separated namespace names or namespace indexes\n";
230 echo " --dry-run Parse dump without actually importing pages.\n";
231 echo " --debug Output extra verbose debug information\n";
232 echo " --uploads Process file upload data if included (experimental)\n";
233 echo "\n";
234 echo "Compressed XML files may be read directly:\n";
235 echo " .gz $gz\n";
236 echo " .bz2 $bz2\n";
237 echo " .7z (if 7za executable is in PATH)\n";
238 echo "\n";
239 }
240 }
241
242 if ( wfReadOnly() ) {
243 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
244 }
245
246 $reader = new BackupReader();
247 if ( isset( $options['quiet'] ) ) {
248 $reader->reporting = false;
249 }
250 if ( isset( $options['report'] ) ) {
251 $reader->reportingInterval = intval( $options['report'] );
252 }
253 if ( isset( $options['dry-run'] ) ) {
254 $reader->dryRun = true;
255 }
256 if ( isset( $options['debug'] ) ) {
257 $reader->debug = true;
258 }
259 if ( isset( $options['uploads'] ) ) {
260 $reader->uploads = true; // experimental!
261 }
262 if ( isset( $options['namespaces'] ) ) {
263 $reader->setNsfilter( explode( '|', $options['namespaces'] ) );
264 }
265
266 if ( isset( $options['help'] ) ) {
267 $reader->showHelp();
268 exit();
269 } elseif ( isset( $args[0] ) ) {
270 $result = $reader->importFromFile( $args[0] );
271 } else {
272 $result = $reader->importFromStdin();
273 }
274
275 echo "Done!\n";
276 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
277 echo "the recentchanges page.\n";