Add an option to importImages to search subdirectories recursively
authorMarius Hoch <hoo@online.de>
Wed, 19 Dec 2012 19:29:15 +0000 (20:29 +0100)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 20 Dec 2012 09:56:10 +0000 (09:56 +0000)
Change-Id: I73a0d42884c1006492cd3de912eb1dc6dab08c5e

maintenance/importImages.inc
maintenance/importImages.php

index ac5d144..2b3d551 100644 (file)
  *
  * @param $dir string Path to directory to search
  * @param $exts Array of extensions to search for
+ * @param $recurse Bool Search subdirectories recursively
  * @return mixed Array of filenames on success, or false on failure
  */
-function findFiles( $dir, $exts ) {
+function findFiles( $dir, $exts, $recurse = false ) {
        if ( is_dir( $dir ) ) {
                $dhl = opendir( $dir );
                if ( $dhl ) {
@@ -38,8 +39,11 @@ function findFiles( $dir, $exts ) {
                        while ( ( $file = readdir( $dhl ) ) !== false ) {
                                if ( is_file( $dir . '/' . $file ) ) {
                                        list( /* $name */, $ext ) = splitFilename( $dir . '/' . $file );
-                                       if ( array_search( strtolower( $ext ), $exts ) !== false )
+                                       if ( array_search( strtolower( $ext ), $exts ) !== false ) {
                                                $files[] = $dir . '/' . $file;
+                                       }
+                               } elseif ( $recurse && is_dir( $dir . '/' . $file ) && $file !== '..' && $file !== '.' ) {
+                                       $files = array_merge( $files, findFiles( $dir . '/' . $file, $exts, true ) );
                                }
                        }
                        return $files;
index d94b948..782f502 100644 (file)
@@ -64,7 +64,7 @@ $extensions = isset( $options['extensions'] )
        : $wgFileExtensions;
 
 # Search the path provided for candidates for import
-$files = findFiles( $dir, $extensions );
+$files = findFiles( $dir, $extensions, isset( $options['search-recursively'] ) );
 
 # Initialise the user for this operation
 $user = isset( $options['user'] )
@@ -332,6 +332,7 @@ Options:
 --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)
+--search-recursively    Search recursively for files in subdirectories
 --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.