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;
36 function BackupReader() {
37 $this->stderr
= fopen( "php://stderr", "wt" );
40 function reportPage( $page ) {
44 function handleRevision( $rev ) {
45 $title = $rev->getTitle();
47 $this->progress( "Got bogus revision with null title!" );
50 #$timestamp = $rev->getTimestamp();
51 #$display = $title->getPrefixedText();
52 #echo "$display $timestamp\n";
57 if( !$this->dryRun
) {
58 call_user_func( $this->importCallback
, $rev );
62 function handleUpload( $data ) {
65 $this->progress( "upload: " . $data->getFilename() );
67 if( !$this->dryRun
) {
68 call_user_func( $this->uploadCallback
, $data );
72 function report( $final = false ) {
73 if( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
78 function showReport() {
79 if( $this->reporting
) {
80 $delta = wfTime() - $this->startTime
;
82 $rate = sprintf("%.2f", $this->pageCount
/ $delta);
83 $revrate = sprintf("%.2f", $this->revCount
/ $delta);
88 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
92 function progress( $string ) {
93 fwrite( $this->stderr
, $string . "\n" );
96 function importFromFile( $filename ) {
97 if( preg_match( '/\.gz$/', $filename ) ) {
98 $filename = 'compress.zlib://' . $filename;
100 $file = fopen( $filename, 'rt' );
101 return $this->importFromHandle( $file );
104 function importFromStdin() {
105 $file = fopen( 'php://stdin', 'rt' );
106 return $this->importFromHandle( $file );
109 function importFromHandle( $handle ) {
110 $this->startTime
= wfTime();
112 $source = new ImportStreamSource( $handle );
113 $importer = new WikiImporter( $source );
115 $importer->setDebug( $this->debug
);
116 $importer->setPageCallback( array( &$this, 'reportPage' ) );
117 $this->importCallback
= $importer->setRevisionCallback(
118 array( &$this, 'handleRevision' ) );
119 $this->uploadCallback
= $importer->setUploadCallback(
120 array( &$this, 'handleUpload' ) );
122 return $importer->doImport();
127 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
130 $reader = new BackupReader();
131 if( isset( $options['quiet'] ) ) {
132 $reader->reporting
= false;
134 if( isset( $options['report'] ) ) {
135 $reader->reportingInterval
= intval( $options['report'] );
137 if( isset( $options['dry-run'] ) ) {
138 $reader->dryRun
= true;
140 if( isset( $options['debug'] ) ) {
141 $reader->debug
= true;
144 if( isset( $args[0] ) ) {
145 $result = $reader->importFromFile( $args[0] );
147 $result = $reader->importFromStdin();
150 if( WikiError
::isError( $result ) ) {
151 echo $result->getMessage() . "\n";
154 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
155 echo "the recentchanges page.\n";