Fixed wrong field definition for filearchive. Fixed delete function to handle encodin...
[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', 'sleep', 'limit', 'from' );
13 require_once( dirname(__FILE__) . '/commandLine.inc' );
14 require_once( 'importImages.inc' );
15 $processed = $added = $ignored = $skipped = $overwritten = $failed = 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 # Check Protection
25 if (isset($options['protect']) && isset($options['unprotect']))
26 die("Cannot specify both protect and unprotect. Only 1 is allowed.\n");
27
28 if (isset($options['protect']) && $options['protect'] == 1)
29 die("You must specify a protection option.\n");
30
31 # Prepare the list of allowed extensions
32 global $wgFileExtensions;
33 $extensions = isset( $options['extensions'] )
34 ? explode( ',', strtolower( $options['extensions'] ) )
35 : $wgFileExtensions;
36
37 # Search the path provided for candidates for import
38 $files = findFiles( $dir, $extensions );
39
40 # Initialise the user for this operation
41 $user = isset( $options['user'] )
42 ? User::newFromName( $options['user'] )
43 : User::newFromName( 'Maintenance script' );
44 if( !$user instanceof User )
45 $user = User::newFromName( 'Maintenance script' );
46 $wgUser = $user;
47
48 # Get block check. If a value is given, this specified how often the check is performed
49 if ( isset( $options['check-userblock'] ) ) {
50 if ( !$options['check-userblock'] ) $checkUserBlock = 1;
51 else $checkUserBlock = (int)$options['check-userblock'];
52 } else {
53 $checkUserBlock = false;
54 }
55
56 # Get --from
57 $from = @$options['from'];
58
59 # Get sleep time.
60 $sleep = @$options['sleep'];
61 if ( $sleep ) $sleep = (int)$sleep;
62
63 # Get limit number
64 $limit = @$options['limit'];
65 if ( $limit ) $limit = (int)$limit;
66
67 # Get the upload comment
68 $comment = 'Importing image file';
69
70 if ( isset( $options['comment-file'] ) ) {
71 $comment = file_get_contents( $options['comment-file'] );
72 if ( $comment === false || $comment === NULL ) {
73 die( "failed to read comment file: {$options['comment-file']}\n" );
74 }
75 }
76 else if ( isset( $options['comment'] ) ) {
77 $comment = $options['comment'];
78 }
79
80 $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
81
82 # Get the license specifier
83 $license = isset( $options['license'] ) ? $options['license'] : '';
84
85 # Batch "upload" operation
86 if( ( $count = count( $files ) ) > 0 ) {
87
88 foreach( $files as $file ) {
89 $base = wfBaseName( $file );
90
91 # Validate a title
92 $title = Title::makeTitleSafe( NS_FILE, $base );
93 if( !is_object( $title ) ) {
94 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
95 continue;
96 }
97
98 if ( $from ) {
99 if ( $from == $title->getDBkey() ) {
100 $from = NULL;
101 } else {
102 $ignored++;
103 continue;
104 }
105 }
106
107 if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
108 $user->clearInstanceCache( 'name' ); //reload from DB!
109 if ( $user->isBlocked() ) {
110 echo( $user->getName() . " was blocked! Aborting." );
111 break;
112 }
113 }
114
115 # Check existence
116 $image = wfLocalFile( $title );
117 if( $image->exists() ) {
118 if( isset( $options['overwrite'] ) ) {
119 echo( "{$base} exists, overwriting..." );
120 $svar = 'overwritten';
121 } else {
122 echo( "{$base} exists, skipping\n" );
123 $skipped++;
124 continue;
125 }
126 } else {
127 if ( isset( $options['skip-dupes'] ) ) {
128 $repo = $image->getRepo();
129 $sha1 = File::sha1Base36( $file ); #XXX: we end up calculating this again when actually uploading. that sucks.
130
131 $dupes = $repo->findBySha1( $sha1 );
132
133 if ( $dupes ) {
134 echo( "{$base} already exists as " . $dupes[0]->getName() . ", skipping\n" );
135 $skipped++;
136 continue;
137 }
138 }
139
140 echo( "Importing {$base}..." );
141 $svar = 'added';
142 }
143
144 # Find comment text
145 $commentText = false;
146
147 if ( $commentExt ) {
148 $f = findAuxFile( $file, $commentExt );
149 if ( !$f ) {
150 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
151 } else {
152 $commentText = file_get_contents( $f );
153 if ( !$f ) {
154 echo( " Failed to load comment file {$f}, using default comment. " );
155 }
156 }
157
158 if ( $commentText && $comment ) {
159 $commentText = trim( $commentText ) . "\n\n" . trim( $comment );
160 }
161 }
162
163 if ( !$commentText ) {
164 $commentText = $comment;
165 }
166
167 # Import the file
168 if ( isset( $options['dry'] ) ) {
169 echo( " publishing {$file}... " );
170 } else {
171 $archive = $image->publish( $file );
172 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
173 echo( "failed.\n" );
174 $failed++;
175 continue;
176 }
177 }
178
179 $doProtect = false;
180 $restrictions = array();
181
182 global $wgRestrictionLevels;
183
184 $protectLevel = isset($options['protect']) ? $options['protect'] : null;
185
186 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
187 $restrictions['move'] = $protectLevel;
188 $restrictions['edit'] = $protectLevel;
189 $doProtect = true;
190 }
191 if (isset($options['unprotect'])) {
192 $restrictions['move'] = '';
193 $restrictions['edit'] = '';
194 $doProtect = true;
195 }
196
197
198 if ( isset( $options['dry'] ) ) {
199 echo( "done.\n" );
200 } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
201 # We're done!
202 echo( "done.\n" );
203 if ($doProtect) {
204 # Protect the file
205 $article = new Article( $title );
206 echo "\nWaiting for slaves...\n";
207 // Wait for slaves.
208 sleep(2.0);
209 wfWaitForSlaves( 1.0 );
210
211 echo( "\nSetting image restrictions ... " );
212 if ( $article->updateRestrictions($restrictions) )
213 echo( "done.\n" );
214 else
215 echo( "failed.\n" );
216 }
217
218 } else {
219 echo( "failed.\n" );
220 $svar = 'failed';
221 }
222
223 $$svar++;
224 $processed++;
225
226 if ( $limit && $processed >= $limit )
227 break;
228
229 if ( $sleep )
230 sleep( $sleep );
231 }
232
233 # Print out some statistics
234 echo( "\n" );
235 foreach( array( 'count' => 'Found', 'limit' => 'Limit', 'ignored' => 'Ignored',
236 'added' => 'Added', 'skipped' => 'Skipped', 'overwritten' => 'Overwritten',
237 'failed' => 'Failed' ) as $var => $desc ) {
238 if( $$var > 0 )
239 echo( "{$desc}: {$$var}\n" );
240 }
241
242 } else {
243 echo( "No suitable files could be found for import.\n" );
244 }
245
246 } else {
247 showUsage();
248 }
249
250 exit(0);
251
252 function showUsage( $reason = false ) {
253 if( $reason ) {
254 echo( $reason . "\n" );
255 }
256
257 echo <<<END
258 Imports images and other media files into the wiki
259 USAGE: php importImages.php [options] <dir>
260
261 <dir> : Path to the directory containing images to be imported
262
263 Options:
264 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
265 --overwrite Overwrite existing images with the same name (default is to skip them)
266 --limit=<num> Limit the number of images to process. Ignored or skipped images are not counted.
267 --from=<name> Ignore all files until the one with the given name. Useful for resuming
268 aborted imports. <name> should be the file's canonical database form.
269 --skip-dupes Skip images that were already uploaded under a different name (check SHA1)
270 --sleep=<sec> Sleep between files. Useful mostly for debugging.
271 --user=<username> Set username of uploader, default 'Maintenance script'
272 --check-userblock Check if the user got blocked during import.
273 --comment=<text> Set upload summary comment, default 'Importing image file'.
274 --comment-file=<file> Set upload summary comment the the content of <file>.
275 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
276 but the extension <ext>. If a global comment is also given, it is appended.
277 --license=<code> Use an optional license template
278 --dry Dry run, don't import anything
279 --protect=<protect> Specify the protect value (autoconfirmed,sysop)
280 --unprotect Unprotects all uploaded images
281
282 END;
283 exit(1);
284 }