make test-light (cause i am lazy)
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @package MediaWiki
22 * @subpackage Maintenance
23 */
24
25 $options = array( 'full', 'verbose', 'dry-run', 'preserve' );
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 $display = $title->getPrefixedText();
48 $timestamp = $rev->getTimestamp();
49 #echo "$display $timestamp\n";
50
51 $this->revCount++;
52 $this->report();
53
54 if( !$this->dryRun ) {
55 call_user_func( $this->importCallback, $rev );
56 }
57 }
58
59 function report( $final = false ) {
60 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
61 $this->showReport();
62 }
63 }
64
65 function showReport() {
66 if( $this->reporting ) {
67 $delta = wfTime() - $this->startTime;
68 if( $delta ) {
69 $rate = $this->pageCount / $delta;
70 $revrate = $this->revCount / $delta;
71 } else {
72 $rate = '-';
73 $revrate = '-';
74 }
75 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
76 }
77 }
78
79 function progress( $string ) {
80 fwrite( $this->stderr, $string . "\n" );
81 }
82
83 function importFromFile( $filename ) {
84 if( preg_match( '/\.gz$/', $filename ) ) {
85 $filename = 'compress.zlib://' . $filename;
86 }
87 $file = fopen( $filename, 'rt' );
88 $this->importFromHandle( $file );
89 }
90
91 function importFromStdin() {
92 $file = fopen( 'php://stdin', 'rt' );
93 $this->importFromHandle( $file );
94 }
95
96 function importFromHandle( $handle ) {
97 $this->startTime = wfTime();
98
99 $source = new ImportStreamSource( $handle );
100 $importer = new WikiImporter( $source );
101
102 $importer->setPageCallback( array( &$this, 'reportPage' ) );
103 $this->importCallback = $importer->setRevisionCallback(
104 array( &$this, 'handleRevision' ) );
105
106 $importer->doImport();
107 }
108 }
109
110 $reader = new BackupReader();
111 if( isset( $options['quiet'] ) ) {
112 $reader->reporting = false;
113 }
114 if( isset( $options['report'] ) ) {
115 $reader->reportingInterval = IntVal( $options['report'] );
116 }
117 if( isset( $options['dry-run'] ) ) {
118 $reader->dryRun = true;
119 }
120
121 if( isset( $args[0] ) ) {
122 $reader->importFromFile( $args[0] );
123 } else {
124 $reader->importFromStdin();
125 }
126
127 ?>