support more greedy params
[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 * @file
8 * @ingroup Maintenance
9 * @author Rob Church <robchur@gmail.com>
10 */
11
12 $optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license' );
13 require_once( 'commandLine.inc' );
14 require_once( 'importImages.inc.php' );
15 $added = $skipped = $overwritten = 0;
16
17 echo( "Import Images\n\n" );
18
19 # Need a path
20 if( count( $args ) > 0 ) {
21
22 $dir = $args[0];
23
24 # Prepare the list of allowed extensions
25 global $wgFileExtensions;
26 $extensions = isset( $options['extensions'] )
27 ? explode( ',', strtolower( $options['extensions'] ) )
28 : $wgFileExtensions;
29
30 # Search the path provided for candidates for import
31 $files = findFiles( $dir, $extensions );
32
33 # Initialise the user for this operation
34 $user = isset( $options['user'] )
35 ? User::newFromName( $options['user'] )
36 : User::newFromName( 'Maintenance script' );
37 if( !$user instanceof User )
38 $user = User::newFromName( 'Maintenance script' );
39 $wgUser = $user;
40
41 # Get the upload comment
42 $comment = 'Importing image file';
43
44 if ( isset( $options['comment-file'] ) ) {
45 $comment = file_get_contents( $options['comment-file'] );
46 if ( $comment === false || $comment === NULL ) {
47 die( "failed to read comment file: {$options['comment-file']}\n" );
48 }
49 }
50 else if ( isset( $options['comment'] ) ) {
51 $comment = $options['comment'];
52 }
53
54 $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
55
56 # Get the license specifier
57 $license = isset( $options['license'] ) ? $options['license'] : '';
58
59 # Batch "upload" operation
60 if( ( $count = count( $files ) ) > 0 ) {
61
62 foreach( $files as $file ) {
63 $base = wfBaseName( $file );
64
65 # Validate a title
66 $title = Title::makeTitleSafe( NS_IMAGE, $base );
67 if( !is_object( $title ) ) {
68 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
69 continue;
70 }
71
72 # Check existence
73 $image = wfLocalFile( $title );
74 if( $image->exists() ) {
75 if( isset( $options['overwrite'] ) ) {
76 echo( "{$base} exists, overwriting..." );
77 $svar = 'overwritten';
78 } else {
79 echo( "{$base} exists, skipping\n" );
80 $skipped++;
81 continue;
82 }
83 } else {
84 echo( "Importing {$base}..." );
85 $svar = 'added';
86 }
87
88 # Find comment text
89 $commentText = false;
90
91 if ( $commentExt ) {
92 $f = findAuxFile( $file, $commentExt );
93 if ( !$f ) {
94 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
95 } else {
96 $commentText = file_get_contents( $f );
97 if ( !$f ) {
98 echo( " Failed to load comment file {$f}, using default comment. " );
99 }
100 }
101 }
102
103 if ( !$commentText ) {
104 $commentText = $comment;
105 }
106
107 # Import the file
108 if ( isset( $options['dry'] ) ) {
109 echo( " publishing {$file}... " );
110 } else {
111 $archive = $image->publish( $file );
112 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
113 echo( "failed.\n" );
114 continue;
115 }
116 }
117
118 $$svar++;
119 if ( isset( $options['dry'] ) ) {
120 echo( "done.\n" );
121 } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
122 # We're done!
123 echo( "done.\n" );
124 } else {
125 echo( "failed.\n" );
126 }
127
128 }
129
130 # Print out some statistics
131 echo( "\n" );
132 foreach( array( 'count' => 'Found', 'added' => 'Added',
133 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
134 if( $$var > 0 )
135 echo( "{$desc}: {$$var}\n" );
136 }
137
138 } else {
139 echo( "No suitable files could be found for import.\n" );
140 }
141
142 } else {
143 showUsage();
144 }
145
146 exit();
147
148 function showUsage( $reason = false ) {
149 if( $reason ) {
150 echo( $reason . "\n" );
151 }
152
153 echo <<<END
154 Imports images and other media files into the wiki
155 USAGE: php importImages.php [options] <dir>
156
157 <dir> : Path to the directory containing images to be imported
158
159 Options:
160 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
161 --overwrite Overwrite existing images if a conflicting-named image is found
162 --user=<username> Set username of uploader, default 'Maintenance script'
163 --comment=<text> Set upload summary comment, default 'Importing image file'
164 --comment-file=<file> Set upload summary comment the the content of <file>.
165 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
166 but the extension <ext>.
167 --license=<code> Use an optional license template
168 --dry Dry run, don't import anything
169
170 END;
171 exit();
172 }