00001 <?php
00002
00012 $optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license' );
00013 require_once( 'commandLine.inc' );
00014 require_once( 'importImages.inc.php' );
00015 $added = $skipped = $overwritten = 0;
00016
00017 echo( "Import Images\n\n" );
00018
00019 # Need a path
00020 if( count( $args ) > 0 ) {
00021
00022 $dir = $args[0];
00023
00024 # Check Protection
00025 if (isset($options['protect']) && isset($options['unprotect']))
00026 die("Cannot specify both protect and unprotect. Only 1 is allowed.\n");
00027
00028 if ($options['protect'] == 1)
00029 die("You must specify a protection option.\n");
00030
00031 # Prepare the list of allowed extensions
00032 global $wgFileExtensions;
00033 $extensions = isset( $options['extensions'] )
00034 ? explode( ',', strtolower( $options['extensions'] ) )
00035 : $wgFileExtensions;
00036
00037 # Search the path provided for candidates for import
00038 $files = findFiles( $dir, $extensions );
00039
00040 # Initialise the user for this operation
00041 $user = isset( $options['user'] )
00042 ? User::newFromName( $options['user'] )
00043 : User::newFromName( 'Maintenance script' );
00044 if( !$user instanceof User )
00045 $user = User::newFromName( 'Maintenance script' );
00046 $wgUser = $user;
00047
00048 # Get the upload comment
00049 $comment = 'Importing image file';
00050
00051 if ( isset( $options['comment-file'] ) ) {
00052 $comment = file_get_contents( $options['comment-file'] );
00053 if ( $comment === false || $comment === NULL ) {
00054 die( "failed to read comment file: {$options['comment-file']}\n" );
00055 }
00056 }
00057 else if ( isset( $options['comment'] ) ) {
00058 $comment = $options['comment'];
00059 }
00060
00061 $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
00062
00063 # Get the license specifier
00064 $license = isset( $options['license'] ) ? $options['license'] : '';
00065
00066 # Batch "upload" operation
00067 if( ( $count = count( $files ) ) > 0 ) {
00068
00069 foreach( $files as $file ) {
00070 $base = wfBaseName( $file );
00071
00072 # Validate a title
00073 $title = Title::makeTitleSafe( NS_FILE, $base );
00074 if( !is_object( $title ) ) {
00075 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
00076 continue;
00077 }
00078
00079 # Check existence
00080 $image = wfLocalFile( $title );
00081 if( $image->exists() ) {
00082 if( isset( $options['overwrite'] ) ) {
00083 echo( "{$base} exists, overwriting..." );
00084 $svar = 'overwritten';
00085 } else {
00086 echo( "{$base} exists, skipping\n" );
00087 $skipped++;
00088 continue;
00089 }
00090 } else {
00091 echo( "Importing {$base}..." );
00092 $svar = 'added';
00093 }
00094
00095 # Find comment text
00096 $commentText = false;
00097
00098 if ( $commentExt ) {
00099 $f = findAuxFile( $file, $commentExt );
00100 if ( !$f ) {
00101 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
00102 } else {
00103 $commentText = file_get_contents( $f );
00104 if ( !$f ) {
00105 echo( " Failed to load comment file {$f}, using default comment. " );
00106 }
00107 }
00108 }
00109
00110 if ( !$commentText ) {
00111 $commentText = $comment;
00112 }
00113
00114 # Import the file
00115 if ( isset( $options['dry'] ) ) {
00116 echo( " publishing {$file}... " );
00117 } else {
00118 $archive = $image->publish( $file );
00119 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
00120 echo( "failed.\n" );
00121 continue;
00122 }
00123 }
00124
00125 $doProtect = false;
00126 $restrictions = array();
00127
00128 global $wgRestrictionLevels;
00129
00130 $protectLevel = isset($options['protect']) ? $options['protect'] : null;
00131
00132 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
00133 $restrictions['move'] = $protectLevel;
00134 $restrictions['edit'] = $protectLevel;
00135 $doProtect = true;
00136 }
00137 if (isset($options['unprotect'])) {
00138 $restrictions['move'] = '';
00139 $restrictions['edit'] = '';
00140 $doProtect = true;
00141 }
00142
00143
00144 $$svar++;
00145 if ( isset( $options['dry'] ) ) {
00146 echo( "done.\n" );
00147 } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
00148 # We're done!
00149 echo( "done.\n" );
00150 if ($doProtect) {
00151 # Protect the file
00152 $article = new Article( $title );
00153 echo "\nWaiting for slaves...\n";
00154
00155 sleep(2.0);
00156 wfWaitForSlaves( 1.0 );
00157
00158 echo( "\nSetting image restrictions ... " );
00159 if ( $article->updateRestrictions($restrictions) )
00160 echo( "done.\n" );
00161 else
00162 echo( "failed.\n" );
00163 }
00164
00165 } else {
00166 echo( "failed.\n" );
00167 }
00168
00169 }
00170
00171 # Print out some statistics
00172 echo( "\n" );
00173 foreach( array( 'count' => 'Found', 'added' => 'Added',
00174 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
00175 if( $$var > 0 )
00176 echo( "{$desc}: {$$var}\n" );
00177 }
00178
00179 } else {
00180 echo( "No suitable files could be found for import.\n" );
00181 }
00182
00183 } else {
00184 showUsage();
00185 }
00186
00187 exit();
00188
00189 function showUsage( $reason = false ) {
00190 if( $reason ) {
00191 echo( $reason . "\n" );
00192 }
00193
00194 echo <<<END
00195 Imports images and other media files into the wiki
00196 USAGE: php importImages.php [options] <dir>
00197
00198 <dir> : Path to the directory containing images to be imported
00199
00200 Options:
00201 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
00202 --overwrite Overwrite existing images if a conflicting-named image is found
00203 --user=<username> Set username of uploader, default 'Maintenance script'
00204 --comment=<text> Set upload summary comment, default 'Importing image file'
00205 --comment-file=<file> Set upload summary comment the the content of <file>.
00206 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
00207 but the extension <ext>.
00208 --license=<code> Use an optional license template
00209 --dry Dry run, don't import anything
00210 --protect=<protect> Specify the protect value (autoconfirmed,sysop)
00211 --unprotect Unprotects all uploaded images
00212
00213 END;
00214 exit();
00215 }