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