* Special:Import/importDump fixes: report XML parse errors, accept <minor/>
[mediawiki.git] / maintenance / importDump.php
blob15110ffd21bdf2172c75ba676deaacb3c7d9e350
1 <?php
2 /**
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @package MediaWiki
22 * @subpackage Maintenance
25 $optionsWithArgs = array( 'report' );
27 require_once( 'commandLine.inc' );
28 require_once( 'SpecialImport.php' );
30 class BackupReader {
31 var $reportingInterval = 100;
32 var $reporting = true;
33 var $pageCount = 0;
34 var $revCount = 0;
35 var $dryRun = false;
37 function BackupReader() {
38 $this->stderr = fopen( "php://stderr", "wt" );
41 function reportPage( $page ) {
42 $this->pageCount++;
45 function handleRevision( $rev ) {
46 $title = $rev->getTitle();
47 if (!$title) {
48 return;
50 $display = $title->getPrefixedText();
51 $timestamp = $rev->getTimestamp();
52 #echo "$display $timestamp\n";
54 $this->revCount++;
55 $this->report();
57 if( !$this->dryRun ) {
58 call_user_func( $this->importCallback, $rev );
62 function report( $final = false ) {
63 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
64 $this->showReport();
68 function showReport() {
69 if( $this->reporting ) {
70 $delta = wfTime() - $this->startTime;
71 if( $delta ) {
72 $rate = $this->pageCount / $delta;
73 $revrate = $this->revCount / $delta;
74 } else {
75 $rate = '-';
76 $revrate = '-';
78 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
82 function progress( $string ) {
83 fwrite( $this->stderr, $string . "\n" );
86 function importFromFile( $filename ) {
87 if( preg_match( '/\.gz$/', $filename ) ) {
88 $filename = 'compress.zlib://' . $filename;
90 $file = fopen( $filename, 'rt' );
91 return $this->importFromHandle( $file );
94 function importFromStdin() {
95 $file = fopen( 'php://stdin', 'rt' );
96 return $this->importFromHandle( $file );
99 function importFromHandle( $handle ) {
100 $this->startTime = wfTime();
102 $source = new ImportStreamSource( $handle );
103 $importer = new WikiImporter( $source );
105 $importer->setPageCallback( array( &$this, 'reportPage' ) );
106 $this->importCallback = $importer->setRevisionCallback(
107 array( &$this, 'handleRevision' ) );
109 return $importer->doImport();
113 $reader = new BackupReader();
114 if( isset( $options['quiet'] ) ) {
115 $reader->reporting = false;
117 if( isset( $options['report'] ) ) {
118 $reader->reportingInterval = intval( $options['report'] );
120 if( isset( $options['dry-run'] ) ) {
121 $reader->dryRun = true;
124 if( isset( $args[0] ) ) {
125 $result = $reader->importFromFile( $args[0] );
126 } else {
127 $result = $reader->importFromStdin();
130 if( WikiError::isError( $result ) ) {
131 echo $result->getMessage() . "\n";
132 } else {
133 echo "Done!\n";