New hook for filters on Special:Contributions form
[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( 'rootpage', 'Pages will be imported as subpages of the specified page',
72 false, true );
73 $this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
74 $this->addOption( 'debug', 'Output extra verbose debug information' );
75 $this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
76 $this->addOption(
77 'no-updates',
78 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
79 );
80 $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
81 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
82 }
83
84 public function execute() {
85 if ( wfReadOnly() ) {
86 $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
87 }
88
89 $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
90 if ( !$this->reportingInterval ) {
91 $this->reportingInterval = 100; // avoid division by zero
92 }
93
94 $this->dryRun = $this->hasOption( 'dry-run' );
95 $this->uploads = $this->hasOption( 'uploads' ); // experimental!
96 if ( $this->hasOption( 'image-base-path' ) ) {
97 $this->imageBasePath = $this->getOption( 'image-base-path' );
98 }
99 if ( $this->hasOption( 'namespaces' ) ) {
100 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
101 }
102
103 if ( $this->hasArg() ) {
104 $this->importFromFile( $this->getArg() );
105 } else {
106 $this->importFromStdin();
107 }
108
109 $this->output( "Done!\n" );
110 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
111 }
112
113 function setNsfilter( array $namespaces ) {
114 if ( count( $namespaces ) == 0 ) {
115 $this->nsFilter = false;
116
117 return;
118 }
119 $this->nsFilter = array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
120 }
121
122 private function getNsIndex( $namespace ) {
123 global $wgContLang;
124 $result = $wgContLang->getNsIndex( $namespace );
125 if ( $result !== false ) {
126 return $result;
127 }
128 $ns = intval( $namespace );
129 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
130 return $ns;
131 }
132 $this->error( "Unknown namespace text / index specified: $namespace", true );
133 }
134
135 /**
136 * @param Title|Revision $obj
137 * @return bool
138 */
139 private function skippedNamespace( $obj ) {
140 $title = null;
141 if ( $obj instanceof Title ) {
142 $title = $obj;
143 } elseif ( $obj instanceof Revision ) {
144 $title = $obj->getTitle();
145 } elseif ( $obj instanceof WikiRevision ) {
146 $title = $obj->title;
147 } else {
148 throw new MWException( "Cannot get namespace of object in " . __METHOD__ );
149 }
150
151 if ( is_null( $title ) ) {
152 // Probably a log entry
153 return false;
154 }
155
156 $ns = $title->getNamespace();
157
158 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
159 }
160
161 function reportPage( $page ) {
162 $this->pageCount++;
163 }
164
165 /**
166 * @param Revision $rev
167 */
168 function handleRevision( $rev ) {
169 $title = $rev->getTitle();
170 if ( !$title ) {
171 $this->progress( "Got bogus revision with null title!" );
172
173 return;
174 }
175
176 if ( $this->skippedNamespace( $title ) ) {
177 return;
178 }
179
180 $this->revCount++;
181 $this->report();
182
183 if ( !$this->dryRun ) {
184 call_user_func( $this->importCallback, $rev );
185 }
186 }
187
188 /**
189 * @param Revision $revision
190 * @return bool
191 */
192 function handleUpload( $revision ) {
193 if ( $this->uploads ) {
194 if ( $this->skippedNamespace( $revision ) ) {
195 return false;
196 }
197 $this->uploadCount++;
198 // $this->report();
199 $this->progress( "upload: " . $revision->getFilename() );
200
201 if ( !$this->dryRun ) {
202 // bluuuh hack
203 // call_user_func( $this->uploadCallback, $revision );
204 $dbw = $this->getDB( DB_MASTER );
205
206 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
207 }
208 }
209
210 return false;
211 }
212
213 function handleLogItem( $rev ) {
214 if ( $this->skippedNamespace( $rev ) ) {
215 return;
216 }
217 $this->revCount++;
218 $this->report();
219
220 if ( !$this->dryRun ) {
221 call_user_func( $this->logItemCallback, $rev );
222 }
223 }
224
225 function report( $final = false ) {
226 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
227 $this->showReport();
228 }
229 }
230
231 function showReport() {
232 if ( !$this->mQuiet ) {
233 $delta = microtime( true ) - $this->startTime;
234 if ( $delta ) {
235 $rate = sprintf( "%.2f", $this->pageCount / $delta );
236 $revrate = sprintf( "%.2f", $this->revCount / $delta );
237 } else {
238 $rate = '-';
239 $revrate = '-';
240 }
241 # Logs dumps don't have page tallies
242 if ( $this->pageCount ) {
243 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
244 } else {
245 $this->progress( "$this->revCount ($revrate revs/sec)" );
246 }
247 }
248 wfWaitForSlaves();
249 }
250
251 function progress( $string ) {
252 fwrite( $this->stderr, $string . "\n" );
253 }
254
255 function importFromFile( $filename ) {
256 if ( preg_match( '/\.gz$/', $filename ) ) {
257 $filename = 'compress.zlib://' . $filename;
258 } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
259 $filename = 'compress.bzip2://' . $filename;
260 } elseif ( preg_match( '/\.7z$/', $filename ) ) {
261 $filename = 'mediawiki.compress.7z://' . $filename;
262 }
263
264 $file = fopen( $filename, 'rt' );
265
266 return $this->importFromHandle( $file );
267 }
268
269 function importFromStdin() {
270 $file = fopen( 'php://stdin', 'rt' );
271 if ( self::posix_isatty( $file ) ) {
272 $this->maybeHelp( true );
273 }
274
275 return $this->importFromHandle( $file );
276 }
277
278 function importFromHandle( $handle ) {
279 $this->startTime = microtime( true );
280
281 $source = new ImportStreamSource( $handle );
282 $importer = new WikiImporter( $source, $this->getConfig() );
283
284 if ( $this->hasOption( 'debug' ) ) {
285 $importer->setDebug( true );
286 }
287 if ( $this->hasOption( 'no-updates' ) ) {
288 $importer->setNoUpdates( true );
289 }
290 if ( $this->hasOption( 'rootpage' ) ) {
291 $statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) );
292 if ( !$statusRootPage->isGood() ) {
293 // Die here so that it doesn't print "Done!"
294 $this->error( $statusRootPage->getMessage()->text(), 1 );
295 return false;
296 }
297 }
298 $importer->setPageCallback( array( &$this, 'reportPage' ) );
299 $this->importCallback = $importer->setRevisionCallback(
300 array( &$this, 'handleRevision' ) );
301 $this->uploadCallback = $importer->setUploadCallback(
302 array( &$this, 'handleUpload' ) );
303 $this->logItemCallback = $importer->setLogItemCallback(
304 array( &$this, 'handleLogItem' ) );
305 if ( $this->uploads ) {
306 $importer->setImportUploads( true );
307 }
308 if ( $this->imageBasePath ) {
309 $importer->setImageBasePath( $this->imageBasePath );
310 }
311
312 if ( $this->dryRun ) {
313 $importer->setPageOutCallback( null );
314 }
315
316 return $importer->doImport();
317 }
318 }
319
320 $maintClass = 'BackupReader';
321 require_once RUN_MAINTENANCE_IF_MAIN;