From b0b93fc4d2da77f5beab5b40fa5d50d09a781438 Mon Sep 17 00:00:00 2001 From: Rob Church Date: Sat, 28 Jul 2007 22:21:23 +0000 Subject: [PATCH] * Allow overwriting existing files with conflicting names using the --override option * More useful output (no. of files added, overwritten, skipped) * Trim some whitespace and kill a dud global \o/ --- RELEASE-NOTES | 2 ++ maintenance/importImages.inc.php | 4 +-- maintenance/importImages.php | 51 +++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 828da12bc5..210eccba1a 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -386,6 +386,8 @@ Full API documentation is available at http://www.mediawiki.org/wiki/API to $wgFileExtensions) * Add option to maintenance/createAndPromote.php to give the user bureaucrat permissions (--bureaucrat) +* Allow overwriting existing files with a conflicting name using + maintenance/importImages.php == Languages updated since 1.10 == diff --git a/maintenance/importImages.inc.php b/maintenance/importImages.inc.php index f1d53fbf49..9a68bac028 100644 --- a/maintenance/importImages.inc.php +++ b/maintenance/importImages.inc.php @@ -45,6 +45,4 @@ function splitFilename( $filename ) { unset( $parts[ count( $parts ) - 1 ] ); $fname = implode( '.', $parts ); return array( $fname, $ext ); -} - - +} \ No newline at end of file diff --git a/maintenance/importImages.php b/maintenance/importImages.php index 023570b97e..660d831cd6 100644 --- a/maintenance/importImages.php +++ b/maintenance/importImages.php @@ -8,9 +8,11 @@ * @author Rob Church */ -$optionsWithArguments = array( 'extensions' ); +$optionsWithArguments = array( 'extensions', 'overwrite' ); require_once( 'commandLine.inc' ); require_once( 'importImages.inc.php' ); +$added = $skipped = $overwritten = 0; + echo( "Import Images\n\n" ); # Need a path @@ -44,8 +46,7 @@ if( count( $args ) > 0 ) { $license = isset( $options['license'] ) ? $options['license'] : ''; # Batch "upload" operation - global $wgUploadDirectory; - if( count( $files ) > 0 ) { + if( ( $count = count( $files ) ) > 0 ) { foreach( $files as $file ) { $base = wfBaseName( $file ); @@ -60,26 +61,42 @@ if( count( $args ) > 0 ) { # Check existence $image = wfLocalFile( $title ); if( $image->exists() ) { - echo( "{$base} could not be imported; a file with this name exists in the wiki\n" ); - continue; + if( isset( $options['overwrite'] ) ) { + echo( "{$base} exists, overwriting..." ); + $svar = 'overwritten'; + } else { + echo( "{$base} exists, skipping\n" ); + $skipped++; + continue; + } + } else { + echo( "Importing {$base}..." ); + $svar = 'added'; } - - # Stash the file - echo( "Saving {$base}..." ); - + + # Import the file $archive = $image->publish( $file ); - if ( WikiError::isError( $archive ) ) { + if( WikiError::isError( $archive ) || !$archive->isGood() ) { echo( "failed.\n" ); continue; } - echo( "importing..." ); - - if ( $image->recordUpload( $archive, $comment, $license ) ) { + + $$svar++; + if ( $image->recordUpload( $archive->value, $comment, $license ) ) { # We're done! echo( "done.\n" ); } else { echo( "failed.\n" ); } + + } + + # Print out some statistics + echo( "\n" ); + foreach( array( 'count' => 'Found', 'added' => 'Added', + 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) { + if( $$var > 0 ) + echo( "{$desc}: {$$var}\n" ); } } else { @@ -98,18 +115,18 @@ function showUsage( $reason = false ) { } echo << : Path to the directory containing images to be imported Options: ---extensions= Comma-separated list of allowable extensions, defaults to $wgFileExtensions +--extensions= Comma-separated list of allowable extensions, defaults to \$wgFileExtensions +--overwrite Overwrite existing images if a conflicting-named image is found --user= Set username of uploader, default 'Maintenance script' --comment= Set upload summary comment, default 'Importing image file' --license= Use an optional license template END; exit(); -} - - +} \ No newline at end of file -- 2.20.1