4 * Maintenance script to import one or more images from the local file system into
5 * the wiki without using the web-based interface
7 * @addtogroup Maintenance
8 * @author Rob Church <robchur@gmail.com>
11 require_once( 'commandLine.inc' );
12 require_once( 'importImages.inc.php' );
13 echo( "Import Images\n\n" );
15 # Need a directory and at least one extension
16 if( count( $args ) > 1 ) {
18 $dir = array_shift( $args );
20 # Check the allowed extensions
21 while( $ext = array_shift( $args ) )
22 $exts[] = ltrim( $ext, '.' );
24 # Search the directory given and pull out suitable candidates
25 $files = findFiles( $dir, $exts );
27 # Set up a fake user for this operation
28 if( isset( $options['user'] ) ) {
29 $wgUser = User
::newFromName( $options['user'] );
31 $wgUser = User
::newFromName( 'Image import script' );
33 if ( $wgUser->isAnon() ) {
34 $wgUser->addToDatabase();
37 # Get the upload comment
38 $comment = isset( $options['comment'] )
40 : 'Importing image file';
42 # Get the license specifier
43 $license = isset( $options['license'] ) ?
$options['license'] : '';
45 # Batch "upload" operation
46 foreach( $files as $file ) {
48 $base = wfBaseName( $file );
51 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
52 if( is_object( $title ) ) {
55 $image = new Image( $title );
56 if( !$image->exists() ) {
58 global $wgUploadDirectory;
60 # copy() doesn't create paths so if the hash path doesn't exist, we
62 makeHashPath( wfGetHashPath( $image->name
) );
65 echo( "Saving {$base}..." );
67 if( copy( $file, $image->getFullPath() ) ) {
69 echo( "importing..." );
72 $image->loadFromFile();
75 if( $image->recordUpload( '', $comment, $license ) ) {
89 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
93 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
105 function showUsage( $reason = false ) {
107 echo( $reason . "\n" );
109 USAGE: php importImages.php [options] <dir> <ext1> ...
111 <dir> : Path to the directory containing images to be imported
112 <ext1+> File extensions to import
115 --user=<username> Set username of uploader, default 'Image import script'
116 --comment=<text> Set upload summary comment, default 'Importing image file'
117 --license=<code> Use an optional license template