* Fixed installation on MyISAM or old InnoDB with charset=utf8, was giving overlong...
[mediawiki.git] / maintenance / importImages.php
blob0c560d7a9aa4396f791dbf449ac3efb437ce4980
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 * @addtogroup Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 */
11 $optionsWithArguments = array( 'extensions' );
12 require_once( 'commandLine.inc' );
13 require_once( 'importImages.inc.php' );
14 echo( "Import Images\n\n" );
16 # Need a path
17 if( count( $args ) > 0 ) {
19 $dir = $args[0];
21 # Prepare the list of allowed extensions
22 global $wgFileExtensions;
23 $extensions = isset( $options['extensions'] )
24 ? explode( ',', strtolower( $options['extensions'] ) )
25 : $wgFileExtensions;
27 # Search the path provided for candidates for import
28 $files = findFiles( $dir, $extensions );
30 # Initialise the user for this operation
31 $user = isset( $options['user'] )
32 ? User::newFromName( $options['user'] )
33 : User::newFromName( 'Maintenance script' );
34 if( !$user instanceof User )
35 $user = User::newFromName( 'Maintenance script' );
36 $wgUser = $user;
38 # Get the upload comment
39 $comment = isset( $options['comment'] )
40 ? $options['comment']
41 : 'Importing image file';
43 # Get the license specifier
44 $license = isset( $options['license'] ) ? $options['license'] : '';
46 # Batch "upload" operation
47 global $wgUploadDirectory;
48 if( count( $files ) > 0 ) {
50 foreach( $files as $file ) {
51 $base = wfBaseName( $file );
53 # Validate a title
54 $title = Title::makeTitleSafe( NS_IMAGE, $base );
55 if( !is_object( $title ) ) {
56 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
57 continue;
60 # Check existence
61 $image = wfLocalFile( $title );
62 if( $image->exists() ) {
63 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
64 continue;
67 # Stash the file
68 echo( "Saving {$base}..." );
70 $archive = $image->publish( $file );
71 if ( WikiError::isError( $archive ) ) {
72 echo( "failed.\n" );
73 continue;
75 echo( "importing..." );
77 if ( $image->recordUpload( $archive, $comment, $license ) ) {
78 # We're done!
79 echo( "done.\n" );
80 } else {
81 echo( "failed.\n" );
85 } else {
86 echo( "No suitable files could be found for import.\n" );
89 } else {
90 showUsage();
93 exit();
95 function showUsage( $reason = false ) {
96 if( $reason ) {
97 echo( $reason . "\n" );
100 echo <<<END
101 USAGE: php importImages.php [options] <dir>
103 <dir> : Path to the directory containing images to be imported
105 Options:
106 --extensions=<exts> Comma-separated list of allowable extensions, defaults to $wgFileExtensions
107 --user=<username> Set username of uploader, default 'Maintenance script'
108 --comment=<text> Set upload summary comment, default 'Importing image file'
109 --license=<code> Use an optional license template
111 END;
112 exit();