4 * Maintenance script to import one or more images from the local file system into
5 * the wiki without using the web-based interface
8 * @subpackage Maintenance
9 * @author Rob Church <robchur@gmail.com>
12 require_once( 'commandLine.inc' );
13 require_once( 'importImages.inc.php' );
14 echo( "Import Images\n\n" );
16 # Need a directory and at least one extension
17 if( count( $args ) > 1 ) {
19 $dir = array_shift( $args );
21 # Check the allowed extensions
22 while( $ext = array_shift( $args ) )
23 $exts[] = ltrim( $ext, '.' );
25 # Search the directory given and pull out suitable candidates
26 $files = findFiles( $dir, $exts );
28 # Set up a fake user for this operation
29 $wgUser = User
::newFromName( 'Image import script' );
30 $wgUser->setLoaded( true );
32 # Batch "upload" operation
33 foreach( $files as $file ) {
35 $base = basename( $file );
38 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
39 if( is_object( $title ) ) {
42 $image = new Image( $title );
43 if( !$image->exists() ) {
45 global $wgUploadDirectory;
47 # copy() doesn't create paths so if the hash path doesn't exist, we
49 makeHashPath( wfGetHashPath( $image->name
) );
52 echo( "Saving {$base}..." );
54 if( copy( $file, $image->getFullPath() ) ) {
56 echo( "importing..." );
59 $image->loadFromFile();
62 if( $image->recordUpload( '', 'Importing image file' ) ) {
76 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
80 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
92 function showUsage( $reason = false ) {
94 echo( $reason . "\n" );
95 echo( "USAGE: php importImages.php <dir> <ext1> <ext2>\n\n" );
96 echo( "<dir> : Path to the directory containing images to be imported\n" );
97 echo( "<ext1+> File extensions to import\n\n" );