MediaWiki REL1_30
importDump.php
Go to the documentation of this file.
1<?php
27require_once __DIR__ . '/Maintenance.php';
28
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
55This script reads pages from an XML file as produced from Special:Export or
56dumpBackup.php, and saves them into the current wiki.
57
58Compressed XML files may be read directly:
59 .gz $gz
60 .bz2 $bz2
61 .7z (if 7za executable is in PATH)
62
63Note that for very large data sets, importDump.php may be slow; there are
64alternate methods which can be much faster for full site restoration:
65<https://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
66TEXT
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
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
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
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 }
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';
334require_once RUN_MAINTENANCE_IF_MAIN;
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition files
Definition COPYING.txt:158
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
wfReadOnly()
Check whether the wiki is in read-only mode.
Maintenance script that imports XML dump files into the current wiki.
getNsIndex( $namespace)
importFromFile( $filename)
handleRevision( $rev)
execute()
Do the actual work.
report( $final=false)
__construct()
Default constructor.
reportPage( $page)
handleUpload( $revision)
handleLogItem( $rev)
importFromHandle( $handle)
skippedNamespace( $obj)
progress( $string)
setNsfilter(array $namespaces)
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
MediaWiki exception.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true)
Add some args that are needed.
hasArg( $argId=0)
Does a given argument exist?
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
hasOption( $name)
Checks to see if a particular param exists.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
maybeHelp( $force=false)
Maybe show the help.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Represents a title within MediaWiki.
Definition Title.php:39
XML file reader for the page data importer.
Represents a revision, log entry or upload during the import process.
The ContentHandler facility adds support for arbitrary content types on wiki pages
Use of locking reads(e.g. the FOR UPDATE clause) is not advised. They are poorly implemented in InnoDB and will cause regular deadlock errors. It 's also surprisingly easy to cripple the wiki with lock contention. Instead of locking reads
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at https
Definition design.txt:12
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database etc For and for historical it also represents a few features of articles that don t involve their such as access rights See also title txt Article Encapsulates access to the page table of the database The object represents a an and maintains state such as etc Revision Encapsulates individual page revision data and access to the revision text blobs storage system Higher level code should never touch text storage directly
Definition design.txt:39
namespace being checked & $result
Definition hooks.txt:2293
namespace and then decline to actually register it & $namespaces
Definition hooks.txt:932
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2581
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1760
$maintClass
require_once RUN_MAINTENANCE_IF_MAIN
$source
const DB_MASTER
Definition defines.php:26