4 * Maintenance script to import one or more images from the local file system into
5 * the wiki without using the web-based interface
9 * @author Rob Church <robchur@gmail.com>
12 $optionsWithArguments = array( 'extensions', 'overwrite' );
13 require_once( 'commandLine.inc' );
14 require_once( 'importImages.inc.php' );
15 $added = $skipped = $overwritten = 0;
17 echo( "Import Images\n\n" );
20 if( count( $args ) > 0 ) {
24 # Prepare the list of allowed extensions
25 global $wgFileExtensions;
26 $extensions = isset( $options['extensions'] )
27 ?
explode( ',', strtolower( $options['extensions'] ) )
30 # Search the path provided for candidates for import
31 $files = findFiles( $dir, $extensions );
33 # Initialise the user for this operation
34 $user = isset( $options['user'] )
35 ? User
::newFromName( $options['user'] )
36 : User
::newFromName( 'Maintenance script' );
37 if( !$user instanceof User
)
38 $user = User
::newFromName( 'Maintenance script' );
41 # Get the upload comment
42 $comment = isset( $options['comment'] )
44 : 'Importing image file';
46 # Get the license specifier
47 $license = isset( $options['license'] ) ?
$options['license'] : '';
49 # Batch "upload" operation
50 if( ( $count = count( $files ) ) > 0 ) {
52 foreach( $files as $file ) {
53 $base = wfBaseName( $file );
56 $title = Title
::makeTitleSafe( NS_IMAGE
, $base );
57 if( !is_object( $title ) ) {
58 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
63 $image = wfLocalFile( $title );
64 if( $image->exists() ) {
65 if( isset( $options['overwrite'] ) ) {
66 echo( "{$base} exists, overwriting..." );
67 $svar = 'overwritten';
69 echo( "{$base} exists, skipping\n" );
74 echo( "Importing {$base}..." );
79 $archive = $image->publish( $file );
80 if( WikiError
::isError( $archive ) ||
!$archive->isGood() ) {
86 if ( $image->recordUpload( $archive->value
, $comment, $license ) ) {
95 # Print out some statistics
97 foreach( array( 'count' => 'Found', 'added' => 'Added',
98 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
100 echo( "{$desc}: {$$var}\n" );
104 echo( "No suitable files could be found for import.\n" );
113 function showUsage( $reason = false ) {
115 echo( $reason . "\n" );
119 Imports images and other media files into the wiki
120 USAGE: php importImages.php [options] <dir>
122 <dir> : Path to the directory containing images to be imported
125 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
126 --overwrite Overwrite existing images if a conflicting-named image is found
127 --user=<username> Set username of uploader, default 'Maintenance script'
128 --comment=<text> Set upload summary comment, default 'Importing image file'
129 --license=<code> Use an optional license template