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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @subpackage Maintenance
25 $optionsWithArgs = array( 'report' );
27 require_once( 'commandLine.inc' );
28 require_once( 'SpecialImport.php' );
31 var $reportingInterval = 100;
32 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 $display = $title->getPrefixedText();
52 $timestamp = $rev->getTimestamp();
53 #echo "$display $timestamp\n";
58 if( !$this->dryRun
) {
59 call_user_func( $this->importCallback
, $rev );
63 function report( $final = false ) {
64 if( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
69 function showReport() {
70 if( $this->reporting
) {
71 $delta = wfTime() - $this->startTime
;
73 $rate = $this->pageCount
/ $delta;
74 $revrate = $this->revCount
/ $delta;
79 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
83 function progress( $string ) {
84 fwrite( $this->stderr
, $string . "\n" );
87 function importFromFile( $filename ) {
88 if( preg_match( '/\.gz$/', $filename ) ) {
89 $filename = 'compress.zlib://' . $filename;
91 $file = fopen( $filename, 'rt' );
92 return $this->importFromHandle( $file );
95 function importFromStdin() {
96 $file = fopen( 'php://stdin', 'rt' );
97 return $this->importFromHandle( $file );
100 function importFromHandle( $handle ) {
101 $this->startTime
= wfTime();
103 $source = new ImportStreamSource( $handle );
104 $importer = new WikiImporter( $source );
106 $importer->setPageCallback( array( &$this, 'reportPage' ) );
107 $this->importCallback
= $importer->setRevisionCallback(
108 array( &$this, 'handleRevision' ) );
110 return $importer->doImport();
115 die( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
118 $reader = new BackupReader();
119 if( isset( $options['quiet'] ) ) {
120 $reader->reporting
= false;
122 if( isset( $options['report'] ) ) {
123 $reader->reportingInterval
= intval( $options['report'] );
125 if( isset( $options['dry-run'] ) ) {
126 $reader->dryRun
= true;
129 if( isset( $args[0] ) ) {
130 $result = $reader->importFromFile( $args[0] );
132 $result = $reader->importFromStdin();
135 if( WikiError
::isError( $result ) ) {
136 echo $result->getMessage() . "\n";