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