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 foreach( $files as $file ) {
47 $base = wfBaseName( $file );
50 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
51 if( !is_object( $title ) ) {
52 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
57 $image = wfLocalFile( $title );
58 if( $image->exists() ) {
59 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
64 echo( "Saving {$base}..." );
66 $archive = $image->publish( $file );
67 if ( WikiError
::isError( $archive ) ) {
71 echo( "importing..." );
73 if ( $image->recordUpload( $archive, $comment, $license ) ) {
87 function showUsage( $reason = false ) {
89 echo( $reason . "\n" );
93 USAGE: php importImages.php [options] <dir> <ext1> ...
95 <dir> : Path to the directory containing images to be imported
96 <ext1+> File extensions to import
99 --user=<username> Set username of uploader, default 'Image import script'
100 --comment=<text> Set upload summary comment, default 'Importing image file'
101 --license=<code> Use an optional license template