* (bug 11728) Unify layout of enhanced watchlist/recent changes:
[mediawiki.git] / maintenance / importImages.php
blob63bbec5fc700013f406a2cd25b0924a15c2dc7e1
1 <?php
3 /**
4 * Maintenance script to import one or more images from the local file system into
5 * the wiki without using the web-based interface
7 * @file
8 * @ingroup Maintenance
9 * @author Rob Church <robchur@gmail.com>
12 $optionsWithArguments = array( 'extensions', 'overwrite' );
13 require_once( 'commandLine.inc' );
14 require_once( 'importImages.inc.php' );
15 $added = $skipped = $overwritten = 0;
17 echo( "Import Images\n\n" );
19 # Need a path
20 if( count( $args ) > 0 ) {
22 $dir = $args[0];
24 # Prepare the list of allowed extensions
25 global $wgFileExtensions;
26 $extensions = isset( $options['extensions'] )
27 ? explode( ',', strtolower( $options['extensions'] ) )
28 : $wgFileExtensions;
30 # Search the path provided for candidates for import
31 $files = findFiles( $dir, $extensions );
33 # Initialise the user for this operation
34 $user = isset( $options['user'] )
35 ? User::newFromName( $options['user'] )
36 : User::newFromName( 'Maintenance script' );
37 if( !$user instanceof User )
38 $user = User::newFromName( 'Maintenance script' );
39 $wgUser = $user;
41 # Get the upload comment
42 $comment = isset( $options['comment'] )
43 ? $options['comment']
44 : 'Importing image file';
46 # Get the license specifier
47 $license = isset( $options['license'] ) ? $options['license'] : '';
49 # Batch "upload" operation
50 if( ( $count = count( $files ) ) > 0 ) {
52 foreach( $files as $file ) {
53 $base = wfBaseName( $file );
55 # Validate a title
56 $title = Title::makeTitleSafe( NS_IMAGE, $base );
57 if( !is_object( $title ) ) {
58 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
59 continue;
62 # Check existence
63 $image = wfLocalFile( $title );
64 if( $image->exists() ) {
65 if( isset( $options['overwrite'] ) ) {
66 echo( "{$base} exists, overwriting..." );
67 $svar = 'overwritten';
68 } else {
69 echo( "{$base} exists, skipping\n" );
70 $skipped++;
71 continue;
73 } else {
74 echo( "Importing {$base}..." );
75 $svar = 'added';
78 # Import the file
79 $archive = $image->publish( $file );
80 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
81 echo( "failed.\n" );
82 continue;
85 $$svar++;
86 if ( $image->recordUpload( $archive->value, $comment, $license ) ) {
87 # We're done!
88 echo( "done.\n" );
89 } else {
90 echo( "failed.\n" );
95 # Print out some statistics
96 echo( "\n" );
97 foreach( array( 'count' => 'Found', 'added' => 'Added',
98 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
99 if( $$var > 0 )
100 echo( "{$desc}: {$$var}\n" );
103 } else {
104 echo( "No suitable files could be found for import.\n" );
107 } else {
108 showUsage();
111 exit();
113 function showUsage( $reason = false ) {
114 if( $reason ) {
115 echo( $reason . "\n" );
118 echo <<<END
119 Imports images and other media files into the wiki
120 USAGE: php importImages.php [options] <dir>
122 <dir> : Path to the directory containing images to be imported
124 Options:
125 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
126 --overwrite Overwrite existing images if a conflicting-named image is found
127 --user=<username> Set username of uploader, default 'Maintenance script'
128 --comment=<text> Set upload summary comment, default 'Importing image file'
129 --license=<code> Use an optional license template
131 END;
132 exit();