Bump $wgStyleVersion to 155.
[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 $optionsWithArguments = array( 'extensions', 'overwrite' );
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 = isset( $options['comment'] )
43 ? $options['comment']
44 : 'Importing image file';
45
46 # Get the license specifier
47 $license = isset( $options['license'] ) ? $options['license'] : '';
48
49 # Batch "upload" operation
50 if( ( $count = count( $files ) ) > 0 ) {
51
52 foreach( $files as $file ) {
53 $base = wfBaseName( $file );
54
55 # Validate a title
56 $title = Title::makeTitleSafe( NS_IMAGE, $base );
57 if( !is_object( $title ) ) {
58 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
59 continue;
60 }
61
62 # Check existence
63 $image = wfLocalFile( $title );
64 if( $image->exists() ) {
65 if( isset( $options['overwrite'] ) ) {
66 echo( "{$base} exists, overwriting..." );
67 $svar = 'overwritten';
68 } else {
69 echo( "{$base} exists, skipping\n" );
70 $skipped++;
71 continue;
72 }
73 } else {
74 echo( "Importing {$base}..." );
75 $svar = 'added';
76 }
77
78 # Import the file
79 $archive = $image->publish( $file );
80 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
81 echo( "failed.\n" );
82 continue;
83 }
84
85 $$svar++;
86 if ( $image->recordUpload( $archive->value, $comment, $license ) ) {
87 # We're done!
88 echo( "done.\n" );
89 } else {
90 echo( "failed.\n" );
91 }
92
93 }
94
95 # Print out some statistics
96 echo( "\n" );
97 foreach( array( 'count' => 'Found', 'added' => 'Added',
98 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
99 if( $$var > 0 )
100 echo( "{$desc}: {$$var}\n" );
101 }
102
103 } else {
104 echo( "No suitable files could be found for import.\n" );
105 }
106
107 } else {
108 showUsage();
109 }
110
111 exit();
112
113 function showUsage( $reason = false ) {
114 if( $reason ) {
115 echo( $reason . "\n" );
116 }
117
118 echo <<<END
119 Imports images and other media files into the wiki
120 USAGE: php importImages.php [options] <dir>
121
122 <dir> : Path to the directory containing images to be imported
123
124 Options:
125 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
126 --overwrite Overwrite existing images if a conflicting-named image is found
127 --user=<username> Set username of uploader, default 'Maintenance script'
128 --comment=<text> Set upload summary comment, default 'Importing image file'
129 --license=<code> Use an optional license template
130
131 END;
132 exit();
133 }