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' );
13 require_once( 'commandLine.inc' );
14 require_once( 'importImages.inc.php' );
15 $added = $skipped = $overwritten = 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 ($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' );
48 # Get the upload comment
49 $comment = 'Importing image file';
51 if ( isset( $options['comment-file'] ) ) {
52 $comment = file_get_contents( $options['comment-file'] );
53 if ( $comment === false || $comment === NULL ) {
54 die( "failed to read comment file: {$options['comment-file']}\n" );
57 else if ( isset( $options['comment'] ) ) {
58 $comment = $options['comment'];
61 $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
63 # Get the license specifier
64 $license = isset( $options['license'] ) ? $options['license'] : '';
66 # Batch "upload" operation
67 if( ( $count = count( $files ) ) > 0 ) {
69 foreach( $files as $file ) {
70 $base = wfBaseName( $file );
73 $title = Title::makeTitleSafe( NS_FILE, $base );
74 if( !is_object( $title ) ) {
75 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
80 $image = wfLocalFile( $title );
81 if( $image->exists() ) {
82 if( isset( $options['overwrite'] ) ) {
83 echo( "{$base} exists, overwriting..." );
84 $svar = 'overwritten';
86 echo( "{$base} exists, skipping\n" );
91 echo( "Importing {$base}..." );
99 $f = findAuxFile( $file, $commentExt );
101 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
103 $commentText = file_get_contents( $f );
105 echo( " Failed to load comment file {$f}, using default comment. " );
110 if ( !$commentText ) {
111 $commentText = $comment;
115 if ( isset( $options['dry'] ) ) {
116 echo( " publishing {$file}... " );
118 $archive = $image->publish( $file );
119 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
126 $restrictions = array();
128 global $wgRestrictionLevels;
130 $protectLevel = isset($options['protect']) ? $options['protect'] : null;
132 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
133 $restrictions['move'] = $protectLevel;
134 $restrictions['edit'] = $protectLevel;
137 if (isset($options['unprotect'])) {
138 $restrictions['move'] = '';
139 $restrictions['edit'] = '';
145 if ( isset( $options['dry'] ) ) {
147 } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
152 $article = new Article( $title );
153 echo "\nWaiting for slaves...\n";
156 wfWaitForSlaves( 1.0 );
158 echo( "\nSetting image restrictions ... " );
159 if ( $article->updateRestrictions($restrictions) )
171 # Print out some statistics
173 foreach( array( 'count' => 'Found', 'added' => 'Added',
174 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
176 echo( "{$desc}: {$$var}\n" );
180 echo( "No suitable files could be found for import.\n" );
189 function showUsage( $reason = false ) {
191 echo( $reason . "\n" );
195 Imports images and other media files into the wiki
196 USAGE: php importImages.php [options] <dir>
198 <dir> : Path to the directory containing images to be imported
201 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
202 --overwrite Overwrite existing images if a conflicting-named image is found
203 --user=<username> Set username of uploader, default 'Maintenance script'
204 --comment=<text> Set upload summary comment, default 'Importing image file'
205 --comment-file=<file> Set upload summary comment the the content of <file>.
206 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
207 but the extension <ext>.
208 --license=<code> Use an optional license template
209 --dry Dry run, don't import anything
210 --protect=<protect> Specify the protect value (autoconfirmed,sysop)
211 --unprotect Unprotects all uploaded images