Localisation updates for special page aliases from http://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 $this->dryRun = $this->hasOption( 'dry-run' );
78 $this->uploads = $this->hasOption( 'uploads' ); // experimental!
79 if ( $this->hasOption( 'image-base-path' ) ) {
80 $this->imageBasePath = $this->getOption( 'image-base-path' );
81 }
82 if ( $this->hasOption( 'namespaces' ) ) {
83 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
84 }
85
86 if( $this->hasArg() ) {
87 $this->importFromFile( $this->getArg() );
88 } else {
89 $this->importFromStdin();
90 }
91
92 $this->output( "Done!\n" );
93 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
94 }
95
96 function setNsfilter( array $namespaces ) {
97 if ( count( $namespaces ) == 0 ) {
98 $this->nsFilter = false;
99 return;
100 }
101 $this->nsFilter = array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
102 }
103
104 private function getNsIndex( $namespace ) {
105 global $wgContLang;
106 if ( ( $result = $wgContLang->getNsIndex( $namespace ) ) !== false ) {
107 return $result;
108 }
109 $ns = intval( $namespace );
110 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
111 return $ns;
112 }
113 $this->error( "Unknown namespace text / index specified: $namespace", true );
114 }
115
116 private function skippedNamespace( $obj ) {
117 if ( $obj instanceof Title ) {
118 $ns = $obj->getNamespace();
119 } elseif ( $obj instanceof Revision ) {
120 $ns = $obj->getTitle()->getNamespace();
121 } elseif ( $obj instanceof WikiRevision ) {
122 $ns = $obj->title->getNamespace();
123 } else {
124 echo wfBacktrace();
125 $this->error( "Cannot get namespace of object in " . __METHOD__, true );
126 }
127 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
128 }
129
130 function reportPage( $page ) {
131 $this->pageCount++;
132 }
133
134 function handleRevision( $rev ) {
135 $title = $rev->getTitle();
136 if ( !$title ) {
137 $this->progress( "Got bogus revision with null title!" );
138 return;
139 }
140
141 if ( $this->skippedNamespace( $title ) ) {
142 return;
143 }
144
145 $this->revCount++;
146 $this->report();
147
148 if ( !$this->dryRun ) {
149 call_user_func( $this->importCallback, $rev );
150 }
151 }
152
153 function handleUpload( $revision ) {
154 if ( $this->uploads ) {
155 if ( $this->skippedNamespace( $revision ) ) {
156 return;
157 }
158 $this->uploadCount++;
159 // $this->report();
160 $this->progress( "upload: " . $revision->getFilename() );
161
162 if ( !$this->dryRun ) {
163 // bluuuh hack
164 // call_user_func( $this->uploadCallback, $revision );
165 $dbw = wfGetDB( DB_MASTER );
166 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
167 }
168 }
169 }
170
171 function handleLogItem( $rev ) {
172 if ( $this->skippedNamespace( $rev ) ) {
173 return;
174 }
175 $this->revCount++;
176 $this->report();
177
178 if ( !$this->dryRun ) {
179 call_user_func( $this->logItemCallback, $rev );
180 }
181 }
182
183 function report( $final = false ) {
184 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
185 $this->showReport();
186 }
187 }
188
189 function showReport() {
190 if ( $this->mQuiet ) {
191 $delta = wfTime() - $this->startTime;
192 if ( $delta ) {
193 $rate = sprintf( "%.2f", $this->pageCount / $delta );
194 $revrate = sprintf( "%.2f", $this->revCount / $delta );
195 } else {
196 $rate = '-';
197 $revrate = '-';
198 }
199 # Logs dumps don't have page tallies
200 if ( $this->pageCount ) {
201 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
202 } else {
203 $this->progress( "$this->revCount ($revrate revs/sec)" );
204 }
205 }
206 wfWaitForSlaves();
207 // XXX: Don't let deferred jobs array get absurdly large (bug 24375)
208 wfDoUpdates( 'commit' );
209 }
210
211 function progress( $string ) {
212 fwrite( $this->stderr, $string . "\n" );
213 }
214
215 function importFromFile( $filename ) {
216 if ( preg_match( '/\.gz$/', $filename ) ) {
217 $filename = 'compress.zlib://' . $filename;
218 }
219 elseif ( preg_match( '/\.bz2$/', $filename ) ) {
220 $filename = 'compress.bzip2://' . $filename;
221 }
222 elseif ( preg_match( '/\.7z$/', $filename ) ) {
223 $filename = 'mediawiki.compress.7z://' . $filename;
224 }
225
226 $file = fopen( $filename, 'rt' );
227 return $this->importFromHandle( $file );
228 }
229
230 function importFromStdin() {
231 $file = fopen( 'php://stdin', 'rt' );
232 if( self::posix_isatty( $file ) ) {
233 $this->maybeHelp( true );
234 }
235 return $this->importFromHandle( $file );
236 }
237
238 function importFromHandle( $handle ) {
239 $this->startTime = wfTime();
240
241 $source = new ImportStreamSource( $handle );
242 $importer = new WikiImporter( $source );
243
244 if( $this->hasOption( 'debug' ) ) {
245 $importer->setDebug( true );
246 }
247 if ( $this->hasOption( 'no-updates' ) ) {
248 $importer->setNoUpdates( true );
249 }
250 $importer->setPageCallback( array( &$this, 'reportPage' ) );
251 $this->importCallback = $importer->setRevisionCallback(
252 array( &$this, 'handleRevision' ) );
253 $this->uploadCallback = $importer->setUploadCallback(
254 array( &$this, 'handleUpload' ) );
255 $this->logItemCallback = $importer->setLogItemCallback(
256 array( &$this, 'handleLogItem' ) );
257 if ( $this->uploads ) {
258 $importer->setImportUploads( true );
259 }
260 if ( $this->imageBasePath ) {
261 $importer->setImageBasePath( $this->imageBasePath );
262 }
263
264 if ( $this->dryRun ) {
265 $importer->setPageOutCallback( null );
266 }
267
268 return $importer->doImport();
269 }
270 }
271
272 $maintClass = 'BackupReader';
273 require_once( RUN_MAINTENANCE_IF_MAIN );