Undo previous commit. Schema changes are a no-no without permission first :-)
[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' );
26
27 require_once( 'commandLine.inc' );
28
29 /**
30 * @ingroup Maintenance
31 */
32 class BackupReader {
33 var $reportingInterval = 100;
34 var $reporting = true;
35 var $pageCount = 0;
36 var $revCount = 0;
37 var $dryRun = false;
38 var $debug = false;
39 var $uploads = false;
40
41 function BackupReader() {
42 $this->stderr = fopen( "php://stderr", "wt" );
43 }
44
45 function reportPage( $page ) {
46 $this->pageCount++;
47 }
48
49 function handleRevision( $rev ) {
50 $title = $rev->getTitle();
51 if (!$title) {
52 $this->progress( "Got bogus revision with null title!" );
53 return;
54 }
55 #$timestamp = $rev->getTimestamp();
56 #$display = $title->getPrefixedText();
57 #echo "$display $timestamp\n";
58
59 $this->revCount++;
60 $this->report();
61
62 if( !$this->dryRun ) {
63 call_user_func( $this->importCallback, $rev );
64 }
65 }
66
67 function handleUpload( $revision ) {
68 if( $this->uploads ) {
69 $this->uploadCount++;
70 //$this->report();
71 $this->progress( "upload: " . $revision->getFilename() );
72
73 if( !$this->dryRun ) {
74 // bluuuh hack
75 //call_user_func( $this->uploadCallback, $revision );
76 $dbw = wfGetDB( DB_MASTER );
77 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
78 }
79 }
80 }
81
82 function report( $final = false ) {
83 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
84 $this->showReport();
85 }
86 }
87
88 function showReport() {
89 if( $this->reporting ) {
90 $delta = wfTime() - $this->startTime;
91 if( $delta ) {
92 $rate = sprintf("%.2f", $this->pageCount / $delta);
93 $revrate = sprintf("%.2f", $this->revCount / $delta);
94 } else {
95 $rate = '-';
96 $revrate = '-';
97 }
98 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
99 }
100 }
101
102 function progress( $string ) {
103 fwrite( $this->stderr, $string . "\n" );
104 }
105
106 function importFromFile( $filename ) {
107 if( preg_match( '/\.gz$/', $filename ) ) {
108 $filename = 'compress.zlib://' . $filename;
109 }
110 $file = fopen( $filename, 'rt' );
111 return $this->importFromHandle( $file );
112 }
113
114 function importFromStdin() {
115 $file = fopen( 'php://stdin', 'rt' );
116 return $this->importFromHandle( $file );
117 }
118
119 function importFromHandle( $handle ) {
120 $this->startTime = wfTime();
121
122 $source = new ImportStreamSource( $handle );
123 $importer = new WikiImporter( $source );
124
125 $importer->setDebug( $this->debug );
126 $importer->setPageCallback( array( &$this, 'reportPage' ) );
127 $this->importCallback = $importer->setRevisionCallback(
128 array( &$this, 'handleRevision' ) );
129 $this->uploadCallback = $importer->setUploadCallback(
130 array( &$this, 'handleUpload' ) );
131
132 return $importer->doImport();
133 }
134 }
135
136 if( wfReadOnly() ) {
137 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
138 }
139
140 $reader = new BackupReader();
141 if( isset( $options['quiet'] ) ) {
142 $reader->reporting = false;
143 }
144 if( isset( $options['report'] ) ) {
145 $reader->reportingInterval = intval( $options['report'] );
146 }
147 if( isset( $options['dry-run'] ) ) {
148 $reader->dryRun = true;
149 }
150 if( isset( $options['debug'] ) ) {
151 $reader->debug = true;
152 }
153 if( isset( $options['uploads'] ) ) {
154 $reader->uploads = true; // experimental!
155 }
156
157 if( isset( $args[0] ) ) {
158 $result = $reader->importFromFile( $args[0] );
159 } else {
160 $result = $reader->importFromStdin();
161 }
162
163 if( WikiError::isError( $result ) ) {
164 echo $result->getMessage() . "\n";
165 } else {
166 echo "Done!\n";
167 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
168 echo "the recentchanges page.\n";
169 }
170
171