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 * "Smart import" additions:
8 * - aim: preserve the essential metadata (user, description) when importing medias from an existing wiki
10 * - interface with the source wiki, don't use bare files only (see --source-wiki-url).
11 * - fetch metadata from source wiki for each file to import.
12 * - commit the fetched metadata to the destination wiki while submitting.
15 * @ingroup Maintenance
16 * @author Rob Church <robchur@gmail.com>
17 * @author Mij <mij@bitchx.it>
20 $optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license', 'sleep', 'limit', 'from', 'source-wiki-url' );
21 require_once( dirname(__FILE__
) . '/commandLine.inc' );
22 require_once( dirname(__FILE__
) . '/importImages.inc.php' );
23 $processed = $added = $ignored = $skipped = $overwritten = $failed = 0;
25 echo( "Import Images\n\n" );
28 if( count( $args ) > 0 ) {
33 if (isset($options['protect']) && isset($options['unprotect']))
34 die("Cannot specify both protect and unprotect. Only 1 is allowed.\n");
36 if (isset($options['protect']) && $options['protect'] == 1)
37 die("You must specify a protection option.\n");
39 # Prepare the list of allowed extensions
40 global $wgFileExtensions;
41 $extensions = isset( $options['extensions'] )
42 ?
explode( ',', strtolower( $options['extensions'] ) )
45 # Search the path provided for candidates for import
46 $files = findFiles( $dir, $extensions );
48 # Initialise the user for this operation
49 $user = isset( $options['user'] )
50 ? User
::newFromName( $options['user'] )
51 : User
::newFromName( 'Maintenance script' );
52 if( !$user instanceof User
)
53 $user = User
::newFromName( 'Maintenance script' );
56 # Get block check. If a value is given, this specified how often the check is performed
57 if ( isset( $options['check-userblock'] ) ) {
58 if ( !$options['check-userblock'] ) $checkUserBlock = 1;
59 else $checkUserBlock = (int)$options['check-userblock'];
61 $checkUserBlock = false;
65 $from = @$options['from'];
68 $sleep = @$options['sleep'];
69 if ( $sleep ) $sleep = (int)$sleep;
72 $limit = @$options['limit'];
73 if ( $limit ) $limit = (int)$limit;
75 # Get the upload comment
78 if ( isset( $options['comment-file'] ) ) {
79 $comment = file_get_contents( $options['comment-file'] );
80 if ( $comment === false ||
$comment === NULL ) {
81 die( "failed to read comment file: {$options['comment-file']}\n" );
84 else if ( isset( $options['comment'] ) ) {
85 $comment = $options['comment'];
88 $commentExt = isset( $options['comment-ext'] ) ?
$options['comment-ext'] : false;
90 # Get the license specifier
91 $license = isset( $options['license'] ) ?
$options['license'] : '';
93 # Batch "upload" operation
94 if( ( $count = count( $files ) ) > 0 ) {
96 foreach( $files as $file ) {
97 $base = wfBaseName( $file );
100 $title = Title
::makeTitleSafe( NS_FILE
, $base );
101 if( !is_object( $title ) ) {
102 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
107 if ( $from == $title->getDBkey() ) {
115 if ( $checkUserBlock && ( ( $processed %
$checkUserBlock ) == 0 ) ) {
116 $user->clearInstanceCache( 'name' ); //reload from DB!
117 if ( $user->isBlocked() ) {
118 echo( $user->getName() . " was blocked! Aborting.\n" );
124 $image = wfLocalFile( $title );
125 if( $image->exists() ) {
126 if( isset( $options['overwrite'] ) ) {
127 echo( "{$base} exists, overwriting..." );
128 $svar = 'overwritten';
130 echo( "{$base} exists, skipping\n" );
135 if ( isset( $options['skip-dupes'] ) ) {
136 $repo = $image->getRepo();
137 $sha1 = File
::sha1Base36( $file ); #XXX: we end up calculating this again when actually uploading. that sucks.
139 $dupes = $repo->findBySha1( $sha1 );
142 echo( "{$base} already exists as " . $dupes[0]->getName() . ", skipping\n" );
148 echo( "Importing {$base}..." );
152 if (isset( $options['source-wiki-url'])) {
153 /* find comment text directly from source wiki, through MW's API */
154 $real_comment = getFileCommentFromSourceWiki($options['source-wiki-url'], $base);
155 if ($real_comment === false)
156 $commentText = $comment;
158 $commentText = $real_comment;
160 /* find user directly from source wiki, through MW's API */
161 $real_user = getFileUserFromSourceWiki($options['source-wiki-url'], $base);
162 if ($real_user === false) {
165 $wgUser = User
::newFromName($real_user);
166 if ($wgUser === false) {
167 # user does not exist in target wiki
168 echo ("failed: user '$real_user' does not exist in target wiki.");
174 $commentText = false;
177 $f = findAuxFile( $file, $commentExt );
179 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
181 $commentText = file_get_contents( $f );
183 echo( " Failed to load comment file {$f}, using default comment. " );
188 if ( !$commentText ) {
189 $commentText = $comment;
195 if ( isset( $options['dry'] ) ) {
196 echo( " publishing {$file} by '" . $wgUser->getName() . "', comment '$commentText'... " );
198 $archive = $image->publish( $file );
199 if( WikiError
::isError( $archive ) ||
!$archive->isGood() ) {
207 $restrictions = array();
209 global $wgRestrictionLevels;
211 $protectLevel = isset($options['protect']) ?
$options['protect'] : null;
213 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
214 $restrictions['move'] = $protectLevel;
215 $restrictions['edit'] = $protectLevel;
218 if (isset($options['unprotect'])) {
219 $restrictions['move'] = '';
220 $restrictions['edit'] = '';
225 if ( isset( $options['dry'] ) ) {
227 } else if ( $image->recordUpload( $archive->value
, $commentText, $license ) ) {
232 $article = new Article( $title );
233 echo "\nWaiting for slaves...\n";
236 wfWaitForSlaves( 1.0 );
238 echo( "\nSetting image restrictions ... " );
239 if ( $article->updateRestrictions($restrictions) )
253 if ( $limit && $processed >= $limit )
260 # Print out some statistics
262 foreach( array( 'count' => 'Found', 'limit' => 'Limit', 'ignored' => 'Ignored',
263 'added' => 'Added', 'skipped' => 'Skipped', 'overwritten' => 'Overwritten',
264 'failed' => 'Failed' ) as $var => $desc ) {
266 echo( "{$desc}: {$$var}\n" );
270 echo( "No suitable files could be found for import.\n" );
279 function showUsage( $reason = false ) {
281 echo( $reason . "\n" );
285 Imports images and other media files into the wiki
286 USAGE: php importImages.php [options] <dir>
288 <dir> : Path to the directory containing images to be imported
291 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
292 --overwrite Overwrite existing images with the same name (default is to skip them)
293 --limit=<num> Limit the number of images to process. Ignored or skipped images are not counted.
294 --from=<name> Ignore all files until the one with the given name. Useful for resuming
295 aborted imports. <name> should be the file's canonical database form.
296 --skip-dupes Skip images that were already uploaded under a different name (check SHA1)
297 --sleep=<sec> Sleep between files. Useful mostly for debugging.
298 --user=<username> Set username of uploader, default 'Maintenance script'
299 --check-userblock Check if the user got blocked during import.
300 --comment=<text> Set upload summary comment, default 'Importing image file'.
301 --comment-file=<file> Set upload summary comment the the content of <file>.
302 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
303 but the extension <ext>. If a global comment is also given, it is appended.
304 --license=<code> Use an optional license template
305 --dry Dry run, don't import anything
306 --protect=<protect> Specify the protect value (autoconfirmed,sysop)
307 --unprotect Unprotects all uploaded images
308 --source-wiki-url if specified, take User and Comment data for each imported file from this URL.
309 For example, --source-wiki-url="http://en.wikipedia.org/"