imageImport --check-userblock checks if the user used for importing was blocked
[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 echo( "Importing {$base}..." );
128 $svar = 'added';
129 }
130
131 # Find comment text
132 $commentText = false;
133
134 if ( $commentExt ) {
135 $f = findAuxFile( $file, $commentExt );
136 if ( !$f ) {
137 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
138 } else {
139 $commentText = file_get_contents( $f );
140 if ( !$f ) {
141 echo( " Failed to load comment file {$f}, using default comment. " );
142 }
143 }
144
145 if ( $commentText && $comment ) {
146 $commentText = trim( $commentText ) . "\n\n" . trim( $comment );
147 }
148 }
149
150 if ( !$commentText ) {
151 $commentText = $comment;
152 }
153
154 # Import the file
155 if ( isset( $options['dry'] ) ) {
156 echo( " publishing {$file}... " );
157 } else {
158 $archive = $image->publish( $file );
159 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
160 echo( "failed.\n" );
161 $failed++;
162 continue;
163 }
164 }
165
166 $doProtect = false;
167 $restrictions = array();
168
169 global $wgRestrictionLevels;
170
171 $protectLevel = isset($options['protect']) ? $options['protect'] : null;
172
173 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
174 $restrictions['move'] = $protectLevel;
175 $restrictions['edit'] = $protectLevel;
176 $doProtect = true;
177 }
178 if (isset($options['unprotect'])) {
179 $restrictions['move'] = '';
180 $restrictions['edit'] = '';
181 $doProtect = true;
182 }
183
184
185 if ( isset( $options['dry'] ) ) {
186 echo( "done.\n" );
187 } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
188 # We're done!
189 echo( "done.\n" );
190 if ($doProtect) {
191 # Protect the file
192 $article = new Article( $title );
193 echo "\nWaiting for slaves...\n";
194 // Wait for slaves.
195 sleep(2.0);
196 wfWaitForSlaves( 1.0 );
197
198 echo( "\nSetting image restrictions ... " );
199 if ( $article->updateRestrictions($restrictions) )
200 echo( "done.\n" );
201 else
202 echo( "failed.\n" );
203 }
204
205 } else {
206 echo( "failed.\n" );
207 $svar = 'failed';
208 }
209
210 $$svar++;
211 $processed++;
212
213 if ( $limit && $processed >= $limit )
214 break;
215
216 if ( $sleep )
217 sleep( $sleep );
218 }
219
220 # Print out some statistics
221 echo( "\n" );
222 foreach( array( 'count' => 'Found', 'limit' => 'Limit', 'ignored' => 'Ignored',
223 'added' => 'Added', 'skipped' => 'Skipped', 'overwritten' => 'Overwritten',
224 'failed' => 'Failed' ) as $var => $desc ) {
225 if( $$var > 0 )
226 echo( "{$desc}: {$$var}\n" );
227 }
228
229 } else {
230 echo( "No suitable files could be found for import.\n" );
231 }
232
233 } else {
234 showUsage();
235 }
236
237 exit(0);
238
239 function showUsage( $reason = false ) {
240 if( $reason ) {
241 echo( $reason . "\n" );
242 }
243
244 echo <<<END
245 Imports images and other media files into the wiki
246 USAGE: php importImages.php [options] <dir>
247
248 <dir> : Path to the directory containing images to be imported
249
250 Options:
251 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
252 --overwrite Overwrite existing images with the same name (default is to skip them)
253 --limit=<num> Limit the number of images to process. Ignored or skipped images are not counted.
254 --from=<name> Ignore all files until the one with the given name. Useful for resuming
255 aborted imports. <name> should be the file's canonical database form.
256 --sleep=<sec> Sleep between files. Useful mostly for debugging.
257 --user=<username> Set username of uploader, default 'Maintenance script'
258 --check-userblock Check if the user got blocked during import.
259 --comment=<text> Set upload summary comment, default 'Importing image file'.
260 --comment-file=<file> Set upload summary comment the the content of <file>.
261 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
262 but the extension <ext>. If a global comment is also given, it is appended.
263 --license=<code> Use an optional license template
264 --dry Dry run, don't import anything
265 --protect=<protect> Specify the protect value (autoconfirmed,sysop)
266 --unprotect Unprotects all uploaded images
267
268 END;
269 exit(1);
270 }