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