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 $imageBasePath = false;
42 var $nsFilter = false;
44 function __construct() {
45 $this->stderr
= fopen( "php://stderr", "wt" );
48 function setNsfilter( array $namespaces ) {
49 if ( count( $namespaces ) == 0 ) {
50 $this->nsFilter
= false;
53 $this->nsFilter
= array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
56 private function getNsIndex( $namespace ) {
58 if ( ( $result = $wgContLang->getNsIndex( $namespace ) ) !== false ) {
61 $ns = intval( $namespace );
62 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
65 wfDie( "Unknown namespace text / index specified: $namespace\n" );
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();
77 wfDie( "Cannot get namespace of object in " . __METHOD__
. "\n" );
79 return is_array( $this->nsFilter
) && !in_array( $ns, $this->nsFilter
);
82 function reportPage( $page ) {
86 function handleRevision( $rev ) {
87 $title = $rev->getTitle();
89 $this->progress( "Got bogus revision with null title!" );
93 if ( $this->skippedNamespace( $title ) ) {
100 if ( !$this->dryRun
) {
101 call_user_func( $this->importCallback
, $rev );
105 function handleUpload( $revision ) {
106 if ( $this->uploads
) {
107 if ( $this->skippedNamespace( $revision ) ) {
110 $this->uploadCount++
;
112 $this->progress( "upload: " . $revision->getFilename() );
114 if ( !$this->dryRun
) {
116 // call_user_func( $this->uploadCallback, $revision );
117 $dbw = wfGetDB( DB_MASTER
);
118 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
123 function handleLogItem( $rev ) {
124 if ( $this->skippedNamespace( $rev ) ) {
130 if ( !$this->dryRun
) {
131 call_user_func( $this->logItemCallback
, $rev );
135 function report( $final = false ) {
136 if ( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
141 function showReport() {
142 if ( $this->reporting
) {
143 $delta = wfTime() - $this->startTime
;
145 $rate = sprintf( "%.2f", $this->pageCount
/ $delta );
146 $revrate = sprintf( "%.2f", $this->revCount
/ $delta );
151 # Logs dumps don't have page tallies
152 if ( $this->pageCount
) {
153 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
155 $this->progress( "$this->revCount ($revrate revs/sec)" );
159 // XXX: Don't let deferred jobs array get absurdly large (bug 24375)
160 wfDoUpdates( 'commit' );
163 function progress( $string ) {
164 fwrite( $this->stderr
, $string . "\n" );
167 function importFromFile( $filename ) {
168 if ( preg_match( '/\.gz$/', $filename ) ) {
169 $filename = 'compress.zlib://' . $filename;
171 elseif ( preg_match( '/\.bz2$/', $filename ) ) {
172 $filename = 'compress.bzip2://' . $filename;
174 elseif ( preg_match( '/\.7z$/', $filename ) ) {
175 $filename = 'mediawiki.compress.7z://' . $filename;
178 $file = fopen( $filename, 'rt' );
179 return $this->importFromHandle( $file );
182 function importFromStdin() {
183 $file = fopen( 'php://stdin', 'rt' );
184 if( posix_isatty( $file ) ) {
188 return $this->importFromHandle( $file );
191 function importFromHandle( $handle ) {
192 $this->startTime
= wfTime();
194 $source = new ImportStreamSource( $handle );
195 $importer = new WikiImporter( $source );
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 );
208 if ( $this->imageBasePath
) {
209 $importer->setImageBasePath( $this->imageBasePath
);
212 if ( $this->dryRun
) {
213 $importer->setPageOutCallback( null );
216 return $importer->doImport();
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";
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";
229 echo "Usage: php importDump.php [<options>] [<file>]\n";
230 echo "If no file is listed, input may be piped from stdin.\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";
242 echo "Compressed XML files may be read directly:\n";
245 echo " .7z (if 7za executable is in PATH)\n";
250 if ( wfReadOnly() ) {
251 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
254 $reader = new BackupReader();
255 if ( isset( $options['quiet'] ) ) {
256 $reader->reporting
= false;
258 if ( isset( $options['report'] ) ) {
259 $reader->reportingInterval
= intval( $options['report'] );
261 if ( isset( $options['dry-run'] ) ) {
262 $reader->dryRun
= true;
264 if ( isset( $options['debug'] ) ) {
265 $reader->debug
= true;
267 if ( isset( $options['uploads'] ) ) {
268 $reader->uploads
= true; // experimental!
270 if ( isset( $options['image-base-path'] ) ) {
271 $reader->imageBasePath
= $options['image-base-path'];
273 if ( isset( $options['namespaces'] ) ) {
274 $reader->setNsfilter( explode( '|', $options['namespaces'] ) );
277 if ( isset( $options['help'] ) ) {
280 } elseif ( isset( $args[0] ) ) {
281 $result = $reader->importFromFile( $args[0] );
283 $result = $reader->importFromStdin();
287 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
288 echo "the recentchanges page.\n";