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, '.' );
25 # Search the directory given and pull out suitable candidates
26 $files = findFiles( $dir, $exts );
28 # Initialise the user for this operation
29 $user = isset( $options['user'] )
30 ? User
::newFromName( $options['user'] )
31 : User
::newFromName( 'Maintenance script' );
32 if( !$user instanceof User
)
33 $user = User
::newFromName( 'Maintenance script' );
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 global $wgUploadDirectory;
46 if( count( $files ) > 0 ) {
47 foreach( $files as $file ) {
48 $base = wfBaseName( $file );
51 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
52 if( !is_object( $title ) ) {
53 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
58 $image = wfLocalFile( $title );
59 if( $image->exists() ) {
60 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
65 echo( "Saving {$base}..." );
67 $archive = $image->publish( $file );
68 if ( WikiError
::isError( $archive ) ) {
72 echo( "importing..." );
74 if ( $image->recordUpload( $archive, $comment, $license ) ) {
89 function showUsage( $reason = false ) {
91 echo( $reason . "\n" );
95 USAGE: php importImages.php [options] <dir> <ext1> ...
97 <dir> : Path to the directory containing images to be imported
98 <ext1+> File extensions to import
101 --user=<username> Set username of uploader, default 'Image import script'
102 --comment=<text> Set upload summary comment, default 'Importing image file'
103 --license=<code> Use an optional license template