]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/importDump.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.git] / maintenance / importDump.php
1 <?php
2 /**
3  * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4  * http://www.mediawiki.org/
5  *
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.
10  *
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.
15  *
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
20  *
21  * @addtogroup Maintenance
22  */
23
24 $optionsWithArgs = array( 'report' );
25
26 require_once( 'commandLine.inc' );
27
28 class BackupReader {
29         var $reportingInterval = 100;
30         var $reporting = true;
31         var $pageCount = 0;
32         var $revCount  = 0;
33         var $dryRun    = false;
34
35         function BackupReader() {
36                 $this->stderr = fopen( "php://stderr", "wt" );
37         }
38
39         function reportPage( $page ) {
40                 $this->pageCount++;
41         }
42
43         function handleRevision( $rev ) {
44                 $title = $rev->getTitle();
45                 if (!$title) {
46                         $this->progress( "Got bogus revision with null title!" );
47                         return;
48                 }
49                 #$timestamp = $rev->getTimestamp();
50                 #$display = $title->getPrefixedText();
51                 #echo "$display $timestamp\n";
52
53                 $this->revCount++;
54                 $this->report();
55
56                 if( !$this->dryRun ) {
57                         call_user_func( $this->importCallback, $rev );
58                 }
59         }
60
61         function report( $final = false ) {
62                 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
63                         $this->showReport();
64                 }
65         }
66
67         function showReport() {
68                 if( $this->reporting ) {
69                         $delta = wfTime() - $this->startTime;
70                         if( $delta ) {
71                                 $rate = sprintf("%.2f", $this->pageCount / $delta);
72                                 $revrate = sprintf("%.2f", $this->revCount / $delta);
73                         } else {
74                                 $rate = '-';
75                                 $revrate = '-';
76                         }
77                         $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
78                 }
79         }
80
81         function progress( $string ) {
82                 fwrite( $this->stderr, $string . "\n" );
83         }
84
85         function importFromFile( $filename ) {
86                 if( preg_match( '/\.gz$/', $filename ) ) {
87                         $filename = 'compress.zlib://' . $filename;
88                 }
89                 $file = fopen( $filename, 'rt' );
90                 return $this->importFromHandle( $file );
91         }
92
93         function importFromStdin() {
94                 $file = fopen( 'php://stdin', 'rt' );
95                 return $this->importFromHandle( $file );
96         }
97
98         function importFromHandle( $handle ) {
99                 $this->startTime = wfTime();
100
101                 $source = new ImportStreamSource( $handle );
102                 $importer = new WikiImporter( $source );
103
104                 $importer->setPageCallback( array( &$this, 'reportPage' ) );
105                 $this->importCallback =  $importer->setRevisionCallback(
106                         array( &$this, 'handleRevision' ) );
107
108                 return $importer->doImport();
109         }
110 }
111
112 if( wfReadOnly() ) {
113         wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
114 }
115
116 $reader = new BackupReader();
117 if( isset( $options['quiet'] ) ) {
118         $reader->reporting = false;
119 }
120 if( isset( $options['report'] ) ) {
121         $reader->reportingInterval = intval( $options['report'] );
122 }
123 if( isset( $options['dry-run'] ) ) {
124         $reader->dryRun = true;
125 }
126
127 if( isset( $args[0] ) ) {
128         $result = $reader->importFromFile( $args[0] );
129 } else {
130         $result = $reader->importFromStdin();
131 }
132
133 if( WikiError::isError( $result ) ) {
134         echo $result->getMessage() . "\n";
135 } else {
136         echo "Done!\n";
137         echo "You might want to run rebuildrecentchanges.php to regenerate\n";
138         echo "the recentchanges page.\n";
139 }
140
141