MediaWiki  REL1_19
importImages.php
Go to the documentation of this file.
00001 <?php
00002 
00035 $optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license', 'sleep', 'limit', 'from', 'source-wiki-url' );
00036 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
00037 require_once( dirname( __FILE__ ) . '/importImages.inc' );
00038 $processed = $added = $ignored = $skipped = $overwritten = $failed = 0;
00039 
00040 echo( "Import Images\n\n" );
00041 
00042 # Need a path
00043 if ( count( $args ) == 0 ) {
00044         showUsage();
00045 }
00046 
00047 $dir = $args[0];
00048 
00049 # Check Protection
00050 if ( isset( $options['protect'] ) && isset( $options['unprotect'] ) ) {
00051         die( "Cannot specify both protect and unprotect.  Only 1 is allowed.\n" );
00052 }
00053 
00054 if ( isset( $options['protect'] ) && $options['protect'] == 1 ) {
00055         die( "You must specify a protection option.\n" );
00056 }
00057 
00058 # Prepare the list of allowed extensions
00059 global $wgFileExtensions;
00060 $extensions = isset( $options['extensions'] )
00061         ? explode( ',', strtolower( $options['extensions'] ) )
00062         : $wgFileExtensions;
00063 
00064 # Search the path provided for candidates for import
00065 $files = findFiles( $dir, $extensions );
00066 
00067 # Initialise the user for this operation
00068 $user = isset( $options['user'] )
00069         ? User::newFromName( $options['user'] )
00070         : User::newFromName( 'Maintenance script' );
00071 if ( !$user instanceof User ) {
00072         $user = User::newFromName( 'Maintenance script' );
00073 }
00074 $wgUser = $user;
00075 
00076 # Get block check. If a value is given, this specified how often the check is performed
00077 if ( isset( $options['check-userblock'] ) ) {
00078         if ( !$options['check-userblock'] ) {
00079                 $checkUserBlock = 1;
00080         } else {
00081                 $checkUserBlock = (int)$options['check-userblock'];
00082         }
00083 } else {
00084         $checkUserBlock = false;
00085 }
00086 
00087 # Get --from
00088 $from = @$options['from'];
00089 
00090 # Get sleep time.
00091 $sleep = @$options['sleep'];
00092 if ( $sleep ) {
00093         $sleep = (int)$sleep;
00094 }
00095 
00096 # Get limit number
00097 $limit = @$options['limit'];
00098 if ( $limit ) {
00099         $limit = (int)$limit;
00100 }
00101 
00102 # Get the upload comment. Provide a default one in case there's no comment given.
00103 $comment = 'Importing image file';
00104 
00105 if ( isset( $options['comment-file'] ) ) {
00106         $comment =  file_get_contents( $options['comment-file'] );
00107         if ( $comment === false || $comment === null ) {
00108                 die( "failed to read comment file: {$options['comment-file']}\n" );
00109         }
00110 } elseif ( isset( $options['comment'] ) ) {
00111         $comment =  $options['comment'];
00112 }
00113 
00114 $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
00115 
00116 # Get the license specifier
00117 $license = isset( $options['license'] ) ? $options['license'] : '';
00118 
00119 # Batch "upload" operation
00120 $count = count( $files );
00121 if ( $count > 0 ) {
00122 
00123         foreach ( $files as $file ) {
00124                 $base = wfBaseName( $file );
00125 
00126                 # Validate a title
00127                 $title = Title::makeTitleSafe( NS_FILE, $base );
00128                 if ( !is_object( $title ) ) {
00129                         echo( "{$base} could not be imported; a valid title cannot be produced\n" );
00130                         continue;
00131                 }
00132 
00133                 if ( $from ) {
00134                         if ( $from == $title->getDBkey() ) {
00135                                 $from = null;
00136                         } else {
00137                                 $ignored++;
00138                                 continue;
00139                         }
00140                 }
00141 
00142                 if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
00143                         $user->clearInstanceCache( 'name' ); // reload from DB!
00144                         if ( $user->isBlocked() ) {
00145                                 echo( $user->getName() . " was blocked! Aborting.\n" );
00146                                 break;
00147                         }
00148                 }
00149 
00150                 # Check existence
00151                 $image = wfLocalFile( $title );
00152                 if ( $image->exists() ) {
00153                         if ( isset( $options['overwrite'] ) ) {
00154                                 echo( "{$base} exists, overwriting..." );
00155                                 $svar = 'overwritten';
00156                         } else {
00157                                 echo( "{$base} exists, skipping\n" );
00158                                 $skipped++;
00159                                 continue;
00160                         }
00161                 } else {
00162                         if ( isset( $options['skip-dupes'] ) ) {
00163                                 $repo = $image->getRepo();
00164                                 $sha1 = File::sha1Base36( $file ); # XXX: we end up calculating this again when actually uploading. that sucks.
00165 
00166                                 $dupes = $repo->findBySha1( $sha1 );
00167 
00168                                 if ( $dupes ) {
00169                                         echo( "{$base} already exists as " . $dupes[0]->getName() . ", skipping\n" );
00170                                         $skipped++;
00171                                         continue;
00172                                 }
00173                         }
00174 
00175                         echo( "Importing {$base}..." );
00176                         $svar = 'added';
00177                 }
00178 
00179                 if ( isset( $options['source-wiki-url'] ) ) {
00180                         /* find comment text directly from source wiki, through MW's API */
00181                         $real_comment = getFileCommentFromSourceWiki( $options['source-wiki-url'], $base );
00182                         if ( $real_comment === false )
00183                                 $commentText = $comment;
00184                         else
00185                                 $commentText = $real_comment;
00186 
00187                         /* find user directly from source wiki, through MW's API */
00188                         $real_user = getFileUserFromSourceWiki( $options['source-wiki-url'], $base );
00189                         if ( $real_user === false ) {
00190                                 $wgUser = $user;
00191                         } else {
00192                                 $wgUser = User::newFromName( $real_user );
00193                                 if ( $wgUser === false ) {
00194                                         # user does not exist in target wiki
00195                                         echo ( "failed: user '$real_user' does not exist in target wiki." );
00196                                         continue;
00197                                 }
00198                         }
00199                 } else {
00200                         # Find comment text
00201                         $commentText = false;
00202 
00203                         if ( $commentExt ) {
00204                                 $f = findAuxFile( $file, $commentExt );
00205                                 if ( !$f ) {
00206                                         echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
00207                                 } else {
00208                                         $commentText = file_get_contents( $f );
00209                                         if ( !$commentText ) {
00210                                                 echo( " Failed to load comment file {$f}, using default comment. " );
00211                                         }
00212                                 }
00213                         }
00214 
00215                         if ( !$commentText ) {
00216                                 $commentText = $comment;
00217                         }
00218                 }
00219 
00220                 # Import the file
00221                 if ( isset( $options['dry'] ) ) {
00222                         echo( " publishing {$file} by '" . $wgUser->getName() . "', comment '$commentText'... " );
00223                 } else {
00224                         $archive = $image->publish( $file );
00225                         if ( !$archive->isGood() ) {
00226                                 echo( "failed. (" .
00227                                         $archive->getWikiText() .
00228                                         ")\n" );
00229                                 $failed++;
00230                                 continue;
00231                         }
00232                 }
00233 
00234                 if ( isset( $options['dry'] ) ) {
00235                         echo( "done.\n" );
00236                 } elseif ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
00237                         # We're done!
00238                         echo( "done.\n" );
00239 
00240                         $doProtect = false;
00241 
00242                         global $wgRestrictionLevels;
00243 
00244                         $protectLevel = isset( $options['protect'] ) ? $options['protect'] : null;
00245 
00246                         if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
00247                                 $doProtect = true;
00248                         }
00249                         if ( isset( $options['unprotect'] ) ) {
00250                                 $protectLevel = '';
00251                                 $doProtect = true;
00252                         }
00253 
00254                         if ( $doProtect ) {
00255                                         # Protect the file
00256                                         echo "\nWaiting for slaves...\n";
00257                                         // Wait for slaves.
00258                                         sleep( 2.0 ); # Why this sleep?
00259                                         wfWaitForSlaves();
00260 
00261                                         echo( "\nSetting image restrictions ... " );
00262 
00263                                         $cascade = false;
00264                                         $restrictions = array();
00265                                         foreach( $title->getRestrictionTypes() as $type ) {
00266                                                 $restrictions[$type] = $protectLevel;
00267                                         }
00268 
00269                                         $page = WikiPage::factory( $title );
00270                                         $status = $page->doUpdateRestrictions( $restrictions, array(), $cascade, '', $user );
00271                                         echo( ( $status->isOK() ? 'done' : 'failed' ) . "\n" );
00272                         }
00273 
00274                 } else {
00275                         echo( "failed. (at recordUpload stage)\n" );
00276                         $svar = 'failed';
00277                 }
00278 
00279                 $$svar++;
00280                 $processed++;
00281 
00282                 if ( $limit && $processed >= $limit ) {
00283                         break;
00284                 }
00285 
00286                 if ( $sleep ) {
00287                         sleep( $sleep );
00288                 }
00289         }
00290 
00291         # Print out some statistics
00292         echo( "\n" );
00293         foreach ( array( 'count' => 'Found', 'limit' => 'Limit', 'ignored' => 'Ignored',
00294                 'added' => 'Added', 'skipped' => 'Skipped', 'overwritten' => 'Overwritten',
00295                 'failed' => 'Failed' ) as $var => $desc ) {
00296                 if ( $$var > 0 )
00297                         echo( "{$desc}: {$$var}\n" );
00298         }
00299 
00300 } else {
00301         echo( "No suitable files could be found for import.\n" );
00302 }
00303 
00304 exit( 0 );
00305 
00306 function showUsage( $reason = false ) {
00307         if ( $reason ) {
00308                 echo( $reason . "\n" );
00309         }
00310 
00311         echo <<<TEXT
00312 Imports images and other media files into the wiki
00313 USAGE: php importImages.php [options] <dir>
00314 
00315 <dir> : Path to the directory containing images to be imported
00316 
00317 Options:
00318 --extensions=<exts>     Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
00319 --overwrite             Overwrite existing images with the same name (default is to skip them)
00320 --limit=<num>           Limit the number of images to process. Ignored or skipped images are not counted.
00321 --from=<name>           Ignore all files until the one with the given name. Useful for resuming
00322                                                 aborted imports. <name> should be the file's canonical database form.
00323 --skip-dupes            Skip images that were already uploaded under a different name (check SHA1)
00324 --sleep=<sec>           Sleep between files. Useful mostly for debugging.
00325 --user=<username>       Set username of uploader, default 'Maintenance script'
00326 --check-userblock       Check if the user got blocked during import.
00327 --comment=<text>        Set upload summary comment, default 'Importing image file'.
00328 --comment-file=<file>   Set upload summary comment the the content of <file>.
00329 --comment-ext=<ext>     Causes the comment for each file to be loaded from a file with the same name
00330                         but the extension <ext>. If a global comment is also given, it is appended.
00331 --license=<code>        Use an optional license template
00332 --dry                   Dry run, don't import anything
00333 --protect=<protect>     Specify the protect value (autoconfirmed,sysop)
00334 --unprotect             Unprotects all uploaded images
00335 --source-wiki-url   if specified, take User and Comment data for each imported file from this URL.
00336                                         For example, --source-wiki-url="http://en.wikipedia.org/"
00337 
00338 TEXT;
00339         exit( 1 );
00340 }