Remove unused parameter
[mediawiki.git] / maintenance / importImages.php
blob4c6082b21477684c25efe8f15eafad6437b3429b
1 <?php
3 /**
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 * @file
8 * @ingroup Maintenance
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" );
19 # Need a path
20 if( count( $args ) > 0 ) {
22 $dir = $args[0];
24 # Prepare the list of allowed extensions
25 global $wgFileExtensions;
26 $extensions = isset( $options['extensions'] )
27 ? explode( ',', strtolower( $options['extensions'] ) )
28 : $wgFileExtensions;
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' );
39 $wgUser = $user;
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 );
65 # Validate a title
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" );
69 continue;
72 # Check existence
73 $image = wfLocalFile( $title );
74 if( $image->exists() ) {
75 if( isset( $options['overwrite'] ) ) {
76 echo( "{$base} exists, overwriting..." );
77 $svar = 'overwritten';
78 } else {
79 echo( "{$base} exists, skipping\n" );
80 $skipped++;
81 continue;
83 } else {
84 echo( "Importing {$base}..." );
85 $svar = 'added';
88 # Find comment text
89 $commentText = false;
91 if ( $commentExt ) {
92 $f = findAuxFile( $file, $commentExt );
93 if ( !$f ) {
94 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
95 } else {
96 $commentText = file_get_contents( $f );
97 if ( !$f ) {
98 echo( " Failed to load comment file {$f}, using default comment. " );
103 if ( !$commentText ) {
104 $commentText = $comment;
107 # Import the file
108 if ( isset( $options['dry'] ) ) {
109 echo( " publishing {$file}... " );
110 } else {
111 $archive = $image->publish( $file );
112 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
113 echo( "failed.\n" );
114 continue;
118 $$svar++;
119 if ( isset( $options['dry'] ) ) {
120 echo( "done.\n" );
121 } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
122 # We're done!
123 echo( "done.\n" );
124 } else {
125 echo( "failed.\n" );
130 # Print out some statistics
131 echo( "\n" );
132 foreach( array( 'count' => 'Found', 'added' => 'Added',
133 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
134 if( $$var > 0 )
135 echo( "{$desc}: {$$var}\n" );
138 } else {
139 echo( "No suitable files could be found for import.\n" );
142 } else {
143 showUsage();
146 exit();
148 function showUsage( $reason = false ) {
149 if( $reason ) {
150 echo( $reason . "\n" );
153 echo <<<END
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
159 Options:
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
170 END;
171 exit();