* Special:Import/importDump fixes: report XML parse errors, accept <minor/>
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @package MediaWiki
22 * @subpackage Maintenance
23 */
24
25 $optionsWithArgs = array( 'report' );
26
27 require_once( 'commandLine.inc' );
28 require_once( 'SpecialImport.php' );
29
30 class BackupReader {
31 var $reportingInterval = 100;
32 var $reporting = true;
33 var $pageCount = 0;
34 var $revCount = 0;
35 var $dryRun = false;
36
37 function BackupReader() {
38 $this->stderr = fopen( "php://stderr", "wt" );
39 }
40
41 function reportPage( $page ) {
42 $this->pageCount++;
43 }
44
45 function handleRevision( $rev ) {
46 $title = $rev->getTitle();
47 if (!$title) {
48 return;
49 }
50 $display = $title->getPrefixedText();
51 $timestamp = $rev->getTimestamp();
52 #echo "$display $timestamp\n";
53
54 $this->revCount++;
55 $this->report();
56
57 if( !$this->dryRun ) {
58 call_user_func( $this->importCallback, $rev );
59 }
60 }
61
62 function report( $final = false ) {
63 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
64 $this->showReport();
65 }
66 }
67
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 = '-';
77 }
78 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
79 }
80 }
81
82 function progress( $string ) {
83 fwrite( $this->stderr, $string . "\n" );
84 }
85
86 function importFromFile( $filename ) {
87 if( preg_match( '/\.gz$/', $filename ) ) {
88 $filename = 'compress.zlib://' . $filename;
89 }
90 $file = fopen( $filename, 'rt' );
91 return $this->importFromHandle( $file );
92 }
93
94 function importFromStdin() {
95 $file = fopen( 'php://stdin', 'rt' );
96 return $this->importFromHandle( $file );
97 }
98
99 function importFromHandle( $handle ) {
100 $this->startTime = wfTime();
101
102 $source = new ImportStreamSource( $handle );
103 $importer = new WikiImporter( $source );
104
105 $importer->setPageCallback( array( &$this, 'reportPage' ) );
106 $this->importCallback = $importer->setRevisionCallback(
107 array( &$this, 'handleRevision' ) );
108
109 return $importer->doImport();
110 }
111 }
112
113 $reader = new BackupReader();
114 if( isset( $options['quiet'] ) ) {
115 $reader->reporting = false;
116 }
117 if( isset( $options['report'] ) ) {
118 $reader->reportingInterval = intval( $options['report'] );
119 }
120 if( isset( $options['dry-run'] ) ) {
121 $reader->dryRun = true;
122 }
123
124 if( isset( $args[0] ) ) {
125 $result = $reader->importFromFile( $args[0] );
126 } else {
127 $result = $reader->importFromStdin();
128 }
129
130 if( WikiError::isError( $result ) ) {
131 echo $result->getMessage() . "\n";
132 } else {
133 echo "Done!\n";
134 }
135
136 ?>