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 $optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license' );
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 = 'Importing image file';
44 if ( isset( $options['comment-file'] ) ) {
45 $comment = file_get_contents( $options['comment-file'] );
46 if ( $comment === false ||
$comment === NULL ) {
47 die( "failed to read comment file: {$options['comment-file']}\n" );
50 else if ( isset( $options['comment'] ) ) {
51 $comment = $options['comment'];
54 $commentExt = isset( $options['comment-ext'] ) ?
$options['comment-ext'] : false;
56 # Get the license specifier
57 $license = isset( $options['license'] ) ?
$options['license'] : '';
59 # Batch "upload" operation
60 if( ( $count = count( $files ) ) > 0 ) {
62 foreach( $files as $file ) {
63 $base = wfBaseName( $file );
66 $title = Title
::makeTitleSafe( NS_FILE
, $base );
67 if( !is_object( $title ) ) {
68 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
73 $image = wfLocalFile( $title );
74 if( $image->exists() ) {
75 if( isset( $options['overwrite'] ) ) {
76 echo( "{$base} exists, overwriting..." );
77 $svar = 'overwritten';
79 echo( "{$base} exists, skipping\n" );
84 echo( "Importing {$base}..." );
92 $f = findAuxFile( $file, $commentExt );
94 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
96 $commentText = file_get_contents( $f );
98 echo( " Failed to load comment file {$f}, using default comment. " );
103 if ( !$commentText ) {
104 $commentText = $comment;
108 if ( isset( $options['dry'] ) ) {
109 echo( " publishing {$file}... " );
111 $archive = $image->publish( $file );
112 if( WikiError
::isError( $archive ) ||
!$archive->isGood() ) {
119 if ( isset( $options['dry'] ) ) {
121 } else if ( $image->recordUpload( $archive->value
, $commentText, $license ) ) {
130 # Print out some statistics
132 foreach( array( 'count' => 'Found', 'added' => 'Added',
133 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
135 echo( "{$desc}: {$$var}\n" );
139 echo( "No suitable files could be found for import.\n" );
148 function showUsage( $reason = false ) {
150 echo( $reason . "\n" );
154 Imports images and other media files into the wiki
155 USAGE: php importImages.php [options] <dir>
157 <dir> : Path to the directory containing images to be imported
160 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
161 --overwrite Overwrite existing images if a conflicting-named image is found
162 --user=<username> Set username of uploader, default 'Maintenance script'
163 --comment=<text> Set upload summary comment, default 'Importing image file'
164 --comment-file=<file> Set upload summary comment the the content of <file>.
165 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
166 but the extension <ext>.
167 --license=<code> Use an optional license template
168 --dry Dry run, don't import anything