(bug 11857) Add 'protect' option to importImages.php maintenance script.
[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 # 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 ($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 the upload comment
49 $comment = 'Importing image file';
50
51 if ( isset( $options['comment-file'] ) ) {
52 $comment = file_get_contents( $options['comment-file'] );
53 if ( $comment === false || $comment === NULL ) {
54 die( "failed to read comment file: {$options['comment-file']}\n" );
55 }
56 }
57 else if ( isset( $options['comment'] ) ) {
58 $comment = $options['comment'];
59 }
60
61 $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
62
63 # Get the license specifier
64 $license = isset( $options['license'] ) ? $options['license'] : '';
65
66 # Batch "upload" operation
67 if( ( $count = count( $files ) ) > 0 ) {
68
69 foreach( $files as $file ) {
70 $base = wfBaseName( $file );
71
72 # Validate a title
73 $title = Title::makeTitleSafe( NS_FILE, $base );
74 if( !is_object( $title ) ) {
75 echo( "{$base} could not be imported; a valid title cannot be produced\n" );
76 continue;
77 }
78
79 # Check existence
80 $image = wfLocalFile( $title );
81 if( $image->exists() ) {
82 if( isset( $options['overwrite'] ) ) {
83 echo( "{$base} exists, overwriting..." );
84 $svar = 'overwritten';
85 } else {
86 echo( "{$base} exists, skipping\n" );
87 $skipped++;
88 continue;
89 }
90 } else {
91 echo( "Importing {$base}..." );
92 $svar = 'added';
93 }
94
95 # Find comment text
96 $commentText = false;
97
98 if ( $commentExt ) {
99 $f = findAuxFile( $file, $commentExt );
100 if ( !$f ) {
101 echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " );
102 } else {
103 $commentText = file_get_contents( $f );
104 if ( !$f ) {
105 echo( " Failed to load comment file {$f}, using default comment. " );
106 }
107 }
108 }
109
110 if ( !$commentText ) {
111 $commentText = $comment;
112 }
113
114 # Import the file
115 if ( isset( $options['dry'] ) ) {
116 echo( " publishing {$file}... " );
117 } else {
118 $archive = $image->publish( $file );
119 if( WikiError::isError( $archive ) || !$archive->isGood() ) {
120 echo( "failed.\n" );
121 continue;
122 }
123 }
124
125 $doProtect = false;
126 $restrictions = array();
127
128 global $wgRestrictionLevels;
129
130 $protectLevel = isset($options['protect']) ? $options['protect'] : null;
131
132 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
133 $restrictions['move'] = $protectLevel;
134 $restrictions['edit'] = $protectLevel;
135 $doProtect = true;
136 }
137 if (isset($options['unprotect'])) {
138 $restrictions['move'] = '';
139 $restrictions['edit'] = '';
140 $doProtect = true;
141 }
142
143
144 $$svar++;
145 if ( isset( $options['dry'] ) ) {
146 echo( "done.\n" );
147 } else if ( $image->recordUpload( $archive->value, $commentText, $license ) ) {
148 # We're done!
149 echo( "done.\n" );
150 if ($doProtect) {
151 # Protect the file
152 $article = new Article( $title );
153 echo "\nWaiting for slaves...\n";
154 // Wait for slaves.
155 sleep(2.0);
156 wfWaitForSlaves( 1.0 );
157
158 echo( "\nSetting image restrictions ... " );
159 if ( $article->updateRestrictions($restrictions) )
160 echo( "done.\n" );
161 else
162 echo( "failed.\n" );
163 }
164
165 } else {
166 echo( "failed.\n" );
167 }
168
169 }
170
171 # Print out some statistics
172 echo( "\n" );
173 foreach( array( 'count' => 'Found', 'added' => 'Added',
174 'skipped' => 'Skipped', 'overwritten' => 'Overwritten' ) as $var => $desc ) {
175 if( $$var > 0 )
176 echo( "{$desc}: {$$var}\n" );
177 }
178
179 } else {
180 echo( "No suitable files could be found for import.\n" );
181 }
182
183 } else {
184 showUsage();
185 }
186
187 exit();
188
189 function showUsage( $reason = false ) {
190 if( $reason ) {
191 echo( $reason . "\n" );
192 }
193
194 echo <<<END
195 Imports images and other media files into the wiki
196 USAGE: php importImages.php [options] <dir>
197
198 <dir> : Path to the directory containing images to be imported
199
200 Options:
201 --extensions=<exts> Comma-separated list of allowable extensions, defaults to \$wgFileExtensions
202 --overwrite Overwrite existing images if a conflicting-named image is found
203 --user=<username> Set username of uploader, default 'Maintenance script'
204 --comment=<text> Set upload summary comment, default 'Importing image file'
205 --comment-file=<file> Set upload summary comment the the content of <file>.
206 --comment-ext=<ext> Causes the comment for each file to be loaded from a file with the same name
207 but the extension <ext>.
208 --license=<code> Use an optional license template
209 --dry Dry run, don't import anything
210 --protect=<protect> Specify the protect value (autoconfirmed,sysop)
211 --unprotect Unprotects all uploaded images
212
213 END;
214 exit();
215 }