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