3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
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.
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.
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
22 * @ingroup Maintenance
25 $optionsWithArgs = array( 'report', 'namespaces' );
27 require_once( dirname( __FILE__
) . '/commandLine.inc' );
30 * @ingroup Maintenance
31 * @todo port to Maintenance class
34 var $reportingInterval = 100;
35 var $reporting = true;
41 var $nsFilter = false;
43 function __construct() {
44 $this->stderr
= fopen( "php://stderr", "wt" );
47 function setNsfilter( array $namespaces ) {
48 if ( count( $namespaces ) == 0 ) {
49 $this->nsFilter
= false;
52 $this->nsFilter
= array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
55 private function getNsIndex( $namespace ) {
57 if ( ( $result = $wgContLang->getNsIndex( $namespace ) ) !== false ) {
60 $ns = intval( $namespace );
61 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
64 wfDie( "Unknown namespace text / index specified: $namespace\n" );
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();
76 wfDie( "Cannot get namespace of object in " . __METHOD__
. "\n" );
78 return is_array( $this->nsFilter
) && !in_array( $ns, $this->nsFilter
);
81 function reportPage( $page ) {
85 function handleRevision( $rev ) {
86 $title = $rev->getTitle();
88 $this->progress( "Got bogus revision with null title!" );
92 if ( $this->skippedNamespace( $title ) ) {
99 if ( !$this->dryRun
) {
100 call_user_func( $this->importCallback
, $rev );
104 function handleUpload( $revision ) {
105 if ( $this->uploads
) {
106 if ( $this->skippedNamespace( $revision ) ) {
109 $this->uploadCount++
;
111 $this->progress( "upload: " . $revision->getFilename() );
113 if ( !$this->dryRun
) {
115 // call_user_func( $this->uploadCallback, $revision );
116 $dbw = wfGetDB( DB_MASTER
);
117 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
122 function handleLogItem( $rev ) {
123 if ( $this->skippedNamespace( $rev ) ) {
129 if ( !$this->dryRun
) {
130 call_user_func( $this->logItemCallback
, $rev );
134 function report( $final = false ) {
135 if ( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
140 function showReport() {
141 if ( $this->reporting
) {
142 $delta = wfTime() - $this->startTime
;
144 $rate = sprintf( "%.2f", $this->pageCount
/ $delta );
145 $revrate = sprintf( "%.2f", $this->revCount
/ $delta );
150 # Logs dumps don't have page tallies
151 if ( $this->pageCount
) {
152 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
154 $this->progress( "$this->revCount ($revrate revs/sec)" );
157 wfWaitForSlaves( 5 );
158 // XXX: Don't let deferred jobs array get absurdly large (bug 24375)
159 wfDoUpdates( 'commit' );
162 function progress( $string ) {
163 fwrite( $this->stderr
, $string . "\n" );
166 function importFromFile( $filename ) {
167 if ( preg_match( '/\.gz$/', $filename ) ) {
168 $filename = 'compress.zlib://' . $filename;
170 elseif ( preg_match( '/\.bz2$/', $filename ) ) {
171 $filename = 'compress.bzip2://' . $filename;
173 elseif ( preg_match( '/\.7z$/', $filename ) ) {
174 $filename = 'mediawiki.compress.7z://' . $filename;
177 $file = fopen( $filename, 'rt' );
178 return $this->importFromHandle( $file );
181 function importFromStdin() {
182 $file = fopen( 'php://stdin', 'rt' );
183 if( posix_isatty( $file ) ) {
187 return $this->importFromHandle( $file );
190 function importFromHandle( $handle ) {
191 $this->startTime
= wfTime();
193 $source = new ImportStreamSource( $handle );
194 $importer = new WikiImporter( $source );
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' ) );
205 if ( $this->dryRun
) {
206 $importer->setPageOutCallback( null );
209 return $importer->doImport();
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";
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";
222 echo "Usage: php importDump.php [<options>] [<file>]\n";
223 echo "If no file is listed, input may be piped from stdin.\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";
234 echo "Compressed XML files may be read directly:\n";
237 echo " .7z (if 7za executable is in PATH)\n";
242 if ( wfReadOnly() ) {
243 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
246 $reader = new BackupReader();
247 if ( isset( $options['quiet'] ) ) {
248 $reader->reporting
= false;
250 if ( isset( $options['report'] ) ) {
251 $reader->reportingInterval
= intval( $options['report'] );
253 if ( isset( $options['dry-run'] ) ) {
254 $reader->dryRun
= true;
256 if ( isset( $options['debug'] ) ) {
257 $reader->debug
= true;
259 if ( isset( $options['uploads'] ) ) {
260 $reader->uploads
= true; // experimental!
262 if ( isset( $options['namespaces'] ) ) {
263 $reader->setNsfilter( explode( '|', $options['namespaces'] ) );
266 if ( isset( $options['help'] ) ) {
269 } elseif ( isset( $args[0] ) ) {
270 $result = $reader->importFromFile( $args[0] );
272 $result = $reader->importFromStdin();
276 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
277 echo "the recentchanges page.\n";