Let crazy people run updates on broken setups like 5.3.1
[lhc/web/wiklou.git] / maintenance / importImages.php
index c41bfa6..9f9bcae 100644 (file)
@@ -65,7 +65,7 @@ if (isset($options['protect']) && $options['protect'] == 1)
        if ( $limit ) $limit = (int)$limit; 
 
        # Get the upload comment
-       $comment = 'Importing image file';
+       $comment = NULL;
 
        if ( isset( $options['comment-file'] ) ) {
                $comment =  file_get_contents( $options['comment-file'] );
@@ -107,7 +107,7 @@ if (isset($options['protect']) && $options['protect'] == 1)
                        if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
                                $user->clearInstanceCache( 'name' ); //reload from DB!
                                if ( $user->isBlocked() ) {
-                                       echo( $user->getName() . " was blocked! Aborting." );
+                                       echo( $user->getName() . " was blocked! Aborting.\n" );
                                        break;
                                }
                        }
@@ -124,6 +124,19 @@ if (isset($options['protect']) && $options['protect'] == 1)
                                        continue;
                                }
                        } else {
+                               if ( isset( $options['skip-dupes'] ) ) {
+                                       $repo = $image->getRepo();
+                                       $sha1 = File::sha1Base36( $file ); #XXX: we end up calculating this again when actually uploading. that sucks.
+
+                                       $dupes = $repo->findBySha1( $sha1 );
+
+                                       if ( $dupes ) {
+                                               echo( "{$base} already exists as " . $dupes[0]->getName() . ", skipping\n" );
+                                               $skipped++;
+                                               continue;
+                                       }
+                               }
+
                                echo( "Importing {$base}..." );
                                $svar = 'added';
                        }
@@ -134,23 +147,27 @@ if (isset($options['protect']) && $options['protect'] == 1)
                        if ( $commentExt ) {
                                $f = findAuxFile( $file, $commentExt );
                                if ( !$f ) {
-                                       echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
+                                       echo( " No comment file with extension {$commentExt} found for {$file}. " );
+                                       $commentText = $comment;
                                } else {
                                        $commentText = file_get_contents( $f );
                                        if ( !$f ) {
-                                               echo( " Failed to load comment file {$f}, using default comment. " );
+                                               echo( " Failed to load comment file {$f}. " );
+                                               $commentText = $comment;
+                                       } else if ( $comment ) {
+                                               $commentText = trim( $commentText ) . "\n\n" . trim( $comment );
                                        }
                                }
-
-                               if ( $commentText && $comment ) {
-                                       $commentText = trim( $commentText ) . "\n\n" . trim( $comment );
-                               }
                        }
 
                        if ( !$commentText ) {
                                $commentText = $comment;
                        }
 
+                       if ( !$commentText ) {
+                               $commentText = 'Importing image file';
+                       }
+
                        # Import the file       
                        if ( isset( $options['dry'] ) ) {
                                echo( " publishing {$file}... " );
@@ -241,7 +258,7 @@ function showUsage( $reason = false ) {
                echo( $reason . "\n" );
        }
 
-       echo <<<END
+       echo <<<TEXT
 Imports images and other media files into the wiki
 USAGE: php importImages.php [options] <dir>
 
@@ -253,6 +270,7 @@ Options:
 --limit=<num>          Limit the number of images to process. Ignored or skipped images are not counted.
 --from=<name>          Ignore all files until the one with the given name. Useful for resuming
                         aborted imports. <name> should be the file's canonical database form.
+--skip-dupes           Skip images that were already uploaded under a different name (check SHA1)
 --sleep=<sec>          Sleep between files. Useful mostly for debugging.
 --user=<username>      Set username of uploader, default 'Maintenance script'
 --check-userblock      Check if the user got blocked during import.
@@ -265,6 +283,6 @@ Options:
 --protect=<protect>     Specify the protect value (autoconfirmed,sysop)
 --unprotect             Unprotects all uploaded images
 
-END;
+TEXT;
        exit(1);
-}
\ No newline at end of file
+}