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', 'overwrite' );
12 require_once( 'commandLine.inc' );
13 require_once( 'importImages.inc.php' );
14 $added = $skipped = $overwritten = 0;
16 echo( "Import Images\n\n" );
19 if( count( $args ) > 0 ) {
23 # Prepare the list of allowed extensions
24 global $wgFileExtensions;
25 $extensions = isset( $options['extensions'] )
26 ?
explode( ',', strtolower( $options['extensions'] ) )
29 # Search the path provided for candidates for import
30 $files = findFiles( $dir, $extensions );
32 # Initialise the user for this operation
33 $user = isset( $options['user'] )
34 ? User
::newFromName( $options['user'] )
35 : User
::newFromName( 'Maintenance script' );
36 if( !$user instanceof User
)
37 $user = User
::newFromName( 'Maintenance script' );
40 # Get the upload comment
41 $comment = isset( $options['comment'] )
43 : 'Importing image file';
45 # Get the license specifier
46 $license = isset( $options['license'] ) ?
$options['license'] : '';
48 # Batch "upload" operation
49 if( ( $count = count( $files ) ) > 0 ) {
51 foreach( $files as $file ) {
52 $base = wfBaseName( $file );
55 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
56 if( !is_object( $title ) ) {
57 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
62 $image = wfLocalFile( $title );
63 if( $image->exists() ) {
64 if( isset( $options['overwrite'] ) ) {
65 echo( "{$base} exists, overwriting..." );
66 $svar = 'overwritten';
68 echo( "{$base} exists, skipping\n" );
73 echo( "Importing {$base}..." );
78 $archive = $image->publish( $file );
79 if( WikiError
::isError( $archive ) ||
!$archive->isGood() ) {
85 if ( $image->recordUpload( $archive->value
, $comment, $license ) ) {
94 # Print out some statistics
96 foreach( array( 'count' => 'Found', 'added' => 'Added',
97 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
99 echo( "{$desc}: {$$var}\n" );
103 echo( "No suitable files could be found for import.\n" );
112 function showUsage( $reason = false ) {
114 echo( $reason . "\n" );
118 Imports images and other media files into the wiki
119 USAGE: php importImages.php [options] <dir>
121 <dir> : Path to the directory containing images to be imported
124 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
125 --overwrite Overwrite existing images if a conflicting-named image is found
126 --user=<username> Set username of uploader, default 'Maintenance script'
127 --comment=<text> Set upload summary comment, default 'Importing image file'
128 --license=<code> Use an optional license template