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
21 * @addtogroup Maintenance
24 $optionsWithArgs = array( 'report' );
26 require_once( 'commandLine.inc' );
29 var $reportingInterval = 100;
30 var $reporting = true;
37 function BackupReader() {
38 $this->stderr
= fopen( "php://stderr", "wt" );
41 function reportPage( $page ) {
45 function handleRevision( $rev ) {
46 $title = $rev->getTitle();
48 $this->progress( "Got bogus revision with null title!" );
51 #$timestamp = $rev->getTimestamp();
52 #$display = $title->getPrefixedText();
53 #echo "$display $timestamp\n";
58 if( !$this->dryRun
) {
59 call_user_func( $this->importCallback
, $rev );
63 function handleUpload( $revision ) {
64 if( $this->uploads
) {
67 $this->progress( "upload: " . $revision->getFilename() );
69 if( !$this->dryRun
) {
71 //call_user_func( $this->uploadCallback, $revision );
72 $dbw = wfGetDB( DB_MASTER
);
73 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
78 function report( $final = false ) {
79 if( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
84 function showReport() {
85 if( $this->reporting
) {
86 $delta = wfTime() - $this->startTime
;
88 $rate = sprintf("%.2f", $this->pageCount
/ $delta);
89 $revrate = sprintf("%.2f", $this->revCount
/ $delta);
94 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
98 function progress( $string ) {
99 fwrite( $this->stderr
, $string . "\n" );
102 function importFromFile( $filename ) {
103 if( preg_match( '/\.gz$/', $filename ) ) {
104 $filename = 'compress.zlib://' . $filename;
106 $file = fopen( $filename, 'rt' );
107 return $this->importFromHandle( $file );
110 function importFromStdin() {
111 $file = fopen( 'php://stdin', 'rt' );
112 return $this->importFromHandle( $file );
115 function importFromHandle( $handle ) {
116 $this->startTime
= wfTime();
118 $source = new ImportStreamSource( $handle );
119 $importer = new WikiImporter( $source );
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' ) );
128 return $importer->doImport();
133 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
136 $reader = new BackupReader();
137 if( isset( $options['quiet'] ) ) {
138 $reader->reporting
= false;
140 if( isset( $options['report'] ) ) {
141 $reader->reportingInterval
= intval( $options['report'] );
143 if( isset( $options['dry-run'] ) ) {
144 $reader->dryRun
= true;
146 if( isset( $options['debug'] ) ) {
147 $reader->debug
= true;
149 if( isset( $options['uploads'] ) ) {
150 $reader->uploads
= true; // experimental!
153 if( isset( $args[0] ) ) {
154 $result = $reader->importFromFile( $args[0] );
156 $result = $reader->importFromStdin();
159 if( WikiError
::isError( $result ) ) {
160 echo $result->getMessage() . "\n";
163 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
164 echo "the recentchanges page.\n";