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