Fixed bug caused in r52944 - where the inclusion of csshover.htc was lost in the...
[mediawiki.git] / maintenance / importImages.php
blob5efd1fcbb581e5d23a87782fba39a8ebb78aab6b
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' );
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 # Check Protection
25 if (isset($options['protect']) && isset($options['unprotect']))
26 die("Cannot specify both protect and unprotect. Only 1 is allowed.\n");
28 if ($options['protect'] == 1)
29 die("You must specify a protection option.\n");
31 # Prepare the list of allowed extensions
32 global $wgFileExtensions;
33 $extensions = isset( $options['extensions'] )
34 ? explode( ',', strtolower( $options['extensions'] ) )
35 : $wgFileExtensions;
37 # Search the path provided for candidates for import
38 $files = findFiles( $dir, $extensions );
40 # Initialise the user for this operation
41 $user = isset( $options['user'] )
42 ? User::newFromName( $options['user'] )
43 : User::newFromName( 'Maintenance script' );
44 if( !$user instanceof User )
45 $user = User::newFromName( 'Maintenance script' );
46 $wgUser = $user;
48 # Get the upload comment
49 $comment = 'Importing image file';
51 if ( isset( $options['comment-file'] ) ) {
52 $comment = file_get_contents( $options['comment-file'] );
53 if ( $comment === false || $comment === NULL ) {
54 die( "failed to read comment file: {$options['comment-file']}\n" );
57 else if ( isset( $options['comment'] ) ) {
58 $comment = $options['comment'];
61 $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
63 # Get the license specifier
64 $license = isset( $options['license'] ) ? $options['license'] : '';
66 # Batch "upload" operation
67 if( ( $count = count( $files ) ) > 0 ) {
69 foreach( $files as $file ) {
70 $base = wfBaseName( $file );
72 # Validate a title
73 $title = Title::makeTitleSafe( NS_FILE, $base );
74 if( !is_object( $title ) ) {
75 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
76 continue;
79 # Check existence
80 $image = wfLocalFile( $title );
81 if( $image->exists() ) {
82 if( isset( $options['overwrite'] ) ) {
83 echo( "{$base} exists, overwriting..." );
84 $svar = 'overwritten';
85 } else {
86 echo( "{$base} exists, skipping\n" );
87 $skipped++;
88 continue;
90 } else {
91 echo( "Importing {$base}..." );
92 $svar = 'added';
95 # Find comment text
96 $commentText = false;
98 if ( $commentExt ) {
99 $f = findAuxFile( $file, $commentExt );
100 if ( !$f ) {
101 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
102 } else {
103 $commentText = file_get_contents( $f );
104 if ( !$f ) {
105 echo( " Failed to load comment file {$f}, using default comment. " );
110 if ( !$commentText ) {
111 $commentText = $comment;
114 # Import the file
115 if ( isset( $options['dry'] ) ) {
116 echo( " publishing {$file}... " );
117 } else {
118 $archive = $image->publish( $file );
119 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
120 echo( "failed.\n" );
121 continue;
125 $doProtect = false;
126 $restrictions = array();
128 global $wgRestrictionLevels;
130 $protectLevel = isset($options['protect']) ? $options['protect'] : null;
132 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
133 $restrictions['move'] = $protectLevel;
134 $restrictions['edit'] = $protectLevel;
135 $doProtect = true;
137 if (isset($options['unprotect'])) {
138 $restrictions['move'] = '';
139 $restrictions['edit'] = '';
140 $doProtect = true;
144 $$svar++;
145 if ( isset( $options['dry'] ) ) {
146 echo( "done.\n" );
147 } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
148 # We're done!
149 echo( "done.\n" );
150 if ($doProtect) {
151 # Protect the file
152 $article = new Article( $title );
153 echo "\nWaiting for slaves...\n";
154 // Wait for slaves.
155 sleep(2.0);
156 wfWaitForSlaves( 1.0 );
158 echo( "\nSetting image restrictions ... " );
159 if ( $article->updateRestrictions($restrictions) )
160 echo( "done.\n" );
161 else
162 echo( "failed.\n" );
165 } else {
166 echo( "failed.\n" );
171 # Print out some statistics
172 echo( "\n" );
173 foreach( array( 'count' => 'Found', 'added' => 'Added',
174 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
175 if( $$var > 0 )
176 echo( "{$desc}: {$$var}\n" );
179 } else {
180 echo( "No suitable files could be found for import.\n" );
183 } else {
184 showUsage();
187 exit(0);
189 function showUsage( $reason = false ) {
190 if( $reason ) {
191 echo( $reason . "\n" );
194 echo <<<END
195 Imports images and other media files into the wiki
196 USAGE: php importImages.php [options] <dir>
198 <dir> : Path to the directory containing images to be imported
200 Options:
201 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
202 --overwrite Overwrite existing images if a conflicting-named image is found
203 --user=<username> Set username of uploader, default 'Maintenance script'
204 --comment=<text> Set upload summary comment, default 'Importing image file'
205 --comment-file=<file> Set upload summary comment the the content of <file>.
206 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
207 but the extension <ext>.
208 --license=<code> Use an optional license template
209 --dry Dry run, don't import anything
210 --protect=<protect> Specify the protect value (autoconfirmed,sysop)
211 --unprotect Unprotects all uploaded images
213 END;
214 exit(1);