2005-07-05 03:16:56 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-07-17 05:40:40 +00:00
|
|
|
* Import XML dump files into the current wiki.
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2005 Brion Vibber <brion@pobox.com>
|
2014-03-20 15:45:01 +00:00
|
|
|
* https://www.mediawiki.org/
|
2005-08-02 13:35:19 +00:00
|
|
|
*
|
2005-07-05 03:16:56 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2005-08-02 13:35:19 +00:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
2005-07-05 03:16:56 +00:00
|
|
|
* (at your option) any later version.
|
2005-08-02 13:35:19 +00:00
|
|
|
*
|
2005-07-05 03:16:56 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
2005-08-02 13:35:19 +00:00
|
|
|
*
|
2005-07-05 03:16:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2006-04-05 07:43:17 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-07-05 03:16:56 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
2005-07-05 03:16:56 +00:00
|
|
|
*/
|
|
|
|
|
|
2020-04-03 23:03:40 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2018-07-29 12:24:54 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2005-07-05 03:16:56 +00:00
|
|
|
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2012-07-17 05:40:40 +00:00
|
|
|
* Maintenance script that imports XML dump files into the current wiki.
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2011-05-21 23:22:47 +00:00
|
|
|
class BackupReader extends Maintenance {
|
2022-03-07 23:21:18 +00:00
|
|
|
/** @var int */
|
2012-09-14 18:57:14 +00:00
|
|
|
public $reportingInterval = 100;
|
2022-03-07 23:21:18 +00:00
|
|
|
/** @var int */
|
2012-09-14 18:57:14 +00:00
|
|
|
public $pageCount = 0;
|
2022-03-07 23:21:18 +00:00
|
|
|
/** @var int */
|
2013-04-18 18:48:44 +00:00
|
|
|
public $revCount = 0;
|
2022-03-07 23:21:18 +00:00
|
|
|
/** @var bool */
|
2013-04-18 18:48:44 +00:00
|
|
|
public $dryRun = false;
|
2022-03-07 23:21:18 +00:00
|
|
|
/** @var bool */
|
2013-04-18 18:48:44 +00:00
|
|
|
public $uploads = false;
|
2022-03-07 23:21:18 +00:00
|
|
|
/** @var int */
|
2017-08-26 15:28:01 +00:00
|
|
|
protected $uploadCount = 0;
|
2022-03-07 23:21:18 +00:00
|
|
|
/** @var string|false */
|
2012-09-14 18:57:14 +00:00
|
|
|
public $imageBasePath = false;
|
2019-08-30 16:01:28 +00:00
|
|
|
/** @var array|false */
|
2013-04-18 18:48:44 +00:00
|
|
|
public $nsFilter = false;
|
2022-03-07 23:21:18 +00:00
|
|
|
/** @var resource|false */
|
2019-09-09 09:11:50 +00:00
|
|
|
public $stderr;
|
|
|
|
|
/** @var callable|null */
|
|
|
|
|
protected $importCallback;
|
|
|
|
|
/** @var callable|null */
|
|
|
|
|
protected $logItemCallback;
|
|
|
|
|
/** @var callable|null */
|
|
|
|
|
protected $uploadCallback;
|
2022-03-07 23:21:18 +00:00
|
|
|
/** @var float */
|
2019-09-09 09:11:50 +00:00
|
|
|
protected $startTime;
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2019-10-09 18:41:33 +00:00
|
|
|
public function __construct() {
|
2011-05-21 23:22:47 +00:00
|
|
|
parent::__construct();
|
2014-04-23 08:53:03 +00:00
|
|
|
$gz = in_array( 'compress.zlib', stream_get_wrappers() )
|
|
|
|
|
? 'ok'
|
|
|
|
|
: '(disabled; requires PHP zlib module)';
|
|
|
|
|
$bz2 = in_array( 'compress.bzip2', stream_get_wrappers() )
|
|
|
|
|
? 'ok'
|
|
|
|
|
: '(disabled; requires PHP bzip2 module)';
|
2011-05-21 23:22:47 +00:00
|
|
|
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription(
|
|
|
|
|
<<<TEXT
|
2011-05-21 23:22:47 +00:00
|
|
|
This script reads pages from an XML file as produced from Special:Export or
|
|
|
|
|
dumpBackup.php, and saves them into the current wiki.
|
|
|
|
|
|
|
|
|
|
Compressed XML files may be read directly:
|
|
|
|
|
.gz $gz
|
|
|
|
|
.bz2 $bz2
|
|
|
|
|
.7z (if 7za executable is in PATH)
|
|
|
|
|
|
|
|
|
|
Note that for very large data sets, importDump.php may be slow; there are
|
|
|
|
|
alternate methods which can be much faster for full site restoration:
|
2014-03-20 15:45:01 +00:00
|
|
|
<https://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
|
2016-01-30 02:48:47 +00:00
|
|
|
TEXT
|
|
|
|
|
);
|
2005-07-05 03:16:56 +00:00
|
|
|
$this->stderr = fopen( "php://stderr", "wt" );
|
2011-05-21 23:22:47 +00:00
|
|
|
$this->addOption( 'report',
|
|
|
|
|
'Report position and speed after every n pages processed', false, true );
|
2011-10-18 17:31:54 +00:00
|
|
|
$this->addOption( 'namespaces',
|
2011-05-21 23:22:47 +00:00
|
|
|
'Import only the pages from namespaces belonging to the list of ' .
|
|
|
|
|
'pipe-separated namespace names or namespace indexes', false, true );
|
2015-12-26 03:49:48 +00:00
|
|
|
$this->addOption( 'rootpage', 'Pages will be imported as subpages of the specified page',
|
|
|
|
|
false, true );
|
2011-05-21 23:22:47 +00:00
|
|
|
$this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
|
|
|
|
|
$this->addOption( 'debug', 'Output extra verbose debug information' );
|
|
|
|
|
$this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
|
2014-04-23 08:53:03 +00:00
|
|
|
$this->addOption(
|
|
|
|
|
'no-updates',
|
|
|
|
|
'Disable link table updates. Is faster but leaves the wiki in an inconsistent state'
|
|
|
|
|
);
|
2011-05-21 23:22:47 +00:00
|
|
|
$this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
|
2017-05-26 01:54:32 +00:00
|
|
|
$this->addOption( 'skip-to', 'Start from nth page by skipping first n-1 pages', false, true );
|
2018-07-23 16:06:20 +00:00
|
|
|
$this->addOption( 'username-prefix', 'Prefix for interwiki usernames', false, true );
|
2017-10-25 19:26:53 +00:00
|
|
|
$this->addOption( 'no-local-users',
|
|
|
|
|
'Treat all usernames as interwiki. ' .
|
|
|
|
|
'The default is to assign edits to local users where they exist.',
|
|
|
|
|
false, false
|
|
|
|
|
);
|
2011-05-21 23:22:47 +00:00
|
|
|
$this->addArg( 'file', 'Dump file to import [else use stdin]', false );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2022-06-20 15:05:18 +00:00
|
|
|
if ( MediaWikiServices::getInstance()->getReadOnlyMode()->isReadOnly() ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Wiki is in read-only mode; you'll need to disable it for import to work." );
|
2011-05-21 23:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
|
2011-09-06 18:02:44 +00:00
|
|
|
if ( !$this->reportingInterval ) {
|
2020-05-19 22:10:27 +00:00
|
|
|
// avoid division by zero
|
|
|
|
|
$this->reportingInterval = 100;
|
2011-09-06 18:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 23:22:47 +00:00
|
|
|
$this->dryRun = $this->hasOption( 'dry-run' );
|
2020-05-19 22:10:27 +00:00
|
|
|
$this->uploads = $this->hasOption( 'uploads' );
|
|
|
|
|
|
2011-05-21 23:22:47 +00:00
|
|
|
if ( $this->hasOption( 'image-base-path' ) ) {
|
|
|
|
|
$this->imageBasePath = $this->getOption( 'image-base-path' );
|
|
|
|
|
}
|
|
|
|
|
if ( $this->hasOption( 'namespaces' ) ) {
|
|
|
|
|
$this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-28 11:13:49 +00:00
|
|
|
if ( $this->hasArg( 0 ) ) {
|
|
|
|
|
$this->importFromFile( $this->getArg( 0 ) );
|
2011-05-21 23:22:47 +00:00
|
|
|
} else {
|
2011-06-01 16:27:46 +00:00
|
|
|
$this->importFromStdin();
|
2011-05-21 23:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->output( "Done!\n" );
|
2017-01-03 14:35:34 +00:00
|
|
|
$this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges,\n" );
|
|
|
|
|
$this->output( "and initSiteStats.php to update page and revision counts\n" );
|
2005-07-05 03:16:56 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function setNsfilter( array $namespaces ) {
|
2011-03-04 18:25:21 +00:00
|
|
|
if ( count( $namespaces ) == 0 ) {
|
|
|
|
|
$this->nsFilter = false;
|
2014-04-23 18:09:13 +00:00
|
|
|
|
2011-03-04 18:25:21 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->nsFilter = array_unique( array_map( [ $this, 'getNsIndex' ], $namespaces ) );
|
2011-03-04 18:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getNsIndex( $namespace ) {
|
2018-07-29 12:24:54 +00:00
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
|
|
|
|
$result = $contLang->getNsIndex( $namespace );
|
2015-11-01 19:56:20 +00:00
|
|
|
if ( $result !== false ) {
|
2011-03-04 18:25:21 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
$ns = intval( $namespace );
|
2018-07-29 12:24:54 +00:00
|
|
|
if ( strval( $ns ) === $namespace && $contLang->getNsText( $ns ) !== false ) {
|
2011-03-04 18:25:21 +00:00
|
|
|
return $ns;
|
|
|
|
|
}
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Unknown namespace text / index specified: $namespace" );
|
2011-03-04 18:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-28 18:11:47 +00:00
|
|
|
/**
|
2020-04-03 23:03:40 +00:00
|
|
|
* @param LinkTarget|null $title
|
2017-11-14 10:32:59 +00:00
|
|
|
* @throws MWException
|
2011-10-28 18:11:47 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-04-03 23:03:40 +00:00
|
|
|
private function skippedNamespace( $title ) {
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $title === null ) {
|
2015-12-19 09:06:26 +00:00
|
|
|
// Probably a log entry
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ns = $title->getNamespace();
|
|
|
|
|
|
2011-03-04 18:25:21 +00:00
|
|
|
return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 18:48:27 +00:00
|
|
|
public function reportPage( $page ) {
|
2005-07-05 03:16:56 +00:00
|
|
|
$this->pageCount++;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2011-10-28 18:11:47 +00:00
|
|
|
/**
|
2020-04-03 23:03:40 +00:00
|
|
|
* @param WikiRevision $rev
|
2011-10-28 18:11:47 +00:00
|
|
|
*/
|
2020-04-03 23:03:40 +00:00
|
|
|
public function handleRevision( WikiRevision $rev ) {
|
2005-07-05 03:16:56 +00:00
|
|
|
$title = $rev->getTitle();
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$title ) {
|
2005-09-24 03:44:54 +00:00
|
|
|
$this->progress( "Got bogus revision with null title!" );
|
2014-04-23 18:09:13 +00:00
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-04 18:25:21 +00:00
|
|
|
if ( $this->skippedNamespace( $title ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-05 03:16:56 +00:00
|
|
|
$this->revCount++;
|
|
|
|
|
$this->report();
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$this->dryRun ) {
|
2005-07-05 03:16:56 +00:00
|
|
|
call_user_func( $this->importCallback, $rev );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2011-10-18 17:31:54 +00:00
|
|
|
/**
|
2020-04-03 23:03:40 +00:00
|
|
|
* @param WikiRevision $revision
|
2011-10-18 17:31:54 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-04-03 23:03:40 +00:00
|
|
|
public function handleUpload( WikiRevision $revision ) {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->uploads ) {
|
2020-04-03 23:03:40 +00:00
|
|
|
if ( $this->skippedNamespace( $revision->getTitle() ) ) {
|
2014-07-21 09:32:34 +00:00
|
|
|
return false;
|
2011-03-04 18:25:21 +00:00
|
|
|
}
|
2008-03-07 23:14:30 +00:00
|
|
|
$this->uploadCount++;
|
2010-05-22 16:50:39 +00:00
|
|
|
// $this->report();
|
2008-03-07 23:14:30 +00:00
|
|
|
$this->progress( "upload: " . $revision->getFilename() );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$this->dryRun ) {
|
2008-03-07 23:14:30 +00:00
|
|
|
// bluuuh hack
|
2010-05-22 16:50:39 +00:00
|
|
|
// call_user_func( $this->uploadCallback, $revision );
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
2014-04-23 18:09:13 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return $dbw->deadlockLoop( [ $revision, 'importUpload' ] );
|
2008-03-07 23:14:30 +00:00
|
|
|
}
|
2008-03-06 07:24:29 +00:00
|
|
|
}
|
2014-07-21 09:32:34 +00:00
|
|
|
|
|
|
|
|
return false;
|
2008-03-06 07:24:29 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2020-04-03 23:03:40 +00:00
|
|
|
/**
|
|
|
|
|
* @param WikiRevision $rev
|
|
|
|
|
*/
|
|
|
|
|
public function handleLogItem( WikiRevision $rev ) {
|
|
|
|
|
if ( $this->skippedNamespace( $rev->getTitle() ) ) {
|
2011-03-04 18:25:21 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2008-09-18 20:52:34 +00:00
|
|
|
$this->revCount++;
|
|
|
|
|
$this->report();
|
|
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$this->dryRun ) {
|
2008-09-18 20:52:34 +00:00
|
|
|
call_user_func( $this->logItemCallback, $rev );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function report( $final = false ) {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
|
2005-07-05 03:16:56 +00:00
|
|
|
$this->showReport();
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function showReport() {
|
2012-01-11 10:38:25 +00:00
|
|
|
if ( !$this->mQuiet ) {
|
2012-09-04 19:05:40 +00:00
|
|
|
$delta = microtime( true ) - $this->startTime;
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $delta ) {
|
|
|
|
|
$rate = sprintf( "%.2f", $this->pageCount / $delta );
|
|
|
|
|
$revrate = sprintf( "%.2f", $this->revCount / $delta );
|
2005-07-05 03:16:56 +00:00
|
|
|
} else {
|
|
|
|
|
$rate = '-';
|
|
|
|
|
$revrate = '-';
|
|
|
|
|
}
|
2008-09-18 21:48:55 +00:00
|
|
|
# Logs dumps don't have page tallies
|
2011-03-20 21:55:54 +00:00
|
|
|
if ( $this->pageCount ) {
|
2008-09-18 21:48:55 +00:00
|
|
|
$this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
|
2011-03-20 21:55:54 +00:00
|
|
|
} else {
|
2008-09-18 21:48:55 +00:00
|
|
|
$this->progress( "$this->revCount ($revrate revs/sec)" );
|
2011-03-20 21:55:54 +00:00
|
|
|
}
|
2005-07-05 03:16:56 +00:00
|
|
|
}
|
2020-05-01 00:36:46 +00:00
|
|
|
MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
|
2005-07-05 03:16:56 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function progress( $string ) {
|
2005-07-05 03:16:56 +00:00
|
|
|
fwrite( $this->stderr, $string . "\n" );
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function importFromFile( $filename ) {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( preg_match( '/\.gz$/', $filename ) ) {
|
2005-07-05 03:16:56 +00:00
|
|
|
$filename = 'compress.zlib://' . $filename;
|
2011-10-28 18:11:47 +00:00
|
|
|
} elseif ( preg_match( '/\.bz2$/', $filename ) ) {
|
2009-08-07 00:53:21 +00:00
|
|
|
$filename = 'compress.bzip2://' . $filename;
|
2011-10-28 18:11:47 +00:00
|
|
|
} elseif ( preg_match( '/\.7z$/', $filename ) ) {
|
2009-08-07 00:53:21 +00:00
|
|
|
$filename = 'mediawiki.compress.7z://' . $filename;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-04 15:04:41 +00:00
|
|
|
$file = fopen( $filename, 'rt' );
|
2021-10-18 04:33:47 +00:00
|
|
|
if ( $file === false ) {
|
|
|
|
|
$this->fatalError( error_get_last()['message'] ?? 'Could not open file' );
|
|
|
|
|
}
|
2014-04-23 18:09:13 +00:00
|
|
|
|
2005-09-17 11:10:15 +00:00
|
|
|
return $this->importFromHandle( $file );
|
2005-07-05 03:16:56 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function importFromStdin() {
|
2005-07-05 03:16:56 +00:00
|
|
|
$file = fopen( 'php://stdin', 'rt' );
|
2013-04-18 18:48:44 +00:00
|
|
|
if ( self::posix_isatty( $file ) ) {
|
2011-05-21 23:22:47 +00:00
|
|
|
$this->maybeHelp( true );
|
2011-02-08 23:45:16 +00:00
|
|
|
}
|
2014-04-23 18:09:13 +00:00
|
|
|
|
2005-09-17 11:10:15 +00:00
|
|
|
return $this->importFromHandle( $file );
|
2005-07-05 03:16:56 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function importFromHandle( $handle ) {
|
2012-09-04 19:05:40 +00:00
|
|
|
$this->startTime = microtime( true );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-07-05 03:16:56 +00:00
|
|
|
$source = new ImportStreamSource( $handle );
|
2021-05-14 23:16:34 +00:00
|
|
|
$importer = MediaWikiServices::getInstance()
|
|
|
|
|
->getWikiImporterFactory()
|
|
|
|
|
->getWikiImporter( $source );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2017-01-03 14:35:34 +00:00
|
|
|
// Updating statistics require a lot of time so disable it
|
|
|
|
|
$importer->disableStatisticsUpdate();
|
|
|
|
|
|
2013-04-18 18:48:44 +00:00
|
|
|
if ( $this->hasOption( 'debug' ) ) {
|
2011-05-21 23:22:47 +00:00
|
|
|
$importer->setDebug( true );
|
|
|
|
|
}
|
2011-08-02 14:05:01 +00:00
|
|
|
if ( $this->hasOption( 'no-updates' ) ) {
|
|
|
|
|
$importer->setNoUpdates( true );
|
|
|
|
|
}
|
2017-10-25 19:26:53 +00:00
|
|
|
if ( $this->hasOption( 'username-prefix' ) ) {
|
|
|
|
|
$importer->setUsernamePrefix(
|
|
|
|
|
$this->getOption( 'username-prefix' ),
|
|
|
|
|
!$this->hasOption( 'no-local-users' )
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-12-26 03:49:48 +00:00
|
|
|
if ( $this->hasOption( 'rootpage' ) ) {
|
|
|
|
|
$statusRootPage = $importer->setTargetRootPage( $this->getOption( 'rootpage' ) );
|
|
|
|
|
if ( !$statusRootPage->isGood() ) {
|
|
|
|
|
// Die here so that it doesn't print "Done!"
|
2019-08-19 16:12:32 +00:00
|
|
|
$this->fatalError( $statusRootPage->getMessage( false, false, 'en' )->text() );
|
2015-12-26 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-05-26 01:54:32 +00:00
|
|
|
if ( $this->hasOption( 'skip-to' ) ) {
|
|
|
|
|
$nthPage = (int)$this->getOption( 'skip-to' );
|
|
|
|
|
$importer->setPageOffset( $nthPage );
|
|
|
|
|
$this->pageCount = $nthPage - 1;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$importer->setPageCallback( [ $this, 'reportPage' ] );
|
2021-02-10 22:31:02 +00:00
|
|
|
$importer->setNoticeCallback( static function ( $msg, $params ) {
|
2017-12-22 03:12:28 +00:00
|
|
|
echo wfMessage( $msg, $params )->text() . "\n";
|
|
|
|
|
} );
|
2013-04-13 11:36:24 +00:00
|
|
|
$this->importCallback = $importer->setRevisionCallback(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $this, 'handleRevision' ] );
|
2008-03-06 07:24:29 +00:00
|
|
|
$this->uploadCallback = $importer->setUploadCallback(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $this, 'handleUpload' ] );
|
2008-09-18 20:52:34 +00:00
|
|
|
$this->logItemCallback = $importer->setLogItemCallback(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $this, 'handleLogItem' ] );
|
2011-05-15 10:39:15 +00:00
|
|
|
if ( $this->uploads ) {
|
|
|
|
|
$importer->setImportUploads( true );
|
|
|
|
|
}
|
|
|
|
|
if ( $this->imageBasePath ) {
|
|
|
|
|
$importer->setImageBasePath( $this->imageBasePath );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-06-25 21:03:48 +00:00
|
|
|
if ( $this->dryRun ) {
|
|
|
|
|
$importer->setPageOutCallback( null );
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-09-17 11:10:15 +00:00
|
|
|
return $importer->doImport();
|
2005-07-05 03:16:56 +00:00
|
|
|
}
|
2005-09-17 11:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = BackupReader::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|