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' );
27 require_once( 'SpecialImport.php' );
30 var $reportingInterval = 100;
31 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 report( $final = false ) {
63 if( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
68 function showReport() {
69 if( $this->reporting
) {
70 $delta = wfTime() - $this->startTime
;
72 $rate = $this->pageCount
/ $delta;
73 $revrate = $this->revCount
/ $delta;
78 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
82 function progress( $string ) {
83 fwrite( $this->stderr
, $string . "\n" );
86 function importFromFile( $filename ) {
87 if( preg_match( '/\.gz$/', $filename ) ) {
88 $filename = 'compress.zlib://' . $filename;
90 $file = fopen( $filename, 'rt' );
91 return $this->importFromHandle( $file );
94 function importFromStdin() {
95 $file = fopen( 'php://stdin', 'rt' );
96 return $this->importFromHandle( $file );
99 function importFromHandle( $handle ) {
100 $this->startTime
= wfTime();
102 $source = new ImportStreamSource( $handle );
103 $importer = new WikiImporter( $source );
105 $importer->setPageCallback( array( &$this, 'reportPage' ) );
106 $this->importCallback
= $importer->setRevisionCallback(
107 array( &$this, 'handleRevision' ) );
109 return $importer->doImport();
114 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
117 $reader = new BackupReader();
118 if( isset( $options['quiet'] ) ) {
119 $reader->reporting
= false;
121 if( isset( $options['report'] ) ) {
122 $reader->reportingInterval
= intval( $options['report'] );
124 if( isset( $options['dry-run'] ) ) {
125 $reader->dryRun
= true;
128 if( isset( $args[0] ) ) {
129 $result = $reader->importFromFile( $args[0] );
131 $result = $reader->importFromStdin();
134 if( WikiError
::isError( $result ) ) {
135 echo $result->getMessage() . "\n";
138 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
139 echo "the recentchanges page.";