Localisation updates for core and extension messages from translatewiki.net
[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 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
27 /**
28 * @ingroup Maintenance
29 */
30 class BackupReader extends Maintenance {
31 var $reportingInterval = 100;
32 var $pageCount = 0;
33 var $revCount = 0;
34 var $dryRun = false;
35 var $uploads = false;
36 var $imageBasePath = false;
37 var $nsFilter = false;
38
39 function __construct() {
40 parent::__construct();
41 $gz = in_array('compress.zlib', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP zlib module)';
42 $bz2 = in_array('compress.bzip2', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP bzip2 module)';
43
44 $this->mDescription = <<<TEXT
45 This script reads pages from an XML file as produced from Special:Export or
46 dumpBackup.php, and saves them into the current wiki.
47
48 Compressed XML files may be read directly:
49 .gz $gz
50 .bz2 $bz2
51 .7z (if 7za executable is in PATH)
52
53 Note that for very large data sets, importDump.php may be slow; there are
54 alternate methods which can be much faster for full site restoration:
55 <http://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
56 TEXT;
57 $this->stderr = fopen( "php://stderr", "wt" );
58 $this->addOption( 'report',
59 'Report position and speed after every n pages processed', false, true );
60 $this->addOption( 'namespaces',
61 'Import only the pages from namespaces belonging to the list of ' .
62 'pipe-separated namespace names or namespace indexes', false, true );
63 $this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
64 $this->addOption( 'debug', 'Output extra verbose debug information' );
65 $this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
66 $this->addOption( 'no-updates', 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state' );
67 $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
68 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
69 }
70
71 public function execute() {
72 if( wfReadOnly() ) {
73 $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
74 }
75
76 $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
77 if ( !$this->reportingInterval ) {
78 $this->reportingInterval = 100; // avoid division by zero
79 }
80
81 $this->dryRun = $this->hasOption( 'dry-run' );
82 $this->uploads = $this->hasOption( 'uploads' ); // experimental!
83 if ( $this->hasOption( 'image-base-path' ) ) {
84 $this->imageBasePath = $this->getOption( 'image-base-path' );
85 }
86 if ( $this->hasOption( 'namespaces' ) ) {
87 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
88 }
89
90 if( $this->hasArg() ) {
91 $this->importFromFile( $this->getArg() );
92 } else {
93 $this->importFromStdin();
94 }
95
96 $this->output( "Done!\n" );
97 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
98 }
99
100 function setNsfilter( array $namespaces ) {
101 if ( count( $namespaces ) == 0 ) {
102 $this->nsFilter = false;
103 return;
104 }
105 $this->nsFilter = array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
106 }
107
108 private function getNsIndex( $namespace ) {
109 global $wgContLang;
110 if ( ( $result = $wgContLang->getNsIndex( $namespace ) ) !== false ) {
111 return $result;
112 }
113 $ns = intval( $namespace );
114 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
115 return $ns;
116 }
117 $this->error( "Unknown namespace text / index specified: $namespace", true );
118 }
119
120 private function skippedNamespace( $obj ) {
121 if ( $obj instanceof Title ) {
122 $ns = $obj->getNamespace();
123 } elseif ( $obj instanceof Revision ) {
124 $ns = $obj->getTitle()->getNamespace();
125 } elseif ( $obj instanceof WikiRevision ) {
126 $ns = $obj->title->getNamespace();
127 } else {
128 echo wfBacktrace();
129 $this->error( "Cannot get namespace of object in " . __METHOD__, true );
130 }
131 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
132 }
133
134 function reportPage( $page ) {
135 $this->pageCount++;
136 }
137
138 function handleRevision( $rev ) {
139 $title = $rev->getTitle();
140 if ( !$title ) {
141 $this->progress( "Got bogus revision with null title!" );
142 return;
143 }
144
145 if ( $this->skippedNamespace( $title ) ) {
146 return;
147 }
148
149 $this->revCount++;
150 $this->report();
151
152 if ( !$this->dryRun ) {
153 call_user_func( $this->importCallback, $rev );
154 }
155 }
156
157 function handleUpload( $revision ) {
158 if ( $this->uploads ) {
159 if ( $this->skippedNamespace( $revision ) ) {
160 return;
161 }
162 $this->uploadCount++;
163 // $this->report();
164 $this->progress( "upload: " . $revision->getFilename() );
165
166 if ( !$this->dryRun ) {
167 // bluuuh hack
168 // call_user_func( $this->uploadCallback, $revision );
169 $dbw = wfGetDB( DB_MASTER );
170 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
171 }
172 }
173 }
174
175 function handleLogItem( $rev ) {
176 if ( $this->skippedNamespace( $rev ) ) {
177 return;
178 }
179 $this->revCount++;
180 $this->report();
181
182 if ( !$this->dryRun ) {
183 call_user_func( $this->logItemCallback, $rev );
184 }
185 }
186
187 function report( $final = false ) {
188 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
189 $this->showReport();
190 }
191 }
192
193 function showReport() {
194 if ( $this->mQuiet ) {
195 $delta = wfTime() - $this->startTime;
196 if ( $delta ) {
197 $rate = sprintf( "%.2f", $this->pageCount / $delta );
198 $revrate = sprintf( "%.2f", $this->revCount / $delta );
199 } else {
200 $rate = '-';
201 $revrate = '-';
202 }
203 # Logs dumps don't have page tallies
204 if ( $this->pageCount ) {
205 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
206 } else {
207 $this->progress( "$this->revCount ($revrate revs/sec)" );
208 }
209 }
210 wfWaitForSlaves();
211 // XXX: Don't let deferred jobs array get absurdly large (bug 24375)
212 DeferredUpdates::doUpdates( 'commit' );
213 }
214
215 function progress( $string ) {
216 fwrite( $this->stderr, $string . "\n" );
217 }
218
219 function importFromFile( $filename ) {
220 if ( preg_match( '/\.gz$/', $filename ) ) {
221 $filename = 'compress.zlib://' . $filename;
222 }
223 elseif ( preg_match( '/\.bz2$/', $filename ) ) {
224 $filename = 'compress.bzip2://' . $filename;
225 }
226 elseif ( preg_match( '/\.7z$/', $filename ) ) {
227 $filename = 'mediawiki.compress.7z://' . $filename;
228 }
229
230 $file = fopen( $filename, 'rt' );
231 return $this->importFromHandle( $file );
232 }
233
234 function importFromStdin() {
235 $file = fopen( 'php://stdin', 'rt' );
236 if( self::posix_isatty( $file ) ) {
237 $this->maybeHelp( true );
238 }
239 return $this->importFromHandle( $file );
240 }
241
242 function importFromHandle( $handle ) {
243 $this->startTime = wfTime();
244
245 $source = new ImportStreamSource( $handle );
246 $importer = new WikiImporter( $source );
247
248 if( $this->hasOption( 'debug' ) ) {
249 $importer->setDebug( true );
250 }
251 if ( $this->hasOption( 'no-updates' ) ) {
252 $importer->setNoUpdates( true );
253 }
254 $importer->setPageCallback( array( &$this, 'reportPage' ) );
255 $this->importCallback = $importer->setRevisionCallback(
256 array( &$this, 'handleRevision' ) );
257 $this->uploadCallback = $importer->setUploadCallback(
258 array( &$this, 'handleUpload' ) );
259 $this->logItemCallback = $importer->setLogItemCallback(
260 array( &$this, 'handleLogItem' ) );
261 if ( $this->uploads ) {
262 $importer->setImportUploads( true );
263 }
264 if ( $this->imageBasePath ) {
265 $importer->setImageBasePath( $this->imageBasePath );
266 }
267
268 if ( $this->dryRun ) {
269 $importer->setPageOutCallback( null );
270 }
271
272 return $importer->doImport();
273 }
274 }
275
276 $maintClass = 'BackupReader';
277 require_once( RUN_MAINTENANCE_IF_MAIN );