[Mediawiki-l] Help with Bulk Image Upload

Ken McDonald ken at pixologic.com
Wed Jun 21 20:11:31 UTC 2006


Chris Walker wrote:
> Where can i get "Subverion Trunk"?
>   
If you are "subversion challenged" like I am :-), here are the two 
relevant files, (written by Rob of course!)

importImages.php
---------------------------<?php

/**
 * Maintenance script to import one or more images from the local file 
system into
 * the wiki without using the web-based interface
 *
 * @package MediaWiki
 * @subpackage Maintenance
 * @author Rob Church <robchur at gmail.com>
 */

require_once( 'commandLine.inc' );
require_once( 'importImages.inc.php' );
echo( "Import Images\n\n" );

# Need a directory and at least one extension
if( count( $args ) > 1 ) {

        $dir = array_shift( $args );

        # Check the allowed extensions
        while( $ext = array_shift( $args ) )
                $exts[] = ltrim( $ext, '.' );

        # Search the directory given and pull out suitable candidates
        $files = findFiles( $dir, $exts );

        # Set up a fake user for this operation
        $wgUser = User::newFromName( 'Image import script' );
        $wgUser->setLoaded( true );

        # Batch "upload" operation
        foreach( $files as $file ) {

                $base = basename( $file );

                # Validate a title
                $title = Title::makeTitleSafe( NS_IMAGE, $base );
                if( is_object( $title ) ) {

                        # Check existence
                        $image = new Image( $title );
                        if( !$image->exists() ) {

                                global $wgUploadDirectory;

                                # copy() doesn't create paths so if the 
hash path doesn't exist, we
                                # have to create it
                                makeHashPath( wfGetHashPath( 
$image->name ) );

                                # Stash the file
                                echo( "Saving {$base}..." );

                                if( copy( $file, $image->getFullPath() ) ) {

                                        echo( "importing..." );

                                        # Grab the metadata
                                        $image->loadFromFile();

                                        # Record the upload
                                        if( $image->recordUpload( '', 
'Importing image file' ) ) {

                                                # We're done!
                                                echo( "done.\n" );

                                        } else {
                                                echo( "failed.\n" );
                                        }

                                } else {
                                        echo( "failed.\n" );
                                }

                        } else {
                                echo( "{$base} could not be imported; a 
file with this name exists in the wiki\n" );
                        }

                } else {
                        echo( "{$base} could not be imported; a valid 
title cannot be produced\n" );
                }

        }


} else {
        showUsage();
}

exit();

function showUsage( $reason = false ) {
        if( $reason )
                echo( $reason . "\n" );
        echo( "USAGE: php importImages.php <dir> <ext1> <ext2>\n\n" );
        echo( "<dir> : Path to the directory containing images to be 
imported\n" );
        echo( "<ext1+> File extensions to import\n\n" );
        exit();
}

?>
------------------

and, "importImages.inc.php":
------------------
<?php

/**
 * Support functions for the importImages script
 *
 * @package MediaWiki
 * @subpackage Maintenance
 * @author Rob Church <robchur at gmail.com>
 */

/**
 * Search a directory for files with one of a set of extensions
 *
 * @param $dir Path to directory to search
 * @param $exts Array of extensions to search for
 * @return mixed Array of filenames on success, or false on failure
 */
function findFiles( $dir, $exts ) {
        if( is_dir( $dir ) ) {
                if( $dhl = opendir( $dir ) ) {
                        while( ( $file = readdir( $dhl ) ) !== false ) {
                                if( is_file( $dir . '/' . $file ) ) {
                                        list( $name, $ext ) = 
splitFilename( $dir . '/' . $file );
                                        if( array_search( strtolower( 
$ext ), $exts ) !== false )
                                                $files[] = $dir . '/' . 
$file;
                                }
                        }
                        return $files;
                } else {
                        return false;
                }
        } else {
                return false;
        }
}

/**
 * Split a filename into filename and extension
 *
 * @param $filename Filename
 * @return array
 */
function splitFilename( $filename ) {
        $parts = explode( '.', $filename );
        $ext = $parts[ count( $parts ) - 1 ];
        unset( $parts[ count( $parts ) - 1 ] );
        $fname = implode( '.', $parts );
        return array( $fname, $ext );
}

/**
 * Given an image hash, check that the structure exists to save the 
image file
 * and create it if it doesn't
 *
 * @param $hash Part of an image hash, e.g. /f/fd/
 */
function makeHashPath( $hash ) {
        global $wgUploadDirectory;
        $parts = explode( '/', substr( $hash, 1, strlen( $hash ) - 2 ) );
        if( !is_dir( $wgUploadDirectory . '/' . $parts[0] ) )
                mkdir( $wgUploadDirectory . '/' . $parts[0] );
        if( !is_dir( $wgUploadDirectory . '/' . $hash ) )
                mkdir( $wgUploadDirectory . '/' . $hash );
}


?>
---------------

I put these into my .../maintenance directory. To run, ensure you are in 
the maintenance directory, and then do:

php importImages.php /path/to/myimagefolder extension

where "extension" is one of "jpg", "png", or "gif" (no periods). This 
will import all the images of the given type (suffix) from the given 
directory into the MW site.

NOTE: Since this isn't from the Subversion tree, it may be slightly out 
of date. Also, I may have introduced some minor errors at the 
beginning/end of the files from the copy/paste operations; if so, the 
errors should, I think, be easy to find.

Cheers,
Ken
> Rob Church wrote:
>   
>> On 21/06/06, Chris Walker <me at cr1sp.com> wrote:
>>   
>>     
>>> Is there a way to add multiple images to mediawiki, via FTP ?
>>>     
>>>       
>> Upload the images into a directory somewhere and then run the image
>> import scripts available in Subversion trunk to insert them into the
>> wiki.
>>
>>
>> Rob Church
>> _______________________________________________
>> MediaWiki-l mailing list
>> MediaWiki-l at Wikimedia.org
>> http://mail.wikipedia.org/mailman/listinfo/mediawiki-l
>>
>>
>>
>>
>>   
>>     
>
> _______________________________________________
> MediaWiki-l mailing list
> MediaWiki-l at Wikimedia.org
> http://mail.wikipedia.org/mailman/listinfo/mediawiki-l
>
>
>   




More information about the MediaWiki-l mailing list