2006-05-15 10:57:52 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-07-17 05:40:40 +00:00
|
|
|
* Import one or more images from the local file system into the wiki without
|
|
|
|
|
* using the web-based interface.
|
2010-02-07 16:10:14 +00:00
|
|
|
*
|
|
|
|
|
* "Smart import" additions:
|
2014-04-22 21:25:04 +00:00
|
|
|
* - aim: preserve the essential metadata (user, description) when importing media
|
|
|
|
|
* files from an existing wiki.
|
2010-02-07 16:10:14 +00:00
|
|
|
* - process:
|
|
|
|
|
* - interface with the source wiki, don't use bare files only (see --source-wiki-url).
|
|
|
|
|
* - fetch metadata from source wiki for each file to import.
|
|
|
|
|
* - commit the fetched metadata to the destination wiki while submitting.
|
2006-05-15 10:57:52 +00:00
|
|
|
*
|
2010-12-16 19:15:12 +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
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* 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
|
2006-05-15 10:57:52 +00:00
|
|
|
* @author Rob Church <robchur@gmail.com>
|
2010-02-07 16:10:14 +00:00
|
|
|
* @author Mij <mij@bitchx.it>
|
2006-05-15 10:57:52 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-12-16 22:45:20 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-12-16 22:45:20 +00:00
|
|
|
|
2022-07-10 21:18:31 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2024-08-11 16:32:58 +00:00
|
|
|
use MediaWiki\Maintenance\Maintenance;
|
2023-09-13 16:09:30 +00:00
|
|
|
use MediaWiki\Specials\SpecialUpload;
|
2022-10-25 16:58:49 +00:00
|
|
|
use MediaWiki\StubObject\StubGlobalUser;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2024-09-27 19:20:56 +00:00
|
|
|
use Wikimedia\FileBackend\FSFile\FSFile;
|
2019-07-19 18:32:08 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
class ImportImages extends Maintenance {
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
|
|
$this->addDescription( 'Imports images and other media files into the wiki' );
|
|
|
|
|
$this->addArg( 'dir', 'Path to the directory containing images to be imported' );
|
|
|
|
|
|
|
|
|
|
$this->addOption( 'extensions',
|
|
|
|
|
'Comma-separated list of allowable extensions, defaults to $wgFileExtensions',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'overwrite',
|
|
|
|
|
'Overwrite existing images with the same name (default is to skip them)' );
|
|
|
|
|
$this->addOption( 'limit',
|
|
|
|
|
'Limit the number of images to process. Ignored or skipped images are not counted',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'from',
|
|
|
|
|
"Ignore all files until the one with the given name. Useful for resuming aborted "
|
|
|
|
|
. "imports. The name should be the file's canonical database form.",
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'skip-dupes',
|
|
|
|
|
'Skip images that were already uploaded under a different name (check SHA1)' );
|
|
|
|
|
$this->addOption( 'search-recursively', 'Search recursively for files in subdirectories' );
|
|
|
|
|
$this->addOption( 'sleep',
|
|
|
|
|
'Sleep between files. Useful mostly for debugging',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'user',
|
|
|
|
|
"Set username of uploader, default 'Maintenance script'",
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
// This parameter can optionally have an argument. If none specified, getOption()
|
|
|
|
|
// returns 1 which is precisely what we need.
|
|
|
|
|
$this->addOption( 'check-userblock', 'Check if the user got blocked during import' );
|
|
|
|
|
$this->addOption( 'comment',
|
|
|
|
|
"Set file description, default 'Importing file'",
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'comment-file',
|
|
|
|
|
'Set description to the content of this file',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'comment-ext',
|
|
|
|
|
'Causes the description for each file to be loaded from a file with the same name, but '
|
|
|
|
|
. 'the extension provided. If a global description is also given, it is appended.',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'summary',
|
|
|
|
|
'Upload summary, description will be used if not provided',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'license',
|
|
|
|
|
'Use an optional license template',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'timestamp',
|
|
|
|
|
'Override upload time/date, all MediaWiki timestamp formats are accepted',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'protect',
|
|
|
|
|
'Specify the protect value (autoconfirmed,sysop)',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'unprotect', 'Unprotects all uploaded images' );
|
|
|
|
|
$this->addOption( 'source-wiki-url',
|
|
|
|
|
'If specified, take User and Comment data for each imported file from this URL. '
|
2022-09-05 16:12:45 +00:00
|
|
|
. 'For example, --source-wiki-url="https://en.wikipedia.org/w/',
|
2016-12-16 22:45:20 +00:00
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'dry', "Dry run, don't import anything" );
|
|
|
|
|
}
|
2016-02-03 22:58:53 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
public function execute() {
|
2023-08-31 09:21:12 +00:00
|
|
|
$services = $this->getServiceContainer();
|
2020-11-12 20:36:35 +00:00
|
|
|
$permissionManager = $services->getPermissionManager();
|
2019-07-19 18:32:08 +00:00
|
|
|
|
2023-03-03 03:08:52 +00:00
|
|
|
$found = 0;
|
|
|
|
|
$processed = 0;
|
2022-06-27 08:00:01 +00:00
|
|
|
$statistics = [
|
|
|
|
|
'ignored' => 0,
|
|
|
|
|
'added' => 0,
|
|
|
|
|
'skipped' => 0,
|
|
|
|
|
'overwritten' => 0,
|
|
|
|
|
'failed' => 0,
|
|
|
|
|
];
|
2007-07-28 22:21:23 +00:00
|
|
|
|
2019-06-28 20:15:13 +00:00
|
|
|
$this->output( "Importing Files\n\n" );
|
2006-05-15 10:57:52 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$dir = $this->getArg( 0 );
|
2011-10-19 20:08:15 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
# Check Protection
|
|
|
|
|
if ( $this->hasOption( 'protect' ) && $this->hasOption( 'unprotect' ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Cannot specify both protect and unprotect. Only 1 is allowed.\n" );
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2011-10-19 20:08:15 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( $this->hasOption( 'protect' ) && trim( $this->getOption( 'protect' ) ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "You must specify a protection option.\n" );
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2011-10-19 20:08:15 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
# Prepare the list of allowed extensions
|
|
|
|
|
$extensions = $this->hasOption( 'extensions' )
|
|
|
|
|
? explode( ',', strtolower( $this->getOption( 'extensions' ) ) )
|
2022-07-10 21:18:31 +00:00
|
|
|
: $this->getConfig()->get( MainConfigNames::FileExtensions );
|
2011-10-19 20:08:15 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
# Search the path provided for candidates for import
|
|
|
|
|
$files = $this->findFiles( $dir, $extensions, $this->hasOption( 'search-recursively' ) );
|
2023-02-11 08:44:09 +00:00
|
|
|
if ( !$files->valid() ) {
|
2022-07-17 10:12:25 +00:00
|
|
|
$this->output( "No suitable files could be found for import.\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-10-02 10:04:41 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
# Initialise the user for this operation
|
|
|
|
|
$user = $this->hasOption( 'user' )
|
|
|
|
|
? User::newFromName( $this->getOption( 'user' ) )
|
2021-05-03 21:34:26 +00:00
|
|
|
: User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( !$user instanceof User ) {
|
2021-05-03 21:34:26 +00:00
|
|
|
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2021-10-25 19:15:52 +00:00
|
|
|
'@phan-var User $user';
|
2021-09-04 19:19:47 +00:00
|
|
|
StubGlobalUser::setUser( $user );
|
2016-12-16 22:45:20 +00:00
|
|
|
|
|
|
|
|
# Get block check. If a value is given, this specified how often the check is performed
|
|
|
|
|
$checkUserBlock = (int)$this->getOption( 'check-userblock' );
|
|
|
|
|
|
|
|
|
|
$from = $this->getOption( 'from' );
|
|
|
|
|
$sleep = (int)$this->getOption( 'sleep' );
|
|
|
|
|
$limit = (int)$this->getOption( 'limit' );
|
|
|
|
|
$timestamp = $this->getOption( 'timestamp', false );
|
|
|
|
|
|
|
|
|
|
# Get the upload comment. Provide a default one in case there's no comment given.
|
|
|
|
|
$commentFile = $this->getOption( 'comment-file' );
|
|
|
|
|
if ( $commentFile !== null ) {
|
|
|
|
|
$comment = file_get_contents( $commentFile );
|
|
|
|
|
if ( $comment === false || $comment === null ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "failed to read comment file: {$commentFile}\n" );
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$comment = $this->getOption( 'comment', 'Importing file' );
|
|
|
|
|
}
|
|
|
|
|
$commentExt = $this->getOption( 'comment-ext' );
|
|
|
|
|
$summary = $this->getOption( 'summary', '' );
|
|
|
|
|
$license = $this->getOption( 'license', '' );
|
|
|
|
|
$sourceWikiUrl = $this->getOption( 'source-wiki-url' );
|
2009-10-02 09:54:20 +00:00
|
|
|
|
2021-04-17 19:38:23 +00:00
|
|
|
$tags = in_array( ChangeTags::TAG_SERVER_SIDE_UPLOAD, ChangeTags::getSoftwareTags() )
|
|
|
|
|
? [ ChangeTags::TAG_SERVER_SIDE_UPLOAD ]
|
|
|
|
|
: [];
|
|
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
# Batch "upload" operation
|
2022-07-17 10:12:25 +00:00
|
|
|
$restrictionStore = $services->getRestrictionStore();
|
|
|
|
|
foreach ( $files as $file ) {
|
2023-02-11 08:44:09 +00:00
|
|
|
$found++;
|
2022-07-17 10:12:25 +00:00
|
|
|
if ( $sleep && ( $processed > 0 ) ) {
|
|
|
|
|
sleep( $sleep );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$base = UtfNormal\Validator::cleanUp( wfBaseName( $file ) );
|
2017-01-16 15:14:34 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
# Validate a title
|
|
|
|
|
$title = Title::makeTitleSafe( NS_FILE, $base );
|
2022-06-27 08:00:01 +00:00
|
|
|
if ( !$title ) {
|
2022-07-17 10:12:25 +00:00
|
|
|
$this->output(
|
|
|
|
|
"{$base} could not be imported; a valid title cannot be produced\n"
|
|
|
|
|
);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $from ) {
|
2022-06-27 08:00:01 +00:00
|
|
|
if ( $from !== $title->getDBkey() ) {
|
|
|
|
|
$statistics['ignored']++;
|
2022-07-17 10:12:25 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-06-27 08:00:01 +00:00
|
|
|
// Found the requested file, continue from here
|
|
|
|
|
$from = null;
|
2022-07-17 10:12:25 +00:00
|
|
|
}
|
2009-10-02 09:54:20 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
|
|
|
|
|
$user->clearInstanceCache( 'name' ); // reload from DB!
|
|
|
|
|
if ( $permissionManager->isBlockedFrom( $user, $title ) ) {
|
2016-12-16 22:45:20 +00:00
|
|
|
$this->output(
|
2022-07-17 10:12:25 +00:00
|
|
|
"{$user->getName()} is blocked from {$title->getPrefixedText()}! skipping.\n"
|
2019-09-05 17:38:22 +00:00
|
|
|
);
|
2022-06-27 08:00:01 +00:00
|
|
|
$statistics['skipped']++;
|
2016-12-16 22:45:20 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
}
|
2012-09-26 16:02:47 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
# Check existence
|
|
|
|
|
$image = $services->getRepoGroup()->getLocalRepo()
|
|
|
|
|
->newFile( $title );
|
|
|
|
|
if ( $image->exists() ) {
|
|
|
|
|
if ( $this->hasOption( 'overwrite' ) ) {
|
|
|
|
|
$this->output( "{$base} exists, overwriting..." );
|
|
|
|
|
$svar = 'overwritten';
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "{$base} exists, skipping\n" );
|
2022-06-27 08:00:01 +00:00
|
|
|
$statistics['skipped']++;
|
2022-07-17 10:12:25 +00:00
|
|
|
continue;
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
} else {
|
|
|
|
|
if ( $this->hasOption( 'skip-dupes' ) ) {
|
|
|
|
|
$repo = $image->getRepo();
|
|
|
|
|
# XXX: we end up calculating this again when actually uploading. that sucks.
|
|
|
|
|
$sha1 = FSFile::getSha1Base36FromPath( $file );
|
|
|
|
|
$dupes = $repo->findBySha1( $sha1 );
|
|
|
|
|
if ( $dupes ) {
|
2019-07-19 18:32:08 +00:00
|
|
|
$this->output(
|
2022-07-17 10:12:25 +00:00
|
|
|
"{$base} already exists as {$dupes[0]->getName()}, skipping\n"
|
2019-07-19 18:32:08 +00:00
|
|
|
);
|
2022-06-27 08:00:01 +00:00
|
|
|
$statistics['skipped']++;
|
2019-07-19 18:32:08 +00:00
|
|
|
continue;
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-11-27 13:07:30 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
$this->output( "Importing {$base}..." );
|
|
|
|
|
$svar = 'added';
|
|
|
|
|
}
|
2007-05-09 19:09:33 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
if ( $sourceWikiUrl ) {
|
|
|
|
|
/* find comment text directly from source wiki, through MW's API */
|
|
|
|
|
$real_comment = $this->getFileCommentFromSourceWiki( $sourceWikiUrl, $base );
|
2022-06-27 08:00:01 +00:00
|
|
|
$commentText = $real_comment !== false ? $real_comment : $comment;
|
2012-09-26 16:02:47 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
/* find user directly from source wiki, through MW's API */
|
|
|
|
|
$real_user = $this->getFileUserFromSourceWiki( $sourceWikiUrl, $base );
|
2022-06-27 08:00:01 +00:00
|
|
|
if ( $real_user !== false ) {
|
2022-07-17 10:12:25 +00:00
|
|
|
$realUser = User::newFromName( $real_user );
|
|
|
|
|
if ( $realUser === false ) {
|
|
|
|
|
# user does not exist in target wiki
|
|
|
|
|
$this->output(
|
|
|
|
|
"failed: user '$real_user' does not exist in target wiki."
|
|
|
|
|
);
|
|
|
|
|
continue;
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
StubGlobalUser::setUser( $realUser );
|
|
|
|
|
$user = $realUser;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
# Find comment text
|
|
|
|
|
$commentText = false;
|
|
|
|
|
|
|
|
|
|
if ( $commentExt ) {
|
|
|
|
|
$f = $this->findAuxFile( $file, $commentExt );
|
|
|
|
|
if ( !$f ) {
|
|
|
|
|
$this->output( " No comment file with extension {$commentExt} found "
|
|
|
|
|
. "for {$file}, using default comment." );
|
2016-12-16 22:45:20 +00:00
|
|
|
} else {
|
2022-07-17 10:12:25 +00:00
|
|
|
$commentText = file_get_contents( $f );
|
|
|
|
|
if ( !$commentText ) {
|
2016-12-16 22:45:20 +00:00
|
|
|
$this->output(
|
2022-07-17 10:12:25 +00:00
|
|
|
" Failed to load comment file {$f}, using default comment."
|
2019-09-05 17:38:22 +00:00
|
|
|
);
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
}
|
2010-09-12 15:39:49 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
if ( !$commentText ) {
|
|
|
|
|
$commentText = $comment;
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
}
|
2010-09-12 15:39:49 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
# Import the file
|
|
|
|
|
if ( $this->hasOption( 'dry' ) ) {
|
|
|
|
|
$this->output(
|
|
|
|
|
" publishing {$file} by '{$user->getName()}', comment '$commentText'..."
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$mwProps = new MWFileProps( $services->getMimeAnalyzer() );
|
|
|
|
|
$props = $mwProps->getPropsFromPath( $file, true );
|
|
|
|
|
$flags = 0;
|
|
|
|
|
$publishOptions = [];
|
|
|
|
|
$handler = MediaHandler::getHandler( $props['mime'] );
|
|
|
|
|
if ( $handler ) {
|
|
|
|
|
$publishOptions['headers'] = $handler->getContentHeaders( $props['metadata'] );
|
2016-12-16 22:45:20 +00:00
|
|
|
} else {
|
2022-07-17 10:12:25 +00:00
|
|
|
$publishOptions['headers'] = [];
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
$archive = $image->publish( $file, $flags, $publishOptions );
|
|
|
|
|
if ( !$archive->isGood() ) {
|
|
|
|
|
$this->output( "failed. (" .
|
|
|
|
|
$archive->getMessage( false, false, 'en' )->text() .
|
|
|
|
|
")\n" );
|
2022-06-27 08:00:01 +00:00
|
|
|
$statistics['failed']++;
|
2022-07-17 10:12:25 +00:00
|
|
|
continue;
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
}
|
2010-09-12 15:39:49 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
$commentText = SpecialUpload::getInitialPageText( $commentText, $license );
|
|
|
|
|
if ( !$this->hasOption( 'summary' ) ) {
|
|
|
|
|
$summary = $commentText;
|
|
|
|
|
}
|
2016-12-16 22:45:20 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
if ( $this->hasOption( 'dry' ) ) {
|
|
|
|
|
$this->output( "done.\n" );
|
|
|
|
|
} elseif ( $image->recordUpload3(
|
|
|
|
|
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable
|
|
|
|
|
$archive->value,
|
|
|
|
|
$summary,
|
|
|
|
|
$commentText,
|
|
|
|
|
$user,
|
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable
|
|
|
|
|
$props,
|
|
|
|
|
$timestamp,
|
|
|
|
|
$tags
|
|
|
|
|
)->isOK() ) {
|
|
|
|
|
$this->output( "done.\n" );
|
|
|
|
|
|
|
|
|
|
$doProtect = false;
|
|
|
|
|
|
|
|
|
|
$protectLevel = $this->getOption( 'protect' );
|
|
|
|
|
$restrictionLevels = $this->getConfig()->get( MainConfigNames::RestrictionLevels );
|
|
|
|
|
|
|
|
|
|
if ( $protectLevel && in_array( $protectLevel, $restrictionLevels ) ) {
|
|
|
|
|
$doProtect = true;
|
|
|
|
|
}
|
|
|
|
|
if ( $this->hasOption( 'unprotect' ) ) {
|
|
|
|
|
$protectLevel = '';
|
|
|
|
|
$doProtect = true;
|
|
|
|
|
}
|
2016-12-16 22:45:20 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
if ( $doProtect ) {
|
|
|
|
|
# Protect the file
|
|
|
|
|
$this->output( "\nWaiting for replica DBs...\n" );
|
|
|
|
|
// Wait for replica DBs.
|
|
|
|
|
sleep( 2 ); # Why this sleep?
|
2022-10-24 18:31:49 +00:00
|
|
|
$this->waitForReplication();
|
2016-12-16 22:45:20 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
$this->output( "\nSetting image restrictions ..." );
|
2016-12-16 22:45:20 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
$cascade = false;
|
|
|
|
|
$restrictions = [];
|
|
|
|
|
foreach ( $restrictionStore->listApplicableRestrictionTypes( $title ) as $type ) {
|
|
|
|
|
$restrictions[$type] = $protectLevel;
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
|
|
|
|
|
$page = $services->getWikiPageFactory()->newFromTitle( $title );
|
|
|
|
|
$status = $page->doUpdateRestrictions( $restrictions, [], $cascade, '', $user );
|
|
|
|
|
$this->output( ( $status->isOK() ? 'done' : 'failed' ) . "\n" );
|
2007-07-28 22:21:23 +00:00
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
} else {
|
|
|
|
|
$this->output( "failed. (at recordUpload stage)\n" );
|
|
|
|
|
$svar = 'failed';
|
|
|
|
|
}
|
2009-10-02 10:13:21 +00:00
|
|
|
|
2022-06-27 08:00:01 +00:00
|
|
|
$statistics[$svar]++;
|
2022-07-17 10:12:25 +00:00
|
|
|
$processed++;
|
2009-10-02 10:13:21 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
if ( $limit && $processed >= $limit ) {
|
|
|
|
|
break;
|
2013-04-18 18:48:44 +00:00
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
}
|
2011-10-19 20:08:15 +00:00
|
|
|
|
2022-07-17 10:12:25 +00:00
|
|
|
# Print out some statistics
|
|
|
|
|
$this->output( "\n" );
|
2022-06-27 08:00:01 +00:00
|
|
|
foreach ( array_merge(
|
2022-07-17 10:12:25 +00:00
|
|
|
[
|
2023-02-11 08:44:09 +00:00
|
|
|
'Found' => $found,
|
2022-07-17 10:12:25 +00:00
|
|
|
'Limit' => $limit,
|
2022-06-27 08:00:01 +00:00
|
|
|
],
|
|
|
|
|
$statistics
|
|
|
|
|
) as $desc => $number ) {
|
2022-07-17 10:12:25 +00:00
|
|
|
if ( $number > 0 ) {
|
2022-06-27 08:00:01 +00:00
|
|
|
$this->output( ucfirst( $desc ) . ": $number\n" );
|
2007-06-05 18:39:02 +00:00
|
|
|
}
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search a directory for files with one of a set of extensions
|
|
|
|
|
*
|
|
|
|
|
* @param string $dir Path to directory to search
|
2022-06-27 08:00:01 +00:00
|
|
|
* @param array $exts Array of lowercase extensions to search for
|
2016-12-16 22:45:20 +00:00
|
|
|
* @param bool $recurse Search subdirectories recursively
|
2024-08-11 16:32:58 +00:00
|
|
|
* @return \Generator<string> Generator that iterating filenames
|
2016-12-16 22:45:20 +00:00
|
|
|
*/
|
|
|
|
|
private function findFiles( $dir, $exts, $recurse = false ) {
|
2022-06-27 08:00:01 +00:00
|
|
|
$dhl = is_dir( $dir ) ? opendir( $dir ) : false;
|
2022-07-17 10:12:25 +00:00
|
|
|
if ( !$dhl ) {
|
2023-02-11 08:44:09 +00:00
|
|
|
return;
|
2012-09-26 16:02:47 +00:00
|
|
|
}
|
2022-07-17 10:12:25 +00:00
|
|
|
|
2024-08-31 21:34:30 +00:00
|
|
|
// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
|
2022-07-17 10:12:25 +00:00
|
|
|
while ( ( $file = readdir( $dhl ) ) !== false ) {
|
|
|
|
|
if ( is_file( $dir . '/' . $file ) ) {
|
|
|
|
|
$ext = pathinfo( $file, PATHINFO_EXTENSION );
|
|
|
|
|
if ( in_array( strtolower( $ext ), $exts ) ) {
|
2023-02-11 08:44:09 +00:00
|
|
|
yield $dir . '/' . $file;
|
2022-07-17 10:12:25 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( $recurse && is_dir( $dir . '/' . $file ) && $file !== '..' && $file !== '.' ) {
|
2023-02-11 08:44:09 +00:00
|
|
|
yield from $this->findFiles( $dir . '/' . $file, $exts, true );
|
2022-07-17 10:12:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
2012-09-26 16:02:47 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
/**
|
2022-05-09 09:09:00 +00:00
|
|
|
* Find an auxiliary file with the given extension, matching
|
2016-12-16 22:45:20 +00:00
|
|
|
* the give base file path. $maxStrip determines how many extensions
|
|
|
|
|
* may be stripped from the original file name before appending the
|
|
|
|
|
* new extension. For example, with $maxStrip = 1 (the default),
|
|
|
|
|
* file files acme.foo.bar.txt and acme.foo.txt would be auxilliary
|
|
|
|
|
* files for acme.foo.bar and the extension ".txt". With $maxStrip = 2,
|
|
|
|
|
* acme.txt would also be acceptable.
|
|
|
|
|
*
|
|
|
|
|
* @param string $file Base path
|
|
|
|
|
* @param string $auxExtension The extension to be appended to the base path
|
|
|
|
|
* @param int $maxStrip The maximum number of extensions to strip from the base path (default: 1)
|
2022-11-09 00:11:22 +00:00
|
|
|
* @return string|false
|
2016-12-16 22:45:20 +00:00
|
|
|
*/
|
|
|
|
|
private function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
|
2022-06-27 08:00:01 +00:00
|
|
|
if ( !str_starts_with( $auxExtension, '.' ) ) {
|
2016-12-16 22:45:20 +00:00
|
|
|
$auxExtension = '.' . $auxExtension;
|
|
|
|
|
}
|
2010-09-12 15:39:49 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$d = dirname( $file );
|
|
|
|
|
$n = basename( $file );
|
2009-03-03 02:56:30 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
while ( $maxStrip >= 0 ) {
|
|
|
|
|
$f = $d . '/' . $n . $auxExtension;
|
2011-12-18 16:01:31 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( file_exists( $f ) ) {
|
|
|
|
|
return $f;
|
2011-12-18 16:01:31 +00:00
|
|
|
}
|
2009-03-03 02:56:30 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$idx = strrpos( $n, '.' );
|
|
|
|
|
if ( !$idx ) {
|
|
|
|
|
break;
|
2007-06-05 18:39:02 +00:00
|
|
|
}
|
2009-10-02 09:54:20 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$n = substr( $n, 0, $idx );
|
|
|
|
|
$maxStrip -= 1;
|
2007-07-28 22:21:23 +00:00
|
|
|
}
|
2010-09-12 15:39:49 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
return false;
|
2011-10-19 20:08:15 +00:00
|
|
|
}
|
2010-09-12 15:39:49 +00:00
|
|
|
|
2020-05-19 21:49:33 +00:00
|
|
|
/**
|
2021-11-22 13:35:17 +00:00
|
|
|
* @todo FIXME: Access the api in a better way and performing just one query
|
2020-05-19 21:49:33 +00:00
|
|
|
* (preferably batching files too).
|
|
|
|
|
*
|
|
|
|
|
* @param string $wiki_host
|
|
|
|
|
* @param string $file
|
|
|
|
|
*
|
2022-11-09 00:11:22 +00:00
|
|
|
* @return string|false
|
2020-05-19 21:49:33 +00:00
|
|
|
*/
|
2016-12-16 22:45:20 +00:00
|
|
|
private function getFileCommentFromSourceWiki( $wiki_host, $file ) {
|
|
|
|
|
$url = $wiki_host . '/api.php?action=query&format=xml&titles=File:'
|
2017-02-25 21:53:36 +00:00
|
|
|
. rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment';
|
2023-08-31 09:21:12 +00:00
|
|
|
$body = $this->getServiceContainer()->getHttpRequestFactory()->get( $url, [], __METHOD__ );
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( preg_match( '#<ii comment="([^"]*)" />#', $body, $matches ) == 0 ) {
|
|
|
|
|
return false;
|
2013-04-18 18:48:44 +00:00
|
|
|
}
|
2016-12-16 22:45:20 +00:00
|
|
|
|
|
|
|
|
return html_entity_decode( $matches[1] );
|
2006-05-15 10:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
private function getFileUserFromSourceWiki( $wiki_host, $file ) {
|
|
|
|
|
$url = $wiki_host . '/api.php?action=query&format=xml&titles=File:'
|
2017-02-25 21:53:36 +00:00
|
|
|
. rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user';
|
2023-08-31 09:21:12 +00:00
|
|
|
$body = $this->getServiceContainer()->getHttpRequestFactory()->get( $url, [], __METHOD__ );
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( preg_match( '#<ii user="([^"]*)" />#', $body, $matches ) == 0 ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2006-05-15 10:57:52 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
return html_entity_decode( $matches[1] );
|
2007-05-09 19:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
2009-12-07 08:51:52 +00:00
|
|
|
}
|
2016-12-16 22:45:20 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = ImportImages::class;
|
2016-12-16 22:45:20 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|