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
|
|
|
*/
|
|
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
|
|
|
|
|
2019-07-19 18:32:08 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
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. '
|
|
|
|
|
. 'For example, --source-wiki-url="http://en.wikipedia.org/',
|
|
|
|
|
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() {
|
2021-09-04 19:19:47 +00:00
|
|
|
global $wgFileExtensions, $wgRestrictionLevels;
|
2016-02-03 22:58:53 +00:00
|
|
|
|
2020-11-12 20:36:35 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$permissionManager = $services->getPermissionManager();
|
2019-07-19 18:32:08 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$processed = $added = $ignored = $skipped = $overwritten = $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' ) ) )
|
|
|
|
|
: $wgFileExtensions;
|
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' ) );
|
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', '' );
|
2009-10-02 09:54:20 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$license = $this->getOption( 'license', '' );
|
2014-04-22 21:25:04 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$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
|
|
|
|
|
$count = count( $files );
|
|
|
|
|
if ( $count > 0 ) {
|
2020-11-12 20:36:35 +00:00
|
|
|
$lbFactory = $services->getDBLoadBalancerFactory();
|
2022-04-13 20:03:34 +00:00
|
|
|
$restrictionStore = $services->getRestrictionStore();
|
2016-12-16 22:45:20 +00:00
|
|
|
foreach ( $files as $file ) {
|
2017-01-16 15:14:34 +00:00
|
|
|
if ( $sleep && ( $processed > 0 ) ) {
|
|
|
|
|
sleep( $sleep );
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$base = UtfNormal\Validator::cleanUp( wfBaseName( $file ) );
|
2009-10-02 09:54:20 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
# Validate a title
|
|
|
|
|
$title = Title::makeTitleSafe( NS_FILE, $base );
|
|
|
|
|
if ( !is_object( $title ) ) {
|
|
|
|
|
$this->output(
|
2019-09-05 17:38:22 +00:00
|
|
|
"{$base} could not be imported; a valid title cannot be produced\n"
|
|
|
|
|
);
|
2016-12-16 22:45:20 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2012-09-26 16:02:47 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( $from ) {
|
|
|
|
|
if ( $from == $title->getDBkey() ) {
|
|
|
|
|
$from = null;
|
|
|
|
|
} else {
|
|
|
|
|
$ignored++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-11-27 13:07:30 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
|
|
|
|
|
$user->clearInstanceCache( 'name' ); // reload from DB!
|
2019-07-19 18:32:08 +00:00
|
|
|
if ( $permissionManager->isBlockedFrom( $user, $title ) ) {
|
|
|
|
|
$this->output(
|
|
|
|
|
"{$user->getName()} is blocked from {$title->getPrefixedText()}! skipping.\n"
|
|
|
|
|
);
|
|
|
|
|
$skipped++;
|
|
|
|
|
continue;
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-11-27 13:07:30 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
# Check existence
|
2020-11-12 20:36:35 +00:00
|
|
|
$image = $services->getRepoGroup()->getLocalRepo()
|
2019-05-14 17:00:34 +00:00
|
|
|
->newFile( $title );
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( $image->exists() ) {
|
|
|
|
|
if ( $this->hasOption( 'overwrite' ) ) {
|
|
|
|
|
$this->output( "{$base} exists, overwriting..." );
|
|
|
|
|
$svar = 'overwritten';
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "{$base} exists, skipping\n" );
|
|
|
|
|
$skipped++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} 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 ) {
|
|
|
|
|
$this->output(
|
2019-09-05 17:38:22 +00:00
|
|
|
"{$base} already exists as {$dupes[0]->getName()}, skipping\n"
|
|
|
|
|
);
|
2016-12-16 22:45:20 +00:00
|
|
|
$skipped++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-05-09 19:09:33 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$this->output( "Importing {$base}..." );
|
|
|
|
|
$svar = 'added';
|
|
|
|
|
}
|
2012-09-26 16:02:47 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( $sourceWikiUrl ) {
|
|
|
|
|
/* find comment text directly from source wiki, through MW's API */
|
|
|
|
|
$real_comment = $this->getFileCommentFromSourceWiki( $sourceWikiUrl, $base );
|
|
|
|
|
if ( $real_comment === false ) {
|
|
|
|
|
$commentText = $comment;
|
|
|
|
|
} else {
|
|
|
|
|
$commentText = $real_comment;
|
|
|
|
|
}
|
2007-05-09 19:09:33 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
/* find user directly from source wiki, through MW's API */
|
|
|
|
|
$real_user = $this->getFileUserFromSourceWiki( $sourceWikiUrl, $base );
|
|
|
|
|
if ( $real_user === false ) {
|
2020-06-16 01:45:46 +00:00
|
|
|
// don't change $wgUser
|
2016-12-16 22:45:20 +00:00
|
|
|
} else {
|
2020-06-16 01:45:46 +00:00
|
|
|
$realUser = User::newFromName( $real_user );
|
|
|
|
|
if ( $realUser === false ) {
|
2016-12-16 22:45:20 +00:00
|
|
|
# user does not exist in target wiki
|
|
|
|
|
$this->output(
|
2019-09-05 17:38:22 +00:00
|
|
|
"failed: user '$real_user' does not exist in target wiki."
|
|
|
|
|
);
|
2016-12-16 22:45:20 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2021-09-04 19:19:47 +00:00
|
|
|
StubGlobalUser::setUser( $realUser );
|
2020-06-16 01:45:46 +00:00
|
|
|
$user = $realUser;
|
2016-12-16 22:45:20 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
# Find comment text
|
|
|
|
|
$commentText = false;
|
|
|
|
|
|
|
|
|
|
if ( $commentExt ) {
|
|
|
|
|
$f = $this->findAuxFile( $file, $commentExt );
|
|
|
|
|
if ( !$f ) {
|
|
|
|
|
$this->output( " No comment file with extension {$commentExt} found "
|
2021-09-03 22:52:31 +00:00
|
|
|
. "for {$file}, using default comment." );
|
2016-12-16 22:45:20 +00:00
|
|
|
} else {
|
|
|
|
|
$commentText = file_get_contents( $f );
|
|
|
|
|
if ( !$commentText ) {
|
|
|
|
|
$this->output(
|
2021-07-21 16:34:05 +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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-12 15:39:49 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( !$commentText ) {
|
|
|
|
|
$commentText = $comment;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-12 15:39:49 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
# Import the file
|
|
|
|
|
if ( $this->hasOption( 'dry' ) ) {
|
|
|
|
|
$this->output(
|
2021-07-21 16:34:05 +00:00
|
|
|
" publishing {$file} by '{$user->getName()}', comment '$commentText'..."
|
2016-12-16 22:45:20 +00:00
|
|
|
);
|
|
|
|
|
} else {
|
2020-11-12 20:36:35 +00:00
|
|
|
$mwProps = new MWFileProps( $services->getMimeAnalyzer() );
|
2016-12-16 22:45:20 +00:00
|
|
|
$props = $mwProps->getPropsFromPath( $file, true );
|
|
|
|
|
$flags = 0;
|
|
|
|
|
$publishOptions = [];
|
|
|
|
|
$handler = MediaHandler::getHandler( $props['mime'] );
|
|
|
|
|
if ( $handler ) {
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
$publishOptions['headers'] = $handler->getContentHeaders( $props['metadata'] );
|
2016-12-16 22:45:20 +00:00
|
|
|
} else {
|
|
|
|
|
$publishOptions['headers'] = [];
|
|
|
|
|
}
|
|
|
|
|
$archive = $image->publish( $file, $flags, $publishOptions );
|
|
|
|
|
if ( !$archive->isGood() ) {
|
|
|
|
|
$this->output( "failed. (" .
|
2021-09-03 22:52:31 +00:00
|
|
|
$archive->getMessage( false, false, 'en' )->text() .
|
|
|
|
|
")\n" );
|
2016-12-16 22:45:20 +00:00
|
|
|
$failed++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-19 20:08:15 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$commentText = SpecialUpload::getInitialPageText( $commentText, $license );
|
|
|
|
|
if ( !$this->hasOption( 'summary' ) ) {
|
|
|
|
|
$summary = $commentText;
|
|
|
|
|
}
|
2010-09-12 15:39:49 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( $this->hasOption( 'dry' ) ) {
|
|
|
|
|
$this->output( "done.\n" );
|
2020-06-16 01:45:46 +00:00
|
|
|
} elseif ( $image->recordUpload3(
|
2022-03-29 18:11:06 +00:00
|
|
|
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable
|
2016-12-16 22:45:20 +00:00
|
|
|
$archive->value,
|
|
|
|
|
$summary,
|
|
|
|
|
$commentText,
|
2020-06-16 01:45:46 +00:00
|
|
|
$user,
|
2022-03-29 18:11:06 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable
|
2016-12-16 22:45:20 +00:00
|
|
|
$props,
|
2021-04-17 19:38:23 +00:00
|
|
|
$timestamp,
|
|
|
|
|
$tags
|
Block same-file reuploads
When uploading a file, there are a few ways of checking for and blocking
(or at least warning about) duplicate uploads already.
However, it occasionally seems to happen that files get uploaded twice.
The exact same file, usually - submitted at (almost) the exact same time
(possibly some error in whatever submits the file upload, but still)
Given 2 uploads at (almost) the exact same time, both of them are stored,
even if they are the exact same files.
The last upload also ends up with a `logging` entry with `log_page = 0`.
I don’t believe such upload should go through: if we do find that a file
is an exact duplicate of something that already exists, I don’t see any
reason it should go through.
Note that with this patch, it will become impossible to reupload a file
with the exact same hash (which was possible before.)
If we still want to allow same-file reuploads while also blocking these
kind of race-condition same-uploads, we could make the check more strict
(e.g. also check timestamps, or check if page already exists, or …)
Bug: T158480
Change-Id: I76cbd2c64c3b893997f1f85974d6f82cbfe121e1
2017-09-18 13:35:42 +00:00
|
|
|
)->isOK() ) {
|
2016-12-16 22:45:20 +00:00
|
|
|
$this->output( "done.\n" );
|
2009-10-02 09:54:20 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$doProtect = false;
|
2009-10-02 10:04:41 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$protectLevel = $this->getOption( 'protect' );
|
2011-10-19 20:08:15 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
|
|
|
|
|
$doProtect = true;
|
|
|
|
|
}
|
|
|
|
|
if ( $this->hasOption( 'unprotect' ) ) {
|
|
|
|
|
$protectLevel = '';
|
|
|
|
|
$doProtect = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $doProtect ) {
|
|
|
|
|
# Protect the file
|
|
|
|
|
$this->output( "\nWaiting for replica DBs...\n" );
|
|
|
|
|
// Wait for replica DBs.
|
2019-09-01 14:00:35 +00:00
|
|
|
sleep( 2 ); # Why this sleep?
|
2020-05-01 00:36:46 +00:00
|
|
|
$lbFactory->waitForReplication();
|
2016-12-16 22:45:20 +00:00
|
|
|
|
2021-07-21 16:34:05 +00:00
|
|
|
$this->output( "\nSetting image restrictions ..." );
|
2016-12-16 22:45:20 +00:00
|
|
|
|
|
|
|
|
$cascade = false;
|
|
|
|
|
$restrictions = [];
|
2022-04-13 20:03:34 +00:00
|
|
|
foreach ( $restrictionStore->listApplicableRestrictionTypes( $title ) as $type ) {
|
2016-12-16 22:45:20 +00:00
|
|
|
$restrictions[$type] = $protectLevel;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 20:36:35 +00:00
|
|
|
$page = $services->getWikiPageFactory()->newFromTitle( $title );
|
2016-12-16 22:45:20 +00:00
|
|
|
$status = $page->doUpdateRestrictions( $restrictions, [], $cascade, '', $user );
|
|
|
|
|
$this->output( ( $status->isOK() ? 'done' : 'failed' ) . "\n" );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "failed. (at recordUpload stage)\n" );
|
|
|
|
|
$svar = 'failed';
|
2007-07-28 22:21:23 +00:00
|
|
|
}
|
2009-10-02 10:13:21 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
$$svar++;
|
|
|
|
|
$processed++;
|
2009-10-02 10:13:21 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( $limit && $processed >= $limit ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-04-18 18:48:44 +00:00
|
|
|
}
|
2011-10-19 20:08:15 +00:00
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
# Print out some statistics
|
|
|
|
|
$this->output( "\n" );
|
|
|
|
|
foreach (
|
|
|
|
|
[
|
|
|
|
|
'count' => 'Found',
|
|
|
|
|
'limit' => 'Limit',
|
|
|
|
|
'ignored' => 'Ignored',
|
|
|
|
|
'added' => 'Added',
|
|
|
|
|
'skipped' => 'Skipped',
|
|
|
|
|
'overwritten' => 'Overwritten',
|
|
|
|
|
'failed' => 'Failed'
|
|
|
|
|
] as $var => $desc
|
|
|
|
|
) {
|
|
|
|
|
if ( $$var > 0 ) {
|
|
|
|
|
$this->output( "{$desc}: {$$var}\n" );
|
2009-10-02 10:13:21 +00:00
|
|
|
}
|
2007-06-05 18:39:02 +00:00
|
|
|
}
|
2011-10-19 20:08:15 +00:00
|
|
|
} else {
|
2016-12-16 22:45:20 +00:00
|
|
|
$this->output( "No suitable files could be found for import.\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search a directory for files with one of a set of extensions
|
|
|
|
|
*
|
|
|
|
|
* @param string $dir Path to directory to search
|
|
|
|
|
* @param array $exts Array of extensions to search for
|
|
|
|
|
* @param bool $recurse Search subdirectories recursively
|
|
|
|
|
* @return array|bool Array of filenames on success, or false on failure
|
|
|
|
|
*/
|
|
|
|
|
private function findFiles( $dir, $exts, $recurse = false ) {
|
|
|
|
|
if ( is_dir( $dir ) ) {
|
|
|
|
|
$dhl = opendir( $dir );
|
|
|
|
|
if ( $dhl ) {
|
|
|
|
|
$files = [];
|
|
|
|
|
while ( ( $file = readdir( $dhl ) ) !== false ) {
|
|
|
|
|
if ( is_file( $dir . '/' . $file ) ) {
|
2019-02-28 10:47:07 +00:00
|
|
|
$ext = pathinfo( $file, PATHINFO_EXTENSION );
|
2016-12-16 22:45:20 +00:00
|
|
|
if ( array_search( strtolower( $ext ), $exts ) !== false ) {
|
|
|
|
|
$files[] = $dir . '/' . $file;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $recurse && is_dir( $dir . '/' . $file ) && $file !== '..' && $file !== '.' ) {
|
|
|
|
|
$files = array_merge( $files, $this->findFiles( $dir . '/' . $file, $exts, true ) );
|
2010-09-12 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 22:45:20 +00:00
|
|
|
return $files;
|
2013-04-22 09:36:51 +00:00
|
|
|
} else {
|
2016-12-16 22:45:20 +00:00
|
|
|
return [];
|
2013-04-22 09:36:51 +00:00
|
|
|
}
|
2016-12-16 22:45:20 +00:00
|
|
|
} else {
|
|
|
|
|
return [];
|
2012-09-26 16:02:47 +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
|
|
|
/**
|
|
|
|
|
* Find an auxilliary file with the given extension, matching
|
|
|
|
|
* 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)
|
|
|
|
|
* @return string|bool
|
|
|
|
|
*/
|
|
|
|
|
private function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
|
|
|
|
|
if ( strpos( $auxExtension, '.' ) !== 0 ) {
|
|
|
|
|
$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
|
|
|
|
|
*
|
|
|
|
|
* @return string|bool
|
|
|
|
|
*/
|
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';
|
2021-08-04 12:35:34 +00:00
|
|
|
$body = MediaWikiServices::getInstance()->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';
|
2021-08-04 12:35:34 +00:00
|
|
|
$body = MediaWikiServices::getInstance()->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
|
|
|
|
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;
|