* Allow overwriting existing files with conflicting names using the --override option
authorRob Church <robchurch@users.mediawiki.org>
Sat, 28 Jul 2007 22:21:23 +0000 (22:21 +0000)
committerRob Church <robchurch@users.mediawiki.org>
Sat, 28 Jul 2007 22:21:23 +0000 (22:21 +0000)
* More useful output (no. of files added, overwritten, skipped)
* Trim some whitespace and kill a dud global \o/

RELEASE-NOTES
maintenance/importImages.inc.php
maintenance/importImages.php

index 828da12..210eccb 100644 (file)
@@ -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 ==
 
index f1d53fb..9a68bac 100644 (file)
@@ -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
index 023570b..660d831 100644 (file)
@@ -8,9 +8,11 @@
  * @author Rob Church <robchur@gmail.com>
  */
 
-$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 <<<END
+Imports images and other media files into the wiki
 USAGE: php importImages.php [options] <dir>
 
 <dir> : Path to the directory containing images to be imported
 
 Options:
---extensions=<exts>    Comma-separated list of allowable extensions, defaults to $wgFileExtensions
+--extensions=<exts>    Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
+--overwrite                    Overwrite existing images if a conflicting-named image is found
 --user=<username>      Set username of uploader, default 'Maintenance script'
 --comment=<text>       Set upload summary comment, default 'Importing image file'
 --license=<code>       Use an optional license template
 
 END;
        exit();
-}
-
-
+}
\ No newline at end of file