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 # Initialise the user for this operation
28 $user = isset( $options['user'] )
29 ? User
::newFromName( $options['user'] )
30 : User
::newFromName( 'Maintenance script' );
31 if( !$user instanceof User
)
32 $user = User
::newFromName( 'Maintenance script' );
35 # Get the upload comment
36 $comment = isset( $options['comment'] )
38 : 'Importing image file';
40 # Get the license specifier
41 $license = isset( $options['license'] ) ?
$options['license'] : '';
43 # Batch "upload" operation
44 foreach( $files as $file ) {
46 $base = wfBaseName( $file );
49 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
50 if( is_object( $title ) ) {
53 $image = new Image( $title );
54 if( !$image->exists() ) {
56 global $wgUploadDirectory;
58 # copy() doesn't create paths so if the hash path doesn't exist, we
60 makeHashPath( wfGetHashPath( $image->name
) );
63 echo( "Saving {$base}..." );
65 if( copy( $file, $image->getFullPath() ) ) {
67 echo( "importing..." );
70 $image->loadFromFile();
73 if( $image->recordUpload( '', $comment, $license ) ) {
87 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
91 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
103 function showUsage( $reason = false ) {
105 echo( $reason . "\n" );
107 USAGE: php importImages.php [options] <dir> <ext1> ...
109 <dir> : Path to the directory containing images to be imported
110 <ext1+> File extensions to import
113 --user=<username> Set username of uploader, default 'Image import script'
114 --comment=<text> Set upload summary comment, default 'Importing image file'
115 --license=<code> Use an optional license template