Merge "Stash WatchedItem changes so that the jobs run from the queue"
[lhc/web/wiklou.git] / maintenance / importImages.php
1 <?php
2 /**
3 * Import one or more images from the local file system into the wiki without
4 * using the web-based interface.
5 *
6 * "Smart import" additions:
7 * - aim: preserve the essential metadata (user, description) when importing media
8 * files from an existing wiki.
9 * - process:
10 * - interface with the source wiki, don't use bare files only (see --source-wiki-url).
11 * - fetch metadata from source wiki for each file to import.
12 * - commit the fetched metadata to the destination wiki while submitting.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * http://www.gnu.org/copyleft/gpl.html
28 *
29 * @file
30 * @ingroup Maintenance
31 * @author Rob Church <robchur@gmail.com>
32 * @author Mij <mij@bitchx.it>
33 */
34
35 require_once __DIR__ . '/Maintenance.php';
36
37 class ImportImages extends Maintenance {
38
39 public function __construct() {
40 parent::__construct();
41
42 $this->addDescription( 'Imports images and other media files into the wiki' );
43 $this->addArg( 'dir', 'Path to the directory containing images to be imported' );
44
45 $this->addOption( 'extensions',
46 'Comma-separated list of allowable extensions, defaults to $wgFileExtensions',
47 false,
48 true
49 );
50 $this->addOption( 'overwrite',
51 'Overwrite existing images with the same name (default is to skip them)' );
52 $this->addOption( 'limit',
53 'Limit the number of images to process. Ignored or skipped images are not counted',
54 false,
55 true
56 );
57 $this->addOption( 'from',
58 "Ignore all files until the one with the given name. Useful for resuming aborted "
59 . "imports. The name should be the file's canonical database form.",
60 false,
61 true
62 );
63 $this->addOption( 'skip-dupes',
64 'Skip images that were already uploaded under a different name (check SHA1)' );
65 $this->addOption( 'search-recursively', 'Search recursively for files in subdirectories' );
66 $this->addOption( 'sleep',
67 'Sleep between files. Useful mostly for debugging',
68 false,
69 true
70 );
71 $this->addOption( 'user',
72 "Set username of uploader, default 'Maintenance script'",
73 false,
74 true
75 );
76 // This parameter can optionally have an argument. If none specified, getOption()
77 // returns 1 which is precisely what we need.
78 $this->addOption( 'check-userblock', 'Check if the user got blocked during import' );
79 $this->addOption( 'comment',
80 "Set file description, default 'Importing file'",
81 false,
82 true
83 );
84 $this->addOption( 'comment-file',
85 'Set description to the content of this file',
86 false,
87 true
88 );
89 $this->addOption( 'comment-ext',
90 'Causes the description for each file to be loaded from a file with the same name, but '
91 . 'the extension provided. If a global description is also given, it is appended.',
92 false,
93 true
94 );
95 $this->addOption( 'summary',
96 'Upload summary, description will be used if not provided',
97 false,
98 true
99 );
100 $this->addOption( 'license',
101 'Use an optional license template',
102 false,
103 true
104 );
105 $this->addOption( 'timestamp',
106 'Override upload time/date, all MediaWiki timestamp formats are accepted',
107 false,
108 true
109 );
110 $this->addOption( 'protect',
111 'Specify the protect value (autoconfirmed,sysop)',
112 false,
113 true
114 );
115 $this->addOption( 'unprotect', 'Unprotects all uploaded images' );
116 $this->addOption( 'source-wiki-url',
117 'If specified, take User and Comment data for each imported file from this URL. '
118 . 'For example, --source-wiki-url="http://en.wikipedia.org/',
119 false,
120 true
121 );
122 $this->addOption( 'dry', "Dry run, don't import anything" );
123 }
124
125 public function execute() {
126 global $wgFileExtensions, $wgUser, $wgRestrictionLevels;
127
128 $processed = $added = $ignored = $skipped = $overwritten = $failed = 0;
129
130 $this->output( "Import Images\n\n" );
131
132 $dir = $this->getArg( 0 );
133
134 # Check Protection
135 if ( $this->hasOption( 'protect' ) && $this->hasOption( 'unprotect' ) ) {
136 $this->fatalError( "Cannot specify both protect and unprotect. Only 1 is allowed.\n" );
137 }
138
139 if ( $this->hasOption( 'protect' ) && trim( $this->getOption( 'protect' ) ) ) {
140 $this->fatalError( "You must specify a protection option.\n" );
141 }
142
143 # Prepare the list of allowed extensions
144 $extensions = $this->hasOption( 'extensions' )
145 ? explode( ',', strtolower( $this->getOption( 'extensions' ) ) )
146 : $wgFileExtensions;
147
148 # Search the path provided for candidates for import
149 $files = $this->findFiles( $dir, $extensions, $this->hasOption( 'search-recursively' ) );
150
151 # Initialise the user for this operation
152 $user = $this->hasOption( 'user' )
153 ? User::newFromName( $this->getOption( 'user' ) )
154 : User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
155 if ( !$user instanceof User ) {
156 $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
157 }
158 $wgUser = $user;
159
160 # Get block check. If a value is given, this specified how often the check is performed
161 $checkUserBlock = (int)$this->getOption( 'check-userblock' );
162
163 $from = $this->getOption( 'from' );
164 $sleep = (int)$this->getOption( 'sleep' );
165 $limit = (int)$this->getOption( 'limit' );
166 $timestamp = $this->getOption( 'timestamp', false );
167
168 # Get the upload comment. Provide a default one in case there's no comment given.
169 $commentFile = $this->getOption( 'comment-file' );
170 if ( $commentFile !== null ) {
171 $comment = file_get_contents( $commentFile );
172 if ( $comment === false || $comment === null ) {
173 $this->fatalError( "failed to read comment file: {$commentFile}\n" );
174 }
175 } else {
176 $comment = $this->getOption( 'comment', 'Importing file' );
177 }
178 $commentExt = $this->getOption( 'comment-ext' );
179 $summary = $this->getOption( 'summary', '' );
180
181 $license = $this->getOption( 'license', '' );
182
183 $sourceWikiUrl = $this->getOption( 'source-wiki-url' );
184
185 # Batch "upload" operation
186 $count = count( $files );
187 if ( $count > 0 ) {
188 foreach ( $files as $file ) {
189 if ( $sleep && ( $processed > 0 ) ) {
190 sleep( $sleep );
191 }
192
193 $base = UtfNormal\Validator::cleanUp( wfBaseName( $file ) );
194
195 # Validate a title
196 $title = Title::makeTitleSafe( NS_FILE, $base );
197 if ( !is_object( $title ) ) {
198 $this->output(
199 "{$base} could not be imported; a valid title cannot be produced\n" );
200 continue;
201 }
202
203 if ( $from ) {
204 if ( $from == $title->getDBkey() ) {
205 $from = null;
206 } else {
207 $ignored++;
208 continue;
209 }
210 }
211
212 if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
213 $user->clearInstanceCache( 'name' ); // reload from DB!
214 if ( $user->isBlocked() ) {
215 $this->output( $user->getName() . " was blocked! Aborting.\n" );
216 break;
217 }
218 }
219
220 # Check existence
221 $image = wfLocalFile( $title );
222 if ( $image->exists() ) {
223 if ( $this->hasOption( 'overwrite' ) ) {
224 $this->output( "{$base} exists, overwriting..." );
225 $svar = 'overwritten';
226 } else {
227 $this->output( "{$base} exists, skipping\n" );
228 $skipped++;
229 continue;
230 }
231 } else {
232 if ( $this->hasOption( 'skip-dupes' ) ) {
233 $repo = $image->getRepo();
234 # XXX: we end up calculating this again when actually uploading. that sucks.
235 $sha1 = FSFile::getSha1Base36FromPath( $file );
236
237 $dupes = $repo->findBySha1( $sha1 );
238
239 if ( $dupes ) {
240 $this->output(
241 "{$base} already exists as {$dupes[0]->getName()}, skipping\n" );
242 $skipped++;
243 continue;
244 }
245 }
246
247 $this->output( "Importing {$base}..." );
248 $svar = 'added';
249 }
250
251 if ( $sourceWikiUrl ) {
252 /* find comment text directly from source wiki, through MW's API */
253 $real_comment = $this->getFileCommentFromSourceWiki( $sourceWikiUrl, $base );
254 if ( $real_comment === false ) {
255 $commentText = $comment;
256 } else {
257 $commentText = $real_comment;
258 }
259
260 /* find user directly from source wiki, through MW's API */
261 $real_user = $this->getFileUserFromSourceWiki( $sourceWikiUrl, $base );
262 if ( $real_user === false ) {
263 $wgUser = $user;
264 } else {
265 $wgUser = User::newFromName( $real_user );
266 if ( $wgUser === false ) {
267 # user does not exist in target wiki
268 $this->output(
269 "failed: user '$real_user' does not exist in target wiki." );
270 continue;
271 }
272 }
273 } else {
274 # Find comment text
275 $commentText = false;
276
277 if ( $commentExt ) {
278 $f = $this->findAuxFile( $file, $commentExt );
279 if ( !$f ) {
280 $this->output( " No comment file with extension {$commentExt} found "
281 . "for {$file}, using default comment. " );
282 } else {
283 $commentText = file_get_contents( $f );
284 if ( !$commentText ) {
285 $this->output(
286 " Failed to load comment file {$f}, using default comment. " );
287 }
288 }
289 }
290
291 if ( !$commentText ) {
292 $commentText = $comment;
293 }
294 }
295
296 # Import the file
297 if ( $this->hasOption( 'dry' ) ) {
298 $this->output(
299 " publishing {$file} by '{$wgUser->getName()}', comment '$commentText'... "
300 );
301 } else {
302 $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
303 $props = $mwProps->getPropsFromPath( $file, true );
304 $flags = 0;
305 $publishOptions = [];
306 $handler = MediaHandler::getHandler( $props['mime'] );
307 if ( $handler ) {
308 $metadata = Wikimedia\quietCall( 'unserialize', $props['metadata'] );
309
310 $publishOptions['headers'] = $handler->getContentHeaders( $metadata );
311 } else {
312 $publishOptions['headers'] = [];
313 }
314 $archive = $image->publish( $file, $flags, $publishOptions );
315 if ( !$archive->isGood() ) {
316 $this->output( "failed. (" .
317 $archive->getWikiText( false, false, 'en' ) .
318 ")\n" );
319 $failed++;
320 continue;
321 }
322 }
323
324 $commentText = SpecialUpload::getInitialPageText( $commentText, $license );
325 if ( !$this->hasOption( 'summary' ) ) {
326 $summary = $commentText;
327 }
328
329 if ( $this->hasOption( 'dry' ) ) {
330 $this->output( "done.\n" );
331 } elseif ( $image->recordUpload2(
332 $archive->value,
333 $summary,
334 $commentText,
335 $props,
336 $timestamp
337 )->isOK() ) {
338 $this->output( "done.\n" );
339
340 $doProtect = false;
341
342 $protectLevel = $this->getOption( 'protect' );
343
344 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
345 $doProtect = true;
346 }
347 if ( $this->hasOption( 'unprotect' ) ) {
348 $protectLevel = '';
349 $doProtect = true;
350 }
351
352 if ( $doProtect ) {
353 # Protect the file
354 $this->output( "\nWaiting for replica DBs...\n" );
355 // Wait for replica DBs.
356 sleep( 2.0 ); # Why this sleep?
357 wfWaitForSlaves();
358
359 $this->output( "\nSetting image restrictions ... " );
360
361 $cascade = false;
362 $restrictions = [];
363 foreach ( $title->getRestrictionTypes() as $type ) {
364 $restrictions[$type] = $protectLevel;
365 }
366
367 $page = WikiPage::factory( $title );
368 $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, '', $user );
369 $this->output( ( $status->isOK() ? 'done' : 'failed' ) . "\n" );
370 }
371 } else {
372 $this->output( "failed. (at recordUpload stage)\n" );
373 $svar = 'failed';
374 }
375
376 $$svar++;
377 $processed++;
378
379 if ( $limit && $processed >= $limit ) {
380 break;
381 }
382 }
383
384 # Print out some statistics
385 $this->output( "\n" );
386 foreach (
387 [
388 'count' => 'Found',
389 'limit' => 'Limit',
390 'ignored' => 'Ignored',
391 'added' => 'Added',
392 'skipped' => 'Skipped',
393 'overwritten' => 'Overwritten',
394 'failed' => 'Failed'
395 ] as $var => $desc
396 ) {
397 if ( $$var > 0 ) {
398 $this->output( "{$desc}: {$$var}\n" );
399 }
400 }
401 } else {
402 $this->output( "No suitable files could be found for import.\n" );
403 }
404 }
405
406 /**
407 * Search a directory for files with one of a set of extensions
408 *
409 * @param string $dir Path to directory to search
410 * @param array $exts Array of extensions to search for
411 * @param bool $recurse Search subdirectories recursively
412 * @return array|bool Array of filenames on success, or false on failure
413 */
414 private function findFiles( $dir, $exts, $recurse = false ) {
415 if ( is_dir( $dir ) ) {
416 $dhl = opendir( $dir );
417 if ( $dhl ) {
418 $files = [];
419 while ( ( $file = readdir( $dhl ) ) !== false ) {
420 if ( is_file( $dir . '/' . $file ) ) {
421 $ext = pathinfo( $file, PATHINFO_EXTENSION );
422 if ( array_search( strtolower( $ext ), $exts ) !== false ) {
423 $files[] = $dir . '/' . $file;
424 }
425 } elseif ( $recurse && is_dir( $dir . '/' . $file ) && $file !== '..' && $file !== '.' ) {
426 $files = array_merge( $files, $this->findFiles( $dir . '/' . $file, $exts, true ) );
427 }
428 }
429
430 return $files;
431 } else {
432 return [];
433 }
434 } else {
435 return [];
436 }
437 }
438
439 /**
440 * Find an auxilliary file with the given extension, matching
441 * the give base file path. $maxStrip determines how many extensions
442 * may be stripped from the original file name before appending the
443 * new extension. For example, with $maxStrip = 1 (the default),
444 * file files acme.foo.bar.txt and acme.foo.txt would be auxilliary
445 * files for acme.foo.bar and the extension ".txt". With $maxStrip = 2,
446 * acme.txt would also be acceptable.
447 *
448 * @param string $file Base path
449 * @param string $auxExtension The extension to be appended to the base path
450 * @param int $maxStrip The maximum number of extensions to strip from the base path (default: 1)
451 * @return string|bool
452 */
453 private function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
454 if ( strpos( $auxExtension, '.' ) !== 0 ) {
455 $auxExtension = '.' . $auxExtension;
456 }
457
458 $d = dirname( $file );
459 $n = basename( $file );
460
461 while ( $maxStrip >= 0 ) {
462 $f = $d . '/' . $n . $auxExtension;
463
464 if ( file_exists( $f ) ) {
465 return $f;
466 }
467
468 $idx = strrpos( $n, '.' );
469 if ( !$idx ) {
470 break;
471 }
472
473 $n = substr( $n, 0, $idx );
474 $maxStrip -= 1;
475 }
476
477 return false;
478 }
479
480 # @todo FIXME: Access the api in a saner way and performing just one query
481 # (preferably batching files too).
482 private function getFileCommentFromSourceWiki( $wiki_host, $file ) {
483 $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:'
484 . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment';
485 $body = Http::get( $url, [], __METHOD__ );
486 if ( preg_match( '#<ii comment="([^"]*)" />#', $body, $matches ) == 0 ) {
487 return false;
488 }
489
490 return html_entity_decode( $matches[1] );
491 }
492
493 private function getFileUserFromSourceWiki( $wiki_host, $file ) {
494 $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:'
495 . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user';
496 $body = Http::get( $url, [], __METHOD__ );
497 if ( preg_match( '#<ii user="([^"]*)" />#', $body, $matches ) == 0 ) {
498 return false;
499 }
500
501 return html_entity_decode( $matches[1] );
502 }
503
504 }
505
506 $maintClass = ImportImages::class;
507 require_once RUN_MAINTENANCE_IF_MAIN;