2006-05-15 10:57:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maintenance script to import one or more images from the local file system into
|
|
|
|
|
* the wiki without using the web-based interface
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
2006-05-15 10:57:52 +00:00
|
|
|
* @author Rob Church <robchur@gmail.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2008-11-28 21:32:10 +00:00
|
|
|
$optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license' );
|
2009-08-03 21:56:41 +00:00
|
|
|
require_once( dirname(__FILE__) . '/commandLine.inc' );
|
2009-05-23 19:26:05 +00:00
|
|
|
require_once( 'importImages.inc' );
|
2007-07-28 22:21:23 +00:00
|
|
|
$added = $skipped = $overwritten = 0;
|
|
|
|
|
|
2006-05-15 10:57:52 +00:00
|
|
|
echo( "Import Images\n\n" );
|
|
|
|
|
|
2007-06-07 15:07:12 +00:00
|
|
|
# Need a path
|
|
|
|
|
if( count( $args ) > 0 ) {
|
2006-05-15 10:57:52 +00:00
|
|
|
|
2007-06-07 15:07:12 +00:00
|
|
|
$dir = $args[0];
|
2006-05-15 10:57:52 +00:00
|
|
|
|
2009-03-03 02:56:30 +00:00
|
|
|
# Check Protection
|
|
|
|
|
if (isset($options['protect']) && isset($options['unprotect']))
|
|
|
|
|
die("Cannot specify both protect and unprotect. Only 1 is allowed.\n");
|
|
|
|
|
|
|
|
|
|
if ($options['protect'] == 1)
|
|
|
|
|
die("You must specify a protection option.\n");
|
|
|
|
|
|
2007-06-07 15:07:12 +00:00
|
|
|
# Prepare the list of allowed extensions
|
|
|
|
|
global $wgFileExtensions;
|
|
|
|
|
$extensions = isset( $options['extensions'] )
|
|
|
|
|
? explode( ',', strtolower( $options['extensions'] ) )
|
|
|
|
|
: $wgFileExtensions;
|
2007-05-09 19:09:33 +00:00
|
|
|
|
2007-06-07 15:07:12 +00:00
|
|
|
# Search the path provided for candidates for import
|
|
|
|
|
$files = findFiles( $dir, $extensions );
|
2006-05-15 10:57:52 +00:00
|
|
|
|
2007-03-27 20:53:33 +00:00
|
|
|
# Initialise the user for this operation
|
|
|
|
|
$user = isset( $options['user'] )
|
|
|
|
|
? User::newFromName( $options['user'] )
|
|
|
|
|
: User::newFromName( 'Maintenance script' );
|
|
|
|
|
if( !$user instanceof User )
|
|
|
|
|
$user = User::newFromName( 'Maintenance script' );
|
|
|
|
|
$wgUser = $user;
|
2007-05-09 19:09:33 +00:00
|
|
|
|
2006-09-11 13:12:52 +00:00
|
|
|
# Get the upload comment
|
2008-11-27 13:07:30 +00:00
|
|
|
$comment = 'Importing image file';
|
|
|
|
|
|
|
|
|
|
if ( isset( $options['comment-file'] ) ) {
|
|
|
|
|
$comment = file_get_contents( $options['comment-file'] );
|
|
|
|
|
if ( $comment === false || $comment === NULL ) {
|
|
|
|
|
die( "failed to read comment file: {$options['comment-file']}\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if ( isset( $options['comment'] ) ) {
|
|
|
|
|
$comment = $options['comment'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
|
2007-05-09 19:09:33 +00:00
|
|
|
|
2006-09-11 13:12:52 +00:00
|
|
|
# Get the license specifier
|
|
|
|
|
$license = isset( $options['license'] ) ? $options['license'] : '';
|
2007-05-09 19:09:33 +00:00
|
|
|
|
2006-05-15 10:57:52 +00:00
|
|
|
# Batch "upload" operation
|
2007-07-28 22:21:23 +00:00
|
|
|
if( ( $count = count( $files ) ) > 0 ) {
|
2007-06-07 15:07:12 +00:00
|
|
|
|
2007-06-05 18:39:02 +00:00
|
|
|
foreach( $files as $file ) {
|
|
|
|
|
$base = wfBaseName( $file );
|
|
|
|
|
|
|
|
|
|
# Validate a title
|
2008-12-01 17:14:30 +00:00
|
|
|
$title = Title::makeTitleSafe( NS_FILE, $base );
|
2007-06-05 18:39:02 +00:00
|
|
|
if( !is_object( $title ) ) {
|
|
|
|
|
echo( "{$base} could not be imported; a valid title cannot be produced\n" );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Check existence
|
|
|
|
|
$image = wfLocalFile( $title );
|
|
|
|
|
if( $image->exists() ) {
|
2007-07-28 22:21:23 +00:00
|
|
|
if( isset( $options['overwrite'] ) ) {
|
|
|
|
|
echo( "{$base} exists, overwriting..." );
|
|
|
|
|
$svar = 'overwritten';
|
|
|
|
|
} else {
|
|
|
|
|
echo( "{$base} exists, skipping\n" );
|
|
|
|
|
$skipped++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
echo( "Importing {$base}..." );
|
|
|
|
|
$svar = 'added';
|
2007-06-05 18:39:02 +00:00
|
|
|
}
|
2007-07-28 22:21:23 +00:00
|
|
|
|
2008-11-27 13:07:30 +00:00
|
|
|
# Find comment text
|
|
|
|
|
$commentText = false;
|
|
|
|
|
|
|
|
|
|
if ( $commentExt ) {
|
|
|
|
|
$f = findAuxFile( $file, $commentExt );
|
|
|
|
|
if ( !$f ) {
|
|
|
|
|
echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
|
|
|
|
|
} else {
|
|
|
|
|
$commentText = file_get_contents( $f );
|
|
|
|
|
if ( !$f ) {
|
|
|
|
|
echo( " Failed to load comment file {$f}, using default comment. " );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$commentText ) {
|
|
|
|
|
$commentText = $comment;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-28 22:21:23 +00:00
|
|
|
# Import the file
|
2008-11-27 13:07:30 +00:00
|
|
|
if ( isset( $options['dry'] ) ) {
|
|
|
|
|
echo( " publishing {$file}... " );
|
|
|
|
|
} else {
|
|
|
|
|
$archive = $image->publish( $file );
|
|
|
|
|
if( WikiError::isError( $archive ) || !$archive->isGood() ) {
|
|
|
|
|
echo( "failed.\n" );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2007-06-05 18:39:02 +00:00
|
|
|
}
|
2009-03-03 02:56:30 +00:00
|
|
|
|
|
|
|
|
$doProtect = false;
|
|
|
|
|
$restrictions = array();
|
|
|
|
|
|
|
|
|
|
global $wgRestrictionLevels;
|
|
|
|
|
|
|
|
|
|
$protectLevel = isset($options['protect']) ? $options['protect'] : null;
|
|
|
|
|
|
|
|
|
|
if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
|
|
|
|
|
$restrictions['move'] = $protectLevel;
|
|
|
|
|
$restrictions['edit'] = $protectLevel;
|
|
|
|
|
$doProtect = true;
|
|
|
|
|
}
|
|
|
|
|
if (isset($options['unprotect'])) {
|
|
|
|
|
$restrictions['move'] = '';
|
|
|
|
|
$restrictions['edit'] = '';
|
|
|
|
|
$doProtect = true;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-28 22:21:23 +00:00
|
|
|
|
|
|
|
|
$$svar++;
|
2008-11-27 13:07:30 +00:00
|
|
|
if ( isset( $options['dry'] ) ) {
|
|
|
|
|
echo( "done.\n" );
|
|
|
|
|
} else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
|
2007-06-05 18:39:02 +00:00
|
|
|
# We're done!
|
|
|
|
|
echo( "done.\n" );
|
2009-03-03 02:56:30 +00:00
|
|
|
if ($doProtect) {
|
|
|
|
|
# Protect the file
|
|
|
|
|
$article = new Article( $title );
|
|
|
|
|
echo "\nWaiting for slaves...\n";
|
|
|
|
|
// Wait for slaves.
|
|
|
|
|
sleep(2.0);
|
|
|
|
|
wfWaitForSlaves( 1.0 );
|
|
|
|
|
|
|
|
|
|
echo( "\nSetting image restrictions ... " );
|
|
|
|
|
if ( $article->updateRestrictions($restrictions) )
|
|
|
|
|
echo( "done.\n" );
|
|
|
|
|
else
|
|
|
|
|
echo( "failed.\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-05 18:39:02 +00:00
|
|
|
} else {
|
|
|
|
|
echo( "failed.\n" );
|
|
|
|
|
}
|
2007-07-28 22:21:23 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Print out some statistics
|
|
|
|
|
echo( "\n" );
|
|
|
|
|
foreach( array( 'count' => 'Found', 'added' => 'Added',
|
|
|
|
|
'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
|
|
|
|
|
if( $$var > 0 )
|
|
|
|
|
echo( "{$desc}: {$$var}\n" );
|
2006-05-15 10:57:52 +00:00
|
|
|
}
|
2007-06-07 15:07:12 +00:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
echo( "No suitable files could be found for import.\n" );
|
2006-05-15 10:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
showUsage();
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-06 14:41:33 +00:00
|
|
|
exit(0);
|
2006-05-15 10:57:52 +00:00
|
|
|
|
|
|
|
|
function showUsage( $reason = false ) {
|
2007-05-09 19:09:33 +00:00
|
|
|
if( $reason ) {
|
2006-05-15 10:57:52 +00:00
|
|
|
echo( $reason . "\n" );
|
2007-05-09 19:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
2006-09-11 13:12:52 +00:00
|
|
|
echo <<<END
|
2007-07-28 22:21:23 +00:00
|
|
|
Imports images and other media files into the wiki
|
2007-06-07 15:07:12 +00:00
|
|
|
USAGE: php importImages.php [options] <dir>
|
2006-09-11 13:12:52 +00:00
|
|
|
|
|
|
|
|
<dir> : Path to the directory containing images to be imported
|
|
|
|
|
|
|
|
|
|
Options:
|
2007-07-28 22:21:23 +00:00
|
|
|
--extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
|
2008-11-27 13:07:30 +00:00
|
|
|
--overwrite Overwrite existing images if a conflicting-named image is found
|
2007-06-07 15:07:12 +00:00
|
|
|
--user=<username> Set username of uploader, default 'Maintenance script'
|
|
|
|
|
--comment=<text> Set upload summary comment, default 'Importing image file'
|
2008-11-27 13:07:30 +00:00
|
|
|
--comment-file=<file> Set upload summary comment the the content of <file>.
|
|
|
|
|
--comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
|
|
|
|
|
but the extension <ext>.
|
2007-06-07 15:07:12 +00:00
|
|
|
--license=<code> Use an optional license template
|
2008-11-27 13:07:30 +00:00
|
|
|
--dry Dry run, don't import anything
|
2009-03-03 02:56:30 +00:00
|
|
|
--protect=<protect> Specify the protect value (autoconfirmed,sysop)
|
|
|
|
|
--unprotect Unprotects all uploaded images
|
2006-09-11 13:12:52 +00:00
|
|
|
|
|
|
|
|
END;
|
2009-04-06 14:41:33 +00:00
|
|
|
exit(1);
|
2007-07-28 22:21:23 +00:00
|
|
|
}
|