Forgot to update table sql per bug 10280
[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 $optionsWithArguments = array( 'extensions' );
12 require_once( 'commandLine.inc' );
13 require_once( 'importImages.inc.php' );
14 echo( "Import Images\n\n" );
15
16 # Need a path
17 if( count( $args ) > 0 ) {
18
19 $dir = $args[0];
20
21 # Prepare the list of allowed extensions
22 global $wgFileExtensions;
23 $extensions = isset( $options['extensions'] )
24 ? explode( ',', strtolower( $options['extensions'] ) )
25 : $wgFileExtensions;
26
27 # Search the path provided for candidates for import
28 $files = findFiles( $dir, $extensions );
29
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;
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 global $wgUploadDirectory;
48 if( count( $files ) > 0 ) {
49
50 foreach( $files as $file ) {
51 $base = wfBaseName( $file );
52
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;
58 }
59
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;
65 }
66
67 # Stash the file
68 echo( "Saving {$base}..." );
69
70 $archive = $image->publish( $file );
71 if ( WikiError::isError( $archive ) ) {
72 echo( "failed.\n" );
73 continue;
74 }
75 echo( "importing..." );
76
77 if ( $image->recordUpload( $archive, $comment, $license ) ) {
78 # We're done!
79 echo( "done.\n" );
80 } else {
81 echo( "failed.\n" );
82 }
83 }
84
85 } else {
86 echo( "No suitable files could be found for import.\n" );
87 }
88
89 } else {
90 showUsage();
91 }
92
93 exit();
94
95 function showUsage( $reason = false ) {
96 if( $reason ) {
97 echo( $reason . "\n" );
98 }
99
100 echo <<<END
101 USAGE: php importImages.php [options] <dir>
102
103 <dir> : Path to the directory containing images to be imported
104
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
110
111 END;
112 exit();
113 }
114
115 ?>