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