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 $options = array( 'full', 'verbose', 'dry-run', 'preserve' );
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();
47 $display = $title->getPrefixedText();
48 $timestamp = $rev->getTimestamp();
49 #echo "$display $timestamp\n";
54 if( !$this->dryRun
) {
55 call_user_func( $this->importCallback
, $rev );
59 function report( $final = false ) {
60 if( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
65 function showReport() {
66 if( $this->reporting
) {
67 $delta = wfTime() - $this->startTime
;
69 $rate = $this->pageCount
/ $delta;
70 $revrate = $this->revCount
/ $delta;
75 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
79 function progress( $string ) {
80 fwrite( $this->stderr
, $string . "\n" );
83 function importFromFile( $filename ) {
84 if( preg_match( '/\.gz$/', $filename ) ) {
85 $filename = 'compress.zlib://' . $filename;
87 $file = fopen( $filename, 'rt' );
88 $this->importFromHandle( $file );
91 function importFromStdin() {
92 $file = fopen( 'php://stdin', 'rt' );
93 $this->importFromHandle( $file );
96 function importFromHandle( $handle ) {
97 $this->startTime
= wfTime();
99 $source = new ImportStreamSource( $handle );
100 $importer = new WikiImporter( $source );
102 $importer->setPageCallback( array( &$this, 'reportPage' ) );
103 $this->importCallback
= $importer->setRevisionCallback(
104 array( &$this, 'handleRevision' ) );
106 $importer->doImport();
110 $reader = new BackupReader();
111 if( isset( $options['quiet'] ) ) {
112 $reader->reporting
= false;
114 if( isset( $options['report'] ) ) {
115 $reader->reportingInterval
= IntVal( $options['report'] );
117 if( isset( $options['dry-run'] ) ) {
118 $reader->dryRun
= true;
121 if( isset( $args[0] ) ) {
122 $reader->importFromFile( $args[0] );
124 $reader->importFromStdin();