]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/importDump.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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( dirname( __FILE__ ) . '/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 __construct() {
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
56                 $this->revCount++;
57                 $this->report();
58
59                 if ( !$this->dryRun ) {
60                         call_user_func( $this->importCallback, $rev );
61                 }
62         }
63
64         function handleUpload( $revision ) {
65                 if ( $this->uploads ) {
66                         $this->uploadCount++;
67                         // $this->report();
68                         $this->progress( "upload: " . $revision->getFilename() );
69
70                         if ( !$this->dryRun ) {
71                                 // bluuuh hack
72                                 // call_user_func( $this->uploadCallback, $revision );
73                                 $dbw = wfGetDB( DB_MASTER );
74                                 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
75                         }
76                 }
77         }
78
79         function handleLogItem( $rev ) {
80                 $this->revCount++;
81                 $this->report();
82
83                 if ( !$this->dryRun ) {
84                         call_user_func( $this->logItemCallback, $rev );
85                 }
86         }
87
88         function report( $final = false ) {
89                 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
90                         $this->showReport();
91                 }
92         }
93
94         function showReport() {
95                 if ( $this->reporting ) {
96                         $delta = wfTime() - $this->startTime;
97                         if ( $delta ) {
98                                 $rate = sprintf( "%.2f", $this->pageCount / $delta );
99                                 $revrate = sprintf( "%.2f", $this->revCount / $delta );
100                         } else {
101                                 $rate = '-';
102                                 $revrate = '-';
103                         }
104                         # Logs dumps don't have page tallies
105                         if ( $this->pageCount )
106                                 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
107                         else
108                                 $this->progress( "$this->revCount ($revrate revs/sec)" );
109                 }
110                 wfWaitForSlaves( 5 );
111         }
112
113         function progress( $string ) {
114                 fwrite( $this->stderr, $string . "\n" );
115         }
116
117         function importFromFile( $filename ) {
118                 if ( preg_match( '/\.gz$/', $filename ) ) {
119                         $filename = 'compress.zlib://' . $filename;
120                 }
121                 elseif ( preg_match( '/\.bz2$/', $filename ) ) {
122                         $filename = 'compress.bzip2://' . $filename;
123                 }
124                 elseif ( preg_match( '/\.7z$/', $filename ) ) {
125                         $filename = 'mediawiki.compress.7z://' . $filename;
126                 }
127
128                 $file = fopen( $filename, 'rt' );
129                 return $this->importFromHandle( $file );
130         }
131
132         function importFromStdin() {
133                 $file = fopen( 'php://stdin', 'rt' );
134                 return $this->importFromHandle( $file );
135         }
136
137         function importFromHandle( $handle ) {
138                 $this->startTime = wfTime();
139
140                 $source = new ImportStreamSource( $handle );
141                 $importer = new WikiImporter( $source );
142
143                 $importer->setDebug( $this->debug );
144                 $importer->setPageCallback( array( &$this, 'reportPage' ) );
145                 $this->importCallback =  $importer->setRevisionCallback(
146                         array( &$this, 'handleRevision' ) );
147                 $this->uploadCallback = $importer->setUploadCallback(
148                         array( &$this, 'handleUpload' ) );
149                 $this->logItemCallback = $importer->setLogItemCallback(
150                         array( &$this, 'handleLogItem' ) );
151
152                 if ( $this->dryRun ) {
153                         $importer->setPageOutCallback( null );
154                 }
155
156                 return $importer->doImport();
157         }
158 }
159
160 if ( wfReadOnly() ) {
161         wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
162 }
163
164 $reader = new BackupReader();
165 if ( isset( $options['quiet'] ) ) {
166         $reader->reporting = false;
167 }
168 if ( isset( $options['report'] ) ) {
169         $reader->reportingInterval = intval( $options['report'] );
170 }
171 if ( isset( $options['dry-run'] ) ) {
172         $reader->dryRun = true;
173 }
174 if ( isset( $options['debug'] ) ) {
175         $reader->debug = true;
176 }
177 if ( isset( $options['uploads'] ) ) {
178         $reader->uploads = true; // experimental!
179 }
180
181 if ( isset( $args[0] ) ) {
182         $result = $reader->importFromFile( $args[0] );
183 } else {
184         $result = $reader->importFromStdin();
185 }
186
187 echo "Done!\n";
188 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
189 echo "the recentchanges page.\n";