4 * Maintenance script to import one or more images from the local file system into
5 * the wiki without using the web-based interface
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" );
20 if( count( $args ) > 0 ) {
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'] ) )
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' );
49 $from = @$options['from'];
52 $sleep = @$options['sleep'];
53 if ( $sleep ) $sleep = (int)$sleep;
56 $limit = @$options['limit'];
57 if ( $limit ) $limit = (int)$limit;
59 # Get the upload comment
60 $comment = 'Importing image file';
62 if ( isset( $options['comment-file'] ) ) {
63 $comment = file_get_contents( $options['comment-file'] );
64 if ( $comment === false ||
$comment === NULL ) {
65 die( "failed to read comment file: {$options['comment-file']}\n" );
68 else if ( isset( $options['comment'] ) ) {
69 $comment = $options['comment'];
72 $commentExt = isset( $options['comment-ext'] ) ?
$options['comment-ext'] : false;
74 # Get the license specifier
75 $license = isset( $options['license'] ) ?
$options['license'] : '';
77 # Batch "upload" operation
78 if( ( $count = count( $files ) ) > 0 ) {
80 foreach( $files as $file ) {
81 $base = wfBaseName( $file );
84 $title = Title
::makeTitleSafe( NS_FILE
, $base );
85 if( !is_object( $title ) ) {
86 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
91 if ( $from == $title->getDBkey() ) {
100 $image = wfLocalFile( $title );
101 if( $image->exists() ) {
102 if( isset( $options['overwrite'] ) ) {
103 echo( "{$base} exists, overwriting..." );
104 $svar = 'overwritten';
106 echo( "{$base} exists, skipping\n" );
111 echo( "Importing {$base}..." );
116 $commentText = false;
119 $f = findAuxFile( $file, $commentExt );
121 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
123 $commentText = file_get_contents( $f );
125 echo( " Failed to load comment file {$f}, using default comment. " );
129 if ( $commentText && $comment ) {
130 $commentText = trim( $commentText ) . "\n\n" . trim( $comment );
134 if ( !$commentText ) {
135 $commentText = $comment;
139 if ( isset( $options['dry'] ) ) {
140 echo( " publishing {$file}... " );
142 $archive = $image->publish( $file );
143 if( WikiError
::isError( $archive ) ||
!$archive->isGood() ) {
151 $restrictions = array();
153 global $wgRestrictionLevels;
155 $protectLevel = isset($options['protect']) ?
$options['protect'] : null;
157 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
158 $restrictions['move'] = $protectLevel;
159 $restrictions['edit'] = $protectLevel;
162 if (isset($options['unprotect'])) {
163 $restrictions['move'] = '';
164 $restrictions['edit'] = '';
169 if ( isset( $options['dry'] ) ) {
171 } else if ( $image->recordUpload( $archive->value
, $commentText, $license ) ) {
176 $article = new Article( $title );
177 echo "\nWaiting for slaves...\n";
180 wfWaitForSlaves( 1.0 );
182 echo( "\nSetting image restrictions ... " );
183 if ( $article->updateRestrictions($restrictions) )
197 if ( $limit && $processed >= $limit )
204 # Print out some statistics
206 foreach( array( 'count' => 'Found', 'limit' => 'Limit', 'ignored' => 'Ignored',
207 'added' => 'Added', 'skipped' => 'Skipped', 'overwritten' => 'Overwritten',
208 'failed' => 'Failed' ) as $var => $desc ) {
210 echo( "{$desc}: {$$var}\n" );
214 echo( "No suitable files could be found for import.\n" );
223 function showUsage( $reason = false ) {
225 echo( $reason . "\n" );
229 Imports images and other media files into the wiki
230 USAGE: php importImages.php [options] <dir>
232 <dir> : Path to the directory containing images to be imported
235 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
236 --overwrite Overwrite existing images with the same name (default is to skip them)
237 --limit=<num> Limit the number of images to process. Ignored or skipped images are not counted.
238 --from=<name> Ignore all files until the one with the given name. Useful for resuming
239 aborted imports. <name> should be the file's canonical database form.
240 --sleep=<sec> Sleep between files. Useful mostly for debugging.
241 --user=<username> Set username of uploader, default 'Maintenance script'
242 --comment=<text> Set upload summary comment, default 'Importing image file'.
243 --comment-file=<file> Set upload summary comment the the content of <file>.
244 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
245 but the extension <ext>. If a global comment is also given, it is appended.
246 --license=<code> Use an optional license template
247 --dry Dry run, don't import anything
248 --protect=<protect> Specify the protect value (autoconfirmed,sysop)
249 --unprotect Unprotects all uploaded images