Localization update for he, and code style fix in en.
[mediawiki.git] / maintenance / importImages.php
bloba85b8bd9950bbacccbd88af31f56eddf3e1a196e
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', 'sleep', 'limit', 'from' );
13 require_once( dirname(__FILE__) . '/commandLine.inc' );
14 require_once( 'importImages.inc' );
15 $processed = $added = $ignored = $skipped = $overwritten = $failed = 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 (isset($options['protect']) && $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 block check. If a value is given, this specified how often the check is performed
49 if ( isset( $options['check-userblock'] ) ) {
50 if ( !$options['check-userblock'] ) $checkUserBlock = 1;
51 else $checkUserBlock = (int)$options['check-userblock'];
52 } else {
53 $checkUserBlock = false;
56 # Get --from
57 $from = @$options['from'];
59 # Get sleep time.
60 $sleep = @$options['sleep'];
61 if ( $sleep ) $sleep = (int)$sleep;
63 # Get limit number
64 $limit = @$options['limit'];
65 if ( $limit ) $limit = (int)$limit;
67 # Get the upload comment
68 $comment = 'Importing image file';
70 if ( isset( $options['comment-file'] ) ) {
71 $comment = file_get_contents( $options['comment-file'] );
72 if ( $comment === false || $comment === NULL ) {
73 die( "failed to read comment file: {$options['comment-file']}\n" );
76 else if ( isset( $options['comment'] ) ) {
77 $comment = $options['comment'];
80 $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
82 # Get the license specifier
83 $license = isset( $options['license'] ) ? $options['license'] : '';
85 # Batch "upload" operation
86 if( ( $count = count( $files ) ) > 0 ) {
88 foreach( $files as $file ) {
89 $base = wfBaseName( $file );
91 # Validate a title
92 $title = Title::makeTitleSafe( NS_FILE, $base );
93 if( !is_object( $title ) ) {
94 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
95 continue;
98 if ( $from ) {
99 if ( $from == $title->getDBkey() ) {
100 $from = NULL;
101 } else {
102 $ignored++;
103 continue;
107 if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
108 $user->clearInstanceCache( 'name' ); //reload from DB!
109 if ( $user->isBlocked() ) {
110 echo( $user->getName() . " was blocked! Aborting." );
111 break;
115 # Check existence
116 $image = wfLocalFile( $title );
117 if( $image->exists() ) {
118 if( isset( $options['overwrite'] ) ) {
119 echo( "{$base} exists, overwriting..." );
120 $svar = 'overwritten';
121 } else {
122 echo( "{$base} exists, skipping\n" );
123 $skipped++;
124 continue;
126 } else {
127 if ( isset( $options['skip-dupes'] ) ) {
128 $repo = $image->getRepo();
129 $sha1 = File::sha1Base36( $file ); #XXX: we end up calculating this again when actually uploading. that sucks.
131 $dupes = $repo->findBySha1( $sha1 );
133 if ( $dupes ) {
134 echo( "{$base} already exists as " . $dupes[0]->getName() . ", skipping\n" );
135 $skipped++;
136 continue;
140 echo( "Importing {$base}..." );
141 $svar = 'added';
144 # Find comment text
145 $commentText = false;
147 if ( $commentExt ) {
148 $f = findAuxFile( $file, $commentExt );
149 if ( !$f ) {
150 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
151 } else {
152 $commentText = file_get_contents( $f );
153 if ( !$f ) {
154 echo( " Failed to load comment file {$f}, using default comment. " );
158 if ( $commentText && $comment ) {
159 $commentText = trim( $commentText ) . "\n\n" . trim( $comment );
163 if ( !$commentText ) {
164 $commentText = $comment;
167 # Import the file
168 if ( isset( $options['dry'] ) ) {
169 echo( " publishing {$file}... " );
170 } else {
171 $archive = $image->publish( $file );
172 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
173 echo( "failed.\n" );
174 $failed++;
175 continue;
179 $doProtect = false;
180 $restrictions = array();
182 global $wgRestrictionLevels;
184 $protectLevel = isset($options['protect']) ? $options['protect'] : null;
186 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
187 $restrictions['move'] = $protectLevel;
188 $restrictions['edit'] = $protectLevel;
189 $doProtect = true;
191 if (isset($options['unprotect'])) {
192 $restrictions['move'] = '';
193 $restrictions['edit'] = '';
194 $doProtect = true;
198 if ( isset( $options['dry'] ) ) {
199 echo( "done.\n" );
200 } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
201 # We're done!
202 echo( "done.\n" );
203 if ($doProtect) {
204 # Protect the file
205 $article = new Article( $title );
206 echo "\nWaiting for slaves...\n";
207 // Wait for slaves.
208 sleep(2.0);
209 wfWaitForSlaves( 1.0 );
211 echo( "\nSetting image restrictions ... " );
212 if ( $article->updateRestrictions($restrictions) )
213 echo( "done.\n" );
214 else
215 echo( "failed.\n" );
218 } else {
219 echo( "failed.\n" );
220 $svar = 'failed';
223 $$svar++;
224 $processed++;
226 if ( $limit && $processed >= $limit )
227 break;
229 if ( $sleep )
230 sleep( $sleep );
233 # Print out some statistics
234 echo( "\n" );
235 foreach( array( 'count' => 'Found', 'limit' => 'Limit', 'ignored' => 'Ignored',
236 'added' => 'Added', 'skipped' => 'Skipped', 'overwritten' => 'Overwritten',
237 'failed' => 'Failed' ) as $var => $desc ) {
238 if( $$var > 0 )
239 echo( "{$desc}: {$$var}\n" );
242 } else {
243 echo( "No suitable files could be found for import.\n" );
246 } else {
247 showUsage();
250 exit(0);
252 function showUsage( $reason = false ) {
253 if( $reason ) {
254 echo( $reason . "\n" );
257 echo <<<END
258 Imports images and other media files into the wiki
259 USAGE: php importImages.php [options] <dir>
261 <dir> : Path to the directory containing images to be imported
263 Options:
264 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
265 --overwrite Overwrite existing images with the same name (default is to skip them)
266 --limit=<num> Limit the number of images to process. Ignored or skipped images are not counted.
267 --from=<name> Ignore all files until the one with the given name. Useful for resuming
268 aborted imports. <name> should be the file's canonical database form.
269 --skip-dupes Skip images that were already uploaded under a different name (check SHA1)
270 --sleep=<sec> Sleep between files. Useful mostly for debugging.
271 --user=<username> Set username of uploader, default 'Maintenance script'
272 --check-userblock Check if the user got blocked during import.
273 --comment=<text> Set upload summary comment, default 'Importing image file'.
274 --comment-file=<file> Set upload summary comment the the content of <file>.
275 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
276 but the extension <ext>. If a global comment is also given, it is appended.
277 --license=<code> Use an optional license template
278 --dry Dry run, don't import anything
279 --protect=<protect> Specify the protect value (autoconfirmed,sysop)
280 --unprotect Unprotects all uploaded images
282 END;
283 exit(1);