* Make some more messages '*-summary' customizeable through Special:Allmessages or...
[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 * @package MediaWiki
8 * @subpackage Maintenance
9 * @author Rob Church <robchur@gmail.com>
10 */
11
12 require_once( 'commandLine.inc' );
13 require_once( 'importImages.inc.php' );
14 echo( "Import Images\n\n" );
15
16 # Need a directory and at least one extension
17 if( count( $args ) > 1 ) {
18
19 $dir = array_shift( $args );
20
21 # Check the allowed extensions
22 while( $ext = array_shift( $args ) )
23 $exts[] = ltrim( $ext, '.' );
24
25 # Search the directory given and pull out suitable candidates
26 $files = findFiles( $dir, $exts );
27
28 # Set up a fake user for this operation
29 if( isset( $options['user'] ) ) {
30 $wgUser = User::newFromName( $options['user'] );
31 } else {
32 $wgUser = User::newFromName( 'Image import script' );
33 $wgUser->setLoaded( true );
34 }
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
98 } else {
99 showUsage();
100 }
101
102 exit();
103
104 function showUsage( $reason = false ) {
105 if( $reason )
106 echo( $reason . "\n" );
107 echo <<<END
108 USAGE: php importImages.php [options] <dir> <ext1> ...
109
110 <dir> : Path to the directory containing images to be imported
111 <ext1+> File extensions to import
112
113 Options:
114 --user=<username> Set username of uploader, default 'Image import script'
115 --comment=<text> Set upload summary comment, default 'Importing image file'
116 --license=<code> Use an optional license template
117
118 END;
119 exit();
120 }
121
122 ?>