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' );
33 $wgUser->setLoaded( true );
36 # Get the upload comment
37 $comment = isset( $options['comment'] )
39 : 'Importing image file';
41 # Get the license specifier
42 $license = isset( $options['license'] ) ?
$options['license'] : '';
44 # Batch "upload" operation
45 foreach( $files as $file ) {
47 $base = wfBaseName( $file );
50 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
51 if( is_object( $title ) ) {
54 $image = new Image( $title );
55 if( !$image->exists() ) {
57 global $wgUploadDirectory;
59 # copy() doesn't create paths so if the hash path doesn't exist, we
61 makeHashPath( wfGetHashPath( $image->name
) );
64 echo( "Saving {$base}..." );
66 if( copy( $file, $image->getFullPath() ) ) {
68 echo( "importing..." );
71 $image->loadFromFile();
74 if( $image->recordUpload( '', $comment, $license ) ) {
88 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
92 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
104 function showUsage( $reason = false ) {
106 echo( $reason . "\n" );
108 USAGE: php importImages.php [options] <dir> <ext1> ...
110 <dir> : Path to the directory containing images to be imported
111 <ext1+> File extensions to import
114 --user=<username> Set username of uploader, default 'Image import script'
115 --comment=<text> Set upload summary comment, default 'Importing image file'
116 --license=<code> Use an optional license template