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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
25 $optionsWithArgs = array( 'report' );
27 require_once( dirname(__FILE__
) . '/commandLine.inc' );
28 require_once( '7zip.inc' );
31 * @ingroup Maintenance
34 var $reportingInterval = 100;
35 var $reporting = true;
42 function BackupReader() {
43 $this->stderr
= fopen( "php://stderr", "wt" );
46 function reportPage( $page ) {
50 function handleRevision( $rev ) {
51 $title = $rev->getTitle();
53 $this->progress( "Got bogus revision with null title!" );
60 if( !$this->dryRun
) {
61 call_user_func( $this->importCallback
, $rev );
65 function handleUpload( $revision ) {
66 if( $this->uploads
) {
69 $this->progress( "upload: " . $revision->getFilename() );
71 if( !$this->dryRun
) {
73 //call_user_func( $this->uploadCallback, $revision );
74 $dbw = wfGetDB( DB_MASTER
);
75 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
80 function handleLogItem( $rev ) {
84 if( !$this->dryRun
) {
85 call_user_func( $this->logItemCallback
, $rev );
89 function report( $final = false ) {
90 if( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
95 function showReport() {
96 if( $this->reporting
) {
97 $delta = wfTime() - $this->startTime
;
99 $rate = sprintf("%.2f", $this->pageCount
/ $delta);
100 $revrate = sprintf("%.2f", $this->revCount
/ $delta);
105 # Logs dumps don't have page tallies
106 if( $this->pageCount
)
107 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
109 $this->progress( "$this->revCount ($revrate revs/sec)" );
114 function progress( $string ) {
115 fwrite( $this->stderr
, $string . "\n" );
118 function importFromFile( $filename ) {
120 if( preg_match( '/\.gz$/', $filename ) ) {
121 $filename = 'compress.zlib://' . $filename;
123 elseif( preg_match( '/\.bz2$/', $filename ) ) {
124 $filename = 'compress.bzip2://' . $filename;
126 elseif( preg_match( '/\.7z$/', $filename ) ) {
127 $filename = 'mediawiki.compress.7z://' . $filename;
131 $file = fopen( $filename, $t ?
'rt' : 't' ); //our 7zip wrapper uses popen, which seems not to like two-letter modes
132 return $this->importFromHandle( $file );
135 function importFromStdin() {
136 $file = fopen( 'php://stdin', 'rt' );
137 return $this->importFromHandle( $file );
140 function importFromHandle( $handle ) {
141 $this->startTime
= wfTime();
143 $source = new ImportStreamSource( $handle );
144 $importer = new WikiImporter( $source );
146 $importer->setDebug( $this->debug
);
147 $importer->setPageCallback( array( &$this, 'reportPage' ) );
148 $this->importCallback
= $importer->setRevisionCallback(
149 array( &$this, 'handleRevision' ) );
150 $this->uploadCallback
= $importer->setUploadCallback(
151 array( &$this, 'handleUpload' ) );
152 $this->logItemCallback
= $importer->setLogItemCallback(
153 array( &$this, 'handleLogItem' ) );
155 return $importer->doImport();
160 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
163 $reader = new BackupReader();
164 if( isset( $options['quiet'] ) ) {
165 $reader->reporting
= false;
167 if( isset( $options['report'] ) ) {
168 $reader->reportingInterval
= intval( $options['report'] );
170 if( isset( $options['dry-run'] ) ) {
171 $reader->dryRun
= true;
173 if( isset( $options['debug'] ) ) {
174 $reader->debug
= true;
176 if( isset( $options['uploads'] ) ) {
177 $reader->uploads
= true; // experimental!
180 if( isset( $args[0] ) ) {
181 $result = $reader->importFromFile( $args[0] );
183 $result = $reader->importFromStdin();
186 if( WikiError
::isError( $result ) ) {
187 echo $result->getMessage() . "\n";
190 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
191 echo "the recentchanges page.\n";