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 if( isset( $options['user'] ) ) {
30 $wgUser = User
::newFromName( $options['user'] );
32 $wgUser = User
::newFromName( 'Image import script' );
34 if ( $wgUser->isAnon() ) {
35 $wgUser->addToDatabase();
38 # Get the upload comment
39 $comment = isset( $options['comment'] )
41 : 'Importing image file';
43 # Get the license specifier
44 $license = isset( $options['license'] ) ?
$options['license'] : '';
46 # Batch "upload" operation
47 foreach( $files as $file ) {
49 $base = wfBaseName( $file );
52 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
53 if( is_object( $title ) ) {
56 $image = new Image( $title );
57 if( !$image->exists() ) {
59 global $wgUploadDirectory;
61 # copy() doesn't create paths so if the hash path doesn't exist, we
63 makeHashPath( wfGetHashPath( $image->name
) );
66 echo( "Saving {$base}..." );
68 if( copy( $file, $image->getFullPath() ) ) {
70 echo( "importing..." );
73 $image->loadFromFile();
76 if( $image->recordUpload( '', $comment, $license ) ) {
90 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
94 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
106 function showUsage( $reason = false ) {
108 echo( $reason . "\n" );
110 USAGE: php importImages.php [options] <dir> <ext1> ...
112 <dir> : Path to the directory containing images to be imported
113 <ext1+> File extensions to import
116 --user=<username> Set username of uploader, default 'Image import script'
117 --comment=<text> Set upload summary comment, default 'Importing image file'
118 --license=<code> Use an optional license template