* (bug 6204) Fixes for indentation with $wgMaxTocLevel:
[lhc/web/wiklou.git] / maintenance / importImages.php
1 <?php
2
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
6 *
7 * @addtogroup Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 */
10
11 require_once( 'commandLine.inc' );
12 require_once( 'importImages.inc.php' );
13 echo( "Import Images\n\n" );
14
15 # Need a directory and at least one extension
16 if( count( $args ) > 1 ) {
17
18 $dir = array_shift( $args );
19
20 # Check the allowed extensions
21 while( $ext = array_shift( $args ) )
22 $exts[] = ltrim( $ext, '.' );
23
24 # Search the directory given and pull out suitable candidates
25 $files = findFiles( $dir, $exts );
26
27 # Initialise the user for this operation
28 $user = isset( $options['user'] )
29 ? User::newFromName( $options['user'] )
30 : User::newFromName( 'Maintenance script' );
31 if( !$user instanceof User )
32 $user = User::newFromName( 'Maintenance script' );
33 $wgUser = $user;
34
35 # Get the upload comment
36 $comment = isset( $options['comment'] )
37 ? $options['comment']
38 : 'Importing image file';
39
40 # Get the license specifier
41 $license = isset( $options['license'] ) ? $options['license'] : '';
42
43 # Batch "upload" operation
44 foreach( $files as $file ) {
45
46 $base = wfBaseName( $file );
47
48 # Validate a title
49 $title = Title::makeTitleSafe( NS_IMAGE, $base );
50 if( is_object( $title ) ) {
51
52 # Check existence
53 $image = new Image( $title );
54 if( !$image->exists() ) {
55
56 global $wgUploadDirectory;
57
58 # copy() doesn't create paths so if the hash path doesn't exist, we
59 # have to create it
60 makeHashPath( wfGetHashPath( $image->name ) );
61
62 # Stash the file
63 echo( "Saving {$base}..." );
64
65 if( copy( $file, $image->getFullPath() ) ) {
66
67 echo( "importing..." );
68
69 # Grab the metadata
70 $image->loadFromFile();
71
72 # Record the upload
73 if( $image->recordUpload( '', $comment, $license ) ) {
74
75 # We're done!
76 echo( "done.\n" );
77
78 } else {
79 echo( "failed.\n" );
80 }
81
82 } else {
83 echo( "failed.\n" );
84 }
85
86 } else {
87 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
88 }
89
90 } else {
91 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
92 }
93
94 }
95
96
97 } else {
98 showUsage();
99 }
100
101 exit();
102
103 function showUsage( $reason = false ) {
104 if( $reason )
105 echo( $reason . "\n" );
106 echo <<<END
107 USAGE: php importImages.php [options] <dir> <ext1> ...
108
109 <dir> : Path to the directory containing images to be imported
110 <ext1+> File extensions to import
111
112 Options:
113 --user=<username> Set username of uploader, default 'Image import script'
114 --comment=<text> Set upload summary comment, default 'Importing image file'
115 --license=<code> Use an optional license template
116
117 END;
118 exit();
119 }
120
121 ?>