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