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 $optionsWithArguments = array( 'extensions' );
12 require_once( 'commandLine.inc' );
13 require_once( 'importImages.inc.php' );
14 echo( "Import Images\n\n" );
17 if( count( $args ) > 0 ) {
21 # Prepare the list of allowed extensions
22 global $wgFileExtensions;
23 $extensions = isset( $options['extensions'] )
24 ?
explode( ',', strtolower( $options['extensions'] ) )
27 # Search the path provided for candidates for import
28 $files = findFiles( $dir, $extensions );
30 # Initialise the user for this operation
31 $user = isset( $options['user'] )
32 ? User
::newFromName( $options['user'] )
33 : User
::newFromName( 'Maintenance script' );
34 if( !$user instanceof User
)
35 $user = User
::newFromName( 'Maintenance script' );
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 global $wgUploadDirectory;
48 if( count( $files ) > 0 ) {
50 foreach( $files as $file ) {
51 $base = wfBaseName( $file );
54 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
55 if( !is_object( $title ) ) {
56 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
61 $image = wfLocalFile( $title );
62 if( $image->exists() ) {
63 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
68 echo( "Saving {$base}..." );
70 $archive = $image->publish( $file );
71 if ( WikiError
::isError( $archive ) ) {
75 echo( "importing..." );
77 if ( $image->recordUpload( $archive, $comment, $license ) ) {
86 echo( "No suitable files could be found for import.\n" );
95 function showUsage( $reason = false ) {
97 echo( $reason . "\n" );
101 USAGE: php importImages.php [options] <dir>
103 <dir> : Path to the directory containing images to be imported
106 --extensions=<exts> Comma-separated list of allowable extensions, defaults to $wgFileExtensions
107 --user=<username> Set username of uploader, default 'Maintenance script'
108 --comment=<text> Set upload summary comment, default 'Importing image file'
109 --license=<code> Use an optional license template