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