whitespaces killing, adding {}
[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
25 # Search the directory given and pull out suitable candidates
26 $files = findFiles( $dir, $exts );
27
28 # Initialise the user for this operation
29 $user = isset( $options['user'] )
30 ? User::newFromName( $options['user'] )
31 : User::newFromName( 'Maintenance script' );
32 if( !$user instanceof User )
33 $user = User::newFromName( 'Maintenance script' );
34 $wgUser = $user;
35
36 # Get the upload comment
37 $comment = isset( $options['comment'] )
38 ? $options['comment']
39 : 'Importing image file';
40
41 # Get the license specifier
42 $license = isset( $options['license'] ) ? $options['license'] : '';
43
44 # Batch "upload" operation
45 foreach( $files as $file ) {
46
47 $base = wfBaseName( $file );
48
49 # Validate a title
50 $title = Title::makeTitleSafe( NS_IMAGE, $base );
51 if( is_object( $title ) ) {
52
53 # Check existence
54 $image = new Image( $title );
55 if( !$image->exists() ) {
56
57 global $wgUploadDirectory;
58
59 # copy() doesn't create paths so if the hash path doesn't exist, we
60 # have to create it
61 makeHashPath( wfGetHashPath( $image->name ) );
62
63 # Stash the file
64 echo( "Saving {$base}..." );
65
66 if( copy( $file, $image->getFullPath() ) ) {
67
68 echo( "importing..." );
69
70 # Grab the metadata
71 $image->loadFromFile();
72
73 # Record the upload
74 if( $image->recordUpload( '', $comment, $license ) ) {
75
76 # We're done!
77 echo( "done.\n" );
78
79 } else {
80 echo( "failed.\n" );
81 }
82
83 } else {
84 echo( "failed.\n" );
85 }
86
87 } else {
88 echo( "{$base} could not be imported; a file with this name exists in the wiki\n" );
89 }
90
91 } else {
92 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
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 }
107
108 echo <<<END
109 USAGE: php importImages.php [options] <dir> <ext1> ...
110
111 <dir> : Path to the directory containing images to be imported
112 <ext1+> File extensions to import
113
114 Options:
115 --user=<username> Set username of uploader, default 'Image import script'
116 --comment=<text> Set upload summary comment, default 'Importing image file'
117 --license=<code> Use an optional license template
118
119 END;
120 exit();
121 }
122
123 ?>