Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / maintenance / importDump.php
blobeb51126a19b35a408d0763751f6b03b101ed3ed8
1 <?php
2 /**
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
22 * @ingroup Maintenance
25 $optionsWithArgs = array( 'report' );
27 require_once( 'commandLine.inc' );
29 /**
30 * @ingroup Maintenance
32 class BackupReader {
33 var $reportingInterval = 100;
34 var $reporting = true;
35 var $pageCount = 0;
36 var $revCount = 0;
37 var $dryRun = false;
38 var $debug = false;
39 var $uploads = false;
41 function BackupReader() {
42 $this->stderr = fopen( "php://stderr", "wt" );
45 function reportPage( $page ) {
46 $this->pageCount++;
49 function handleRevision( $rev ) {
50 $title = $rev->getTitle();
51 if( !$title ) {
52 $this->progress( "Got bogus revision with null title!" );
53 return;
56 $this->revCount++;
57 $this->report();
59 if( !$this->dryRun ) {
60 call_user_func( $this->importCallback, $rev );
64 function handleUpload( $revision ) {
65 if( $this->uploads ) {
66 $this->uploadCount++;
67 //$this->report();
68 $this->progress( "upload: " . $revision->getFilename() );
70 if( !$this->dryRun ) {
71 // bluuuh hack
72 //call_user_func( $this->uploadCallback, $revision );
73 $dbw = wfGetDB( DB_MASTER );
74 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
79 function handleLogItem( $rev ) {
80 $this->revCount++;
81 $this->report();
83 if( !$this->dryRun ) {
84 call_user_func( $this->logItemCallback, $rev );
88 function report( $final = false ) {
89 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
90 $this->showReport();
94 function showReport() {
95 if( $this->reporting ) {
96 $delta = wfTime() - $this->startTime;
97 if( $delta ) {
98 $rate = sprintf("%.2f", $this->pageCount / $delta);
99 $revrate = sprintf("%.2f", $this->revCount / $delta);
100 } else {
101 $rate = '-';
102 $revrate = '-';
104 # Logs dumps don't have page tallies
105 if( $this->pageCount )
106 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
107 else
108 $this->progress( "$this->revCount ($revrate revs/sec)" );
110 wfWaitForSlaves(5);
113 function progress( $string ) {
114 fwrite( $this->stderr, $string . "\n" );
117 function importFromFile( $filename ) {
118 if( preg_match( '/\.gz$/', $filename ) ) {
119 $filename = 'compress.zlib://' . $filename;
121 $file = fopen( $filename, 'rt' );
122 return $this->importFromHandle( $file );
125 function importFromStdin() {
126 $file = fopen( 'php://stdin', 'rt' );
127 return $this->importFromHandle( $file );
130 function importFromHandle( $handle ) {
131 $this->startTime = wfTime();
133 $source = new ImportStreamSource( $handle );
134 $importer = new WikiImporter( $source );
136 $importer->setDebug( $this->debug );
137 $importer->setPageCallback( array( &$this, 'reportPage' ) );
138 $this->importCallback = $importer->setRevisionCallback(
139 array( &$this, 'handleRevision' ) );
140 $this->uploadCallback = $importer->setUploadCallback(
141 array( &$this, 'handleUpload' ) );
142 $this->logItemCallback = $importer->setLogItemCallback(
143 array( &$this, 'handleLogItem' ) );
145 return $importer->doImport();
149 if( wfReadOnly() ) {
150 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
153 $reader = new BackupReader();
154 if( isset( $options['quiet'] ) ) {
155 $reader->reporting = false;
157 if( isset( $options['report'] ) ) {
158 $reader->reportingInterval = intval( $options['report'] );
160 if( isset( $options['dry-run'] ) ) {
161 $reader->dryRun = true;
163 if( isset( $options['debug'] ) ) {
164 $reader->debug = true;
166 if( isset( $options['uploads'] ) ) {
167 $reader->uploads = true; // experimental!
170 if( isset( $args[0] ) ) {
171 $result = $reader->importFromFile( $args[0] );
172 } else {
173 $result = $reader->importFromStdin();
176 if( WikiError::isError( $result ) ) {
177 echo $result->getMessage() . "\n";
178 } else {
179 echo "Done!\n";
180 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
181 echo "the recentchanges page.\n";