35require_once __DIR__ .
'/Maintenance.php';
43 parent::__construct();
45 $this->
addDescription(
'Imports images and other media files into the wiki' );
46 $this->
addArg(
'dir',
'Path to the directory containing images to be imported' );
49 'Comma-separated list of allowable extensions, defaults to $wgFileExtensions',
54 'Overwrite existing images with the same name (default is to skip them)' );
56 'Limit the number of images to process. Ignored or skipped images are not counted',
61 "Ignore all files until the one with the given name. Useful for resuming aborted "
62 .
"imports. The name should be the file's canonical database form.",
67 'Skip images that were already uploaded under a different name (check SHA1)' );
68 $this->
addOption(
'search-recursively',
'Search recursively for files in subdirectories' );
70 'Sleep between files. Useful mostly for debugging',
75 "Set username of uploader, default 'Maintenance script'",
81 $this->
addOption(
'check-userblock',
'Check if the user got blocked during import' );
83 "Set file description, default 'Importing file'",
88 'Set description to the content of this file',
93 'Causes the description for each file to be loaded from a file with the same name, but '
94 .
'the extension provided. If a global description is also given, it is appended.',
99 'Upload summary, description will be used if not provided',
104 'Use an optional license template',
109 'Override upload time/date, all MediaWiki timestamp formats are accepted',
114 'Specify the protect value (autoconfirmed,sysop)',
118 $this->
addOption(
'unprotect',
'Unprotects all uploaded images' );
120 'If specified, take User and Comment data for each imported file from this URL. '
121 .
'For example, --source-wiki-url="http://en.wikipedia.org/',
125 $this->
addOption(
'dry',
"Dry run, don't import anything" );
129 $services = MediaWikiServices::getInstance();
130 $permissionManager = $services->getPermissionManager();
132 $processed = $added = $ignored = $skipped = $overwritten = $failed = 0;
134 $this->
output(
"Importing Files\n\n" );
136 $dir = $this->
getArg( 0 );
140 $this->
fatalError(
"Cannot specify both protect and unprotect. Only 1 is allowed.\n" );
144 $this->
fatalError(
"You must specify a protection option.\n" );
147 # Prepare the list of allowed extensions
148 $extensions = $this->
hasOption(
'extensions' )
149 ? explode(
',', strtolower( $this->
getOption(
'extensions' ) ) )
150 : $this->
getConfig()->get( MainConfigNames::FileExtensions );
152 # Search the path provided for candidates for import
153 $files = $this->findFiles( $dir, $extensions, $this->
hasOption(
'search-recursively' ) );
155 $this->
output(
"No suitable files could be found for import.\n" );
159 # Initialise the user for this operation
163 if ( !$user instanceof
User ) {
166 '@phan-var User $user';
169 # Get block check. If a value is given, this specified how often the check is performed
170 $checkUserBlock = (int)$this->
getOption(
'check-userblock' );
173 $sleep = (int)$this->
getOption(
'sleep' );
174 $limit = (int)$this->
getOption(
'limit' );
175 $timestamp = $this->
getOption(
'timestamp',
false );
177 # Get the upload comment. Provide a default one in case there's no comment given.
178 $commentFile = $this->
getOption(
'comment-file' );
179 if ( $commentFile !==
null ) {
180 $comment = file_get_contents( $commentFile );
181 if ( $comment ===
false || $comment ===
null ) {
182 $this->
fatalError(
"failed to read comment file: {$commentFile}\n" );
185 $comment = $this->
getOption(
'comment',
'Importing file' );
187 $commentExt = $this->
getOption(
'comment-ext' );
188 $summary = $this->
getOption(
'summary',
'' );
190 $license = $this->
getOption(
'license',
'' );
192 $sourceWikiUrl = $this->
getOption(
'source-wiki-url' );
198 # Batch "upload" operation
199 $lbFactory = $services->getDBLoadBalancerFactory();
200 $restrictionStore = $services->getRestrictionStore();
201 foreach ( $files as
$file ) {
202 if ( $sleep && ( $processed > 0 ) ) {
210 if ( !is_object(
$title ) ) {
212 "{$base} could not be imported; a valid title cannot be produced\n"
218 if ( $from ==
$title->getDBkey() ) {
226 if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
227 $user->clearInstanceCache(
'name' );
228 if ( $permissionManager->isBlockedFrom( $user,
$title ) ) {
230 "{$user->getName()} is blocked from {$title->getPrefixedText()}! skipping.\n"
238 $image = $services->getRepoGroup()->getLocalRepo()
240 if ( $image->exists() ) {
242 $this->
output(
"{$base} exists, overwriting..." );
243 $svar =
'overwritten';
245 $this->
output(
"{$base} exists, skipping\n" );
250 if ( $this->
hasOption(
'skip-dupes' ) ) {
251 $repo = $image->getRepo();
252 # XXX: we end up calculating this again when actually uploading. that sucks.
255 $dupes = $repo->findBySha1( $sha1 );
259 "{$base} already exists as {$dupes[0]->getName()}, skipping\n"
266 $this->
output(
"Importing {$base}..." );
270 if ( $sourceWikiUrl ) {
272 $real_comment = $this->getFileCommentFromSourceWiki( $sourceWikiUrl,
$base );
273 if ( $real_comment ===
false ) {
274 $commentText = $comment;
276 $commentText = $real_comment;
280 $real_user = $this->getFileUserFromSourceWiki( $sourceWikiUrl,
$base );
281 if ( $real_user ===
false ) {
285 if ( $realUser ===
false ) {
286 # user does not exist in target wiki
288 "failed: user '$real_user' does not exist in target wiki."
297 $commentText =
false;
300 $f = $this->findAuxFile(
$file, $commentExt );
302 $this->
output(
" No comment file with extension {$commentExt} found "
303 .
"for {$file}, using default comment." );
305 $commentText = file_get_contents( $f );
306 if ( !$commentText ) {
308 " Failed to load comment file {$f}, using default comment."
314 if ( !$commentText ) {
315 $commentText = $comment;
322 " publishing {$file} by '{$user->getName()}', comment '$commentText'..."
325 $mwProps =
new MWFileProps( $services->getMimeAnalyzer() );
326 $props = $mwProps->getPropsFromPath(
$file,
true );
328 $publishOptions = [];
331 $publishOptions[
'headers'] = $handler->getContentHeaders( $props[
'metadata'] );
333 $publishOptions[
'headers'] = [];
335 $archive = $image->publish(
$file, $flags, $publishOptions );
336 if ( !$archive->isGood() ) {
337 $this->
output(
"failed. (" .
338 $archive->getMessage(
false,
false,
'en' )->text() .
345 $commentText = SpecialUpload::getInitialPageText( $commentText, $license );
347 $summary = $commentText;
351 $this->
output(
"done.\n" );
352 } elseif ( $image->recordUpload3(
363 $this->
output(
"done.\n" );
367 $protectLevel = $this->
getOption(
'protect' );
368 $restrictionLevels = $this->
getConfig()->get( MainConfigNames::RestrictionLevels );
370 if ( $protectLevel && in_array( $protectLevel, $restrictionLevels ) ) {
380 $this->
output(
"\nWaiting for replica DBs...\n" );
382 sleep( 2 ); # Why
this sleep?
383 $lbFactory->waitForReplication();
385 $this->
output(
"\nSetting image restrictions ..." );
389 foreach ( $restrictionStore->listApplicableRestrictionTypes(
$title ) as
$type ) {
390 $restrictions[
$type] = $protectLevel;
393 $page = $services->getWikiPageFactory()->newFromTitle(
$title );
394 $status = $page->doUpdateRestrictions( $restrictions, [], $cascade,
'', $user );
395 $this->
output( ( $status->isOK() ?
'done' :
'failed' ) .
"\n" );
398 $this->
output(
"failed. (at recordUpload stage)\n" );
405 if ( $limit && $processed >= $limit ) {
410 # Print out some statistics
414 'Found' => count( $files ),
416 'Ignored' => $ignored,
418 'Skipped' => $skipped,
419 'Overwritten' => $overwritten,
421 ] as $desc => $number
424 $this->
output(
"{$desc}: {$number}\n" );
437 private function findFiles( $dir, $exts, $recurse =
false ) {
438 if ( !is_dir( $dir ) ) {
442 $dhl = opendir( $dir );
448 while ( (
$file = readdir( $dhl ) ) !==
false ) {
449 if ( is_file( $dir .
'/' .
$file ) ) {
450 $ext = pathinfo(
$file, PATHINFO_EXTENSION );
451 if ( in_array( strtolower(
$ext ), $exts ) ) {
452 $files[] = $dir .
'/' .
$file;
454 } elseif ( $recurse && is_dir( $dir .
'/' .
$file ) &&
$file !==
'..' &&
$file !==
'.' ) {
455 $files = array_merge( $files, $this->findFiles( $dir .
'/' .
$file, $exts,
true ) );
476 private function findAuxFile(
$file, $auxExtension, $maxStrip = 1 ) {
477 if ( strpos( $auxExtension,
'.' ) !== 0 ) {
478 $auxExtension =
'.' . $auxExtension;
481 $d = dirname(
$file );
482 $n = basename(
$file );
484 while ( $maxStrip >= 0 ) {
485 $f = $d .
'/' . $n . $auxExtension;
487 if ( file_exists( $f ) ) {
491 $idx = strrpos( $n,
'.' );
496 $n = substr( $n, 0, $idx );
512 private function getFileCommentFromSourceWiki( $wiki_host,
$file ) {
513 $url = $wiki_host .
'/api.php?action=query&format=xml&titles=File:'
514 . rawurlencode(
$file ) .
'&prop=imageinfo&&iiprop=comment';
515 $body = MediaWikiServices::getInstance()->getHttpRequestFactory()->get( $url, [], __METHOD__ );
516 if ( preg_match(
'#<ii comment="([^"]*)" />#', $body,
$matches ) == 0 ) {
520 return html_entity_decode(
$matches[1] );
523 private function getFileUserFromSourceWiki( $wiki_host,
$file ) {
524 $url = $wiki_host .
'/api.php?action=query&format=xml&titles=File:'
525 . rawurlencode(
$file ) .
'&prop=imageinfo&&iiprop=user';
526 $body = MediaWikiServices::getInstance()->getHttpRequestFactory()->get( $url, [], __METHOD__ );
527 if ( preg_match(
'#<ii user="([^"]*)" />#', $body,
$matches ) == 0 ) {
531 return html_entity_decode(
$matches[1] );
537require_once RUN_MAINTENANCE_IF_MAIN;
wfBaseName( $path, $suffix='')
Return the final portion of a pathname.
static getSha1Base36FromPath( $path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
execute()
Do the actual work.
__construct()
Default constructor.
MimeMagic helper wrapper.
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.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
static newFromName( $name, $validate='valid')
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
if(!is_readable( $file)) $ext