Index: trunk/phase3/maintenance/importImages.php |
— | — | @@ -40,255 +40,265 @@ |
41 | 41 | echo( "Import Images\n\n" ); |
42 | 42 | |
43 | 43 | # Need a path |
44 | | -if ( count( $args ) > 0 ) { |
| 44 | +if ( count( $args ) == 0 ) { |
| 45 | + showUsage(); |
| 46 | +} |
45 | 47 | |
46 | | - $dir = $args[0]; |
| 48 | +$dir = $args[0]; |
47 | 49 | |
48 | | - # Check Protection |
49 | | - if ( isset( $options['protect'] ) && isset( $options['unprotect'] ) ) |
50 | | - die( "Cannot specify both protect and unprotect. Only 1 is allowed.\n" ); |
| 50 | +# Check Protection |
| 51 | +if ( isset( $options['protect'] ) && isset( $options['unprotect'] ) ) { |
| 52 | + die( "Cannot specify both protect and unprotect. Only 1 is allowed.\n" ); |
| 53 | +} |
51 | 54 | |
52 | | - if ( isset( $options['protect'] ) && $options['protect'] == 1 ) |
53 | | - die( "You must specify a protection option.\n" ); |
| 55 | +if ( isset( $options['protect'] ) && $options['protect'] == 1 ) { |
| 56 | + die( "You must specify a protection option.\n" ); |
| 57 | +} |
54 | 58 | |
55 | | - # Prepare the list of allowed extensions |
56 | | - global $wgFileExtensions; |
57 | | - $extensions = isset( $options['extensions'] ) |
58 | | - ? explode( ',', strtolower( $options['extensions'] ) ) |
59 | | - : $wgFileExtensions; |
| 59 | +# Prepare the list of allowed extensions |
| 60 | +global $wgFileExtensions; |
| 61 | +$extensions = isset( $options['extensions'] ) |
| 62 | + ? explode( ',', strtolower( $options['extensions'] ) ) |
| 63 | + : $wgFileExtensions; |
60 | 64 | |
61 | | - # Search the path provided for candidates for import |
62 | | - $files = findFiles( $dir, $extensions ); |
| 65 | +# Search the path provided for candidates for import |
| 66 | +$files = findFiles( $dir, $extensions ); |
63 | 67 | |
64 | | - # Initialise the user for this operation |
65 | | - $user = isset( $options['user'] ) |
66 | | - ? User::newFromName( $options['user'] ) |
67 | | - : User::newFromName( 'Maintenance script' ); |
68 | | - if ( !$user instanceof User ) |
69 | | - $user = User::newFromName( 'Maintenance script' ); |
70 | | - $wgUser = $user; |
| 68 | +# Initialise the user for this operation |
| 69 | +$user = isset( $options['user'] ) |
| 70 | + ? User::newFromName( $options['user'] ) |
| 71 | + : User::newFromName( 'Maintenance script' ); |
| 72 | +if ( !$user instanceof User ) { |
| 73 | + $user = User::newFromName( 'Maintenance script' ); |
| 74 | +} |
| 75 | +$wgUser = $user; |
71 | 76 | |
72 | | - # Get block check. If a value is given, this specified how often the check is performed |
73 | | - if ( isset( $options['check-userblock'] ) ) { |
74 | | - if ( !$options['check-userblock'] ) $checkUserBlock = 1; |
75 | | - else $checkUserBlock = (int)$options['check-userblock']; |
| 77 | +# Get block check. If a value is given, this specified how often the check is performed |
| 78 | +if ( isset( $options['check-userblock'] ) ) { |
| 79 | + if ( !$options['check-userblock'] ) { |
| 80 | + $checkUserBlock = 1; |
76 | 81 | } else { |
77 | | - $checkUserBlock = false; |
| 82 | + $checkUserBlock = (int)$options['check-userblock']; |
78 | 83 | } |
| 84 | +} else { |
| 85 | + $checkUserBlock = false; |
| 86 | +} |
79 | 87 | |
80 | | - # Get --from |
81 | | - $from = @$options['from']; |
| 88 | +# Get --from |
| 89 | +$from = @$options['from']; |
82 | 90 | |
83 | | - # Get sleep time. |
84 | | - $sleep = @$options['sleep']; |
85 | | - if ( $sleep ) $sleep = (int)$sleep; |
| 91 | +# Get sleep time. |
| 92 | +$sleep = @$options['sleep']; |
| 93 | +if ( $sleep ) { |
| 94 | + $sleep = (int)$sleep; |
| 95 | +} |
86 | 96 | |
87 | | - # Get limit number |
88 | | - $limit = @$options['limit']; |
89 | | - if ( $limit ) $limit = (int)$limit; |
| 97 | +# Get limit number |
| 98 | +$limit = @$options['limit']; |
| 99 | +if ( $limit ) { |
| 100 | + $limit = (int)$limit; |
| 101 | +} |
90 | 102 | |
91 | | - # Get the upload comment. Provide a default one in case there's no comment given. |
92 | | - $comment = 'Importing image file'; |
| 103 | +# Get the upload comment. Provide a default one in case there's no comment given. |
| 104 | +$comment = 'Importing image file'; |
93 | 105 | |
94 | | - if ( isset( $options['comment-file'] ) ) { |
95 | | - $comment = file_get_contents( $options['comment-file'] ); |
96 | | - if ( $comment === false || $comment === NULL ) { |
97 | | - die( "failed to read comment file: {$options['comment-file']}\n" ); |
98 | | - } |
| 106 | +if ( isset( $options['comment-file'] ) ) { |
| 107 | + $comment = file_get_contents( $options['comment-file'] ); |
| 108 | + if ( $comment === false || $comment === null ) { |
| 109 | + die( "failed to read comment file: {$options['comment-file']}\n" ); |
99 | 110 | } |
100 | | - elseif ( isset( $options['comment'] ) ) { |
101 | | - $comment = $options['comment']; |
102 | | - } |
| 111 | +} elseif ( isset( $options['comment'] ) ) { |
| 112 | + $comment = $options['comment']; |
| 113 | +} |
103 | 114 | |
104 | | - $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false; |
| 115 | +$commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false; |
105 | 116 | |
106 | | - # Get the license specifier |
107 | | - $license = isset( $options['license'] ) ? $options['license'] : ''; |
| 117 | +# Get the license specifier |
| 118 | +$license = isset( $options['license'] ) ? $options['license'] : ''; |
108 | 119 | |
109 | | - # Batch "upload" operation |
110 | | - if ( ( $count = count( $files ) ) > 0 ) { |
| 120 | +# Batch "upload" operation |
| 121 | +$count = count( $files ); |
| 122 | +if ( $count > 0 ) { |
111 | 123 | |
112 | | - foreach ( $files as $file ) { |
113 | | - $base = wfBaseName( $file ); |
| 124 | + foreach ( $files as $file ) { |
| 125 | + $base = wfBaseName( $file ); |
114 | 126 | |
115 | | - # Validate a title |
116 | | - $title = Title::makeTitleSafe( NS_FILE, $base ); |
117 | | - if ( !is_object( $title ) ) { |
118 | | - echo( "{$base} could not be imported; a valid title cannot be produced\n" ); |
| 127 | + # Validate a title |
| 128 | + $title = Title::makeTitleSafe( NS_FILE, $base ); |
| 129 | + if ( !is_object( $title ) ) { |
| 130 | + echo( "{$base} could not be imported; a valid title cannot be produced\n" ); |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + if ( $from ) { |
| 135 | + if ( $from == $title->getDBkey() ) { |
| 136 | + $from = null; |
| 137 | + } else { |
| 138 | + $ignored++; |
119 | 139 | continue; |
120 | 140 | } |
| 141 | + } |
121 | 142 | |
122 | | - if ( $from ) { |
123 | | - if ( $from == $title->getDBkey() ) { |
124 | | - $from = NULL; |
125 | | - } else { |
126 | | - $ignored++; |
127 | | - continue; |
128 | | - } |
| 143 | + if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) { |
| 144 | + $user->clearInstanceCache( 'name' ); // reload from DB! |
| 145 | + if ( $user->isBlocked() ) { |
| 146 | + echo( $user->getName() . " was blocked! Aborting.\n" ); |
| 147 | + break; |
129 | 148 | } |
| 149 | + } |
130 | 150 | |
131 | | - if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) { |
132 | | - $user->clearInstanceCache( 'name' ); // reload from DB! |
133 | | - if ( $user->isBlocked() ) { |
134 | | - echo( $user->getName() . " was blocked! Aborting.\n" ); |
135 | | - break; |
136 | | - } |
| 151 | + # Check existence |
| 152 | + $image = wfLocalFile( $title ); |
| 153 | + if ( $image->exists() ) { |
| 154 | + if ( isset( $options['overwrite'] ) ) { |
| 155 | + echo( "{$base} exists, overwriting..." ); |
| 156 | + $svar = 'overwritten'; |
| 157 | + } else { |
| 158 | + echo( "{$base} exists, skipping\n" ); |
| 159 | + $skipped++; |
| 160 | + continue; |
137 | 161 | } |
| 162 | + } else { |
| 163 | + if ( isset( $options['skip-dupes'] ) ) { |
| 164 | + $repo = $image->getRepo(); |
| 165 | + $sha1 = File::sha1Base36( $file ); # XXX: we end up calculating this again when actually uploading. that sucks. |
138 | 166 | |
139 | | - # Check existence |
140 | | - $image = wfLocalFile( $title ); |
141 | | - if ( $image->exists() ) { |
142 | | - if ( isset( $options['overwrite'] ) ) { |
143 | | - echo( "{$base} exists, overwriting..." ); |
144 | | - $svar = 'overwritten'; |
145 | | - } else { |
146 | | - echo( "{$base} exists, skipping\n" ); |
| 167 | + $dupes = $repo->findBySha1( $sha1 ); |
| 168 | + |
| 169 | + if ( $dupes ) { |
| 170 | + echo( "{$base} already exists as " . $dupes[0]->getName() . ", skipping\n" ); |
147 | 171 | $skipped++; |
148 | 172 | continue; |
149 | 173 | } |
150 | | - } else { |
151 | | - if ( isset( $options['skip-dupes'] ) ) { |
152 | | - $repo = $image->getRepo(); |
153 | | - $sha1 = File::sha1Base36( $file ); # XXX: we end up calculating this again when actually uploading. that sucks. |
| 174 | + } |
154 | 175 | |
155 | | - $dupes = $repo->findBySha1( $sha1 ); |
| 176 | + echo( "Importing {$base}..." ); |
| 177 | + $svar = 'added'; |
| 178 | + } |
156 | 179 | |
157 | | - if ( $dupes ) { |
158 | | - echo( "{$base} already exists as " . $dupes[0]->getName() . ", skipping\n" ); |
159 | | - $skipped++; |
160 | | - continue; |
161 | | - } |
| 180 | + if ( isset( $options['source-wiki-url'] ) ) { |
| 181 | + /* find comment text directly from source wiki, through MW's API */ |
| 182 | + $real_comment = getFileCommentFromSourceWiki( $options['source-wiki-url'], $base ); |
| 183 | + if ( $real_comment === false ) |
| 184 | + $commentText = $comment; |
| 185 | + else |
| 186 | + $commentText = $real_comment; |
| 187 | + |
| 188 | + /* find user directly from source wiki, through MW's API */ |
| 189 | + $real_user = getFileUserFromSourceWiki( $options['source-wiki-url'], $base ); |
| 190 | + if ( $real_user === false ) { |
| 191 | + $wgUser = $user; |
| 192 | + } else { |
| 193 | + $wgUser = User::newFromName( $real_user ); |
| 194 | + if ( $wgUser === false ) { |
| 195 | + # user does not exist in target wiki |
| 196 | + echo ( "failed: user '$real_user' does not exist in target wiki." ); |
| 197 | + continue; |
162 | 198 | } |
163 | | - |
164 | | - echo( "Importing {$base}..." ); |
165 | | - $svar = 'added'; |
166 | 199 | } |
| 200 | + } else { |
| 201 | + # Find comment text |
| 202 | + $commentText = false; |
167 | 203 | |
168 | | - if ( isset( $options['source-wiki-url'] ) ) { |
169 | | - /* find comment text directly from source wiki, through MW's API */ |
170 | | - $real_comment = getFileCommentFromSourceWiki( $options['source-wiki-url'], $base ); |
171 | | - if ( $real_comment === false ) |
172 | | - $commentText = $comment; |
173 | | - else |
174 | | - $commentText = $real_comment; |
175 | | - |
176 | | - /* find user directly from source wiki, through MW's API */ |
177 | | - $real_user = getFileUserFromSourceWiki( $options['source-wiki-url'], $base ); |
178 | | - if ( $real_user === false ) { |
179 | | - $wgUser = $user; |
| 204 | + if ( $commentExt ) { |
| 205 | + $f = findAuxFile( $file, $commentExt ); |
| 206 | + if ( !$f ) { |
| 207 | + echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " ); |
180 | 208 | } else { |
181 | | - $wgUser = User::newFromName( $real_user ); |
182 | | - if ( $wgUser === false ) { |
183 | | - # user does not exist in target wiki |
184 | | - echo ( "failed: user '$real_user' does not exist in target wiki." ); |
185 | | - continue; |
186 | | - } |
187 | | - } |
188 | | - } else { |
189 | | - # Find comment text |
190 | | - $commentText = false; |
191 | | - |
192 | | - if ( $commentExt ) { |
193 | | - $f = findAuxFile( $file, $commentExt ); |
| 209 | + $commentText = file_get_contents( $f ); |
194 | 210 | if ( !$f ) { |
195 | | - echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " ); |
196 | | - } else { |
197 | | - $commentText = file_get_contents( $f ); |
198 | | - if ( !$f ) { |
199 | | - echo( " Failed to load comment file {$f}, using default comment. " ); |
200 | | - } |
| 211 | + echo( " Failed to load comment file {$f}, using default comment. " ); |
201 | 212 | } |
202 | 213 | } |
| 214 | + } |
203 | 215 | |
204 | | - if ( !$commentText ) { |
205 | | - $commentText = $comment; |
206 | | - } |
| 216 | + if ( !$commentText ) { |
| 217 | + $commentText = $comment; |
207 | 218 | } |
| 219 | + } |
208 | 220 | |
209 | | - |
210 | | - # Import the file |
211 | | - if ( isset( $options['dry'] ) ) { |
212 | | - echo( " publishing {$file} by '" . $wgUser->getName() . "', comment '$commentText'... " ); |
213 | | - } else { |
214 | | - $archive = $image->publish( $file ); |
215 | | - if ( !$archive->isGood() ) { |
216 | | - echo( "failed. (" . |
217 | | - $archive->getWikiText() . |
218 | | - ")\n" ); |
219 | | - $failed++; |
220 | | - continue; |
221 | | - } |
| 221 | + # Import the file |
| 222 | + if ( isset( $options['dry'] ) ) { |
| 223 | + echo( " publishing {$file} by '" . $wgUser->getName() . "', comment '$commentText'... " ); |
| 224 | + } else { |
| 225 | + $archive = $image->publish( $file ); |
| 226 | + if ( !$archive->isGood() ) { |
| 227 | + echo( "failed. (" . |
| 228 | + $archive->getWikiText() . |
| 229 | + ")\n" ); |
| 230 | + $failed++; |
| 231 | + continue; |
222 | 232 | } |
| 233 | + } |
223 | 234 | |
224 | | - $doProtect = false; |
225 | | - $restrictions = array(); |
| 235 | + $doProtect = false; |
| 236 | + $restrictions = array(); |
226 | 237 | |
227 | | - global $wgRestrictionLevels; |
| 238 | + global $wgRestrictionLevels; |
228 | 239 | |
229 | | - $protectLevel = isset( $options['protect'] ) ? $options['protect'] : null; |
| 240 | + $protectLevel = isset( $options['protect'] ) ? $options['protect'] : null; |
230 | 241 | |
231 | | - if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) { |
232 | | - $restrictions['move'] = $protectLevel; |
233 | | - $restrictions['edit'] = $protectLevel; |
234 | | - $doProtect = true; |
235 | | - } |
236 | | - if ( isset( $options['unprotect'] ) ) { |
237 | | - $restrictions['move'] = ''; |
238 | | - $restrictions['edit'] = ''; |
239 | | - $doProtect = true; |
240 | | - } |
| 242 | + if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) { |
| 243 | + $restrictions['move'] = $protectLevel; |
| 244 | + $restrictions['edit'] = $protectLevel; |
| 245 | + $doProtect = true; |
| 246 | + } |
| 247 | + if ( isset( $options['unprotect'] ) ) { |
| 248 | + $restrictions['move'] = ''; |
| 249 | + $restrictions['edit'] = ''; |
| 250 | + $doProtect = true; |
| 251 | + } |
241 | 252 | |
242 | 253 | |
243 | | - if ( isset( $options['dry'] ) ) { |
244 | | - echo( "done.\n" ); |
245 | | - } elseif ( $image->recordUpload( $archive->value, $commentText, $license ) ) { |
246 | | - # We're done! |
247 | | - echo( "done.\n" ); |
248 | | - if ( $doProtect ) { |
249 | | - # Protect the file |
250 | | - $article = new Article( $title ); |
251 | | - echo "\nWaiting for slaves...\n"; |
252 | | - // Wait for slaves. |
253 | | - sleep( 2.0 ); # Why this sleep? |
254 | | - wfWaitForSlaves(); |
| 254 | + if ( isset( $options['dry'] ) ) { |
| 255 | + echo( "done.\n" ); |
| 256 | + } elseif ( $image->recordUpload( $archive->value, $commentText, $license ) ) { |
| 257 | + # We're done! |
| 258 | + echo( "done.\n" ); |
| 259 | + if ( $doProtect ) { |
| 260 | + # Protect the file |
| 261 | + $article = new Article( $title ); |
| 262 | + echo "\nWaiting for slaves...\n"; |
| 263 | + // Wait for slaves. |
| 264 | + sleep( 2.0 ); # Why this sleep? |
| 265 | + wfWaitForSlaves(); |
255 | 266 | |
256 | | - echo( "\nSetting image restrictions ... " ); |
257 | | - if ( $article->updateRestrictions( $restrictions ) ) |
258 | | - echo( "done.\n" ); |
259 | | - else |
260 | | - echo( "failed.\n" ); |
261 | | - } |
262 | | - |
263 | | - } else { |
264 | | - echo( "failed. (at recordUpload stage)\n" ); |
265 | | - $svar = 'failed'; |
| 267 | + echo( "\nSetting image restrictions ... " ); |
| 268 | + if ( $article->updateRestrictions( $restrictions ) ) { |
| 269 | + echo( "done.\n" ); |
| 270 | + } else { |
| 271 | + echo( "failed.\n" ); |
| 272 | + } |
266 | 273 | } |
267 | 274 | |
268 | | - $$svar++; |
269 | | - $processed++; |
| 275 | + } else { |
| 276 | + echo( "failed. (at recordUpload stage)\n" ); |
| 277 | + $svar = 'failed'; |
| 278 | + } |
270 | 279 | |
271 | | - if ( $limit && $processed >= $limit ) |
272 | | - break; |
| 280 | + $$svar++; |
| 281 | + $processed++; |
273 | 282 | |
274 | | - if ( $sleep ) |
275 | | - sleep( $sleep ); |
| 283 | + if ( $limit && $processed >= $limit ) { |
| 284 | + break; |
276 | 285 | } |
277 | 286 | |
278 | | - # Print out some statistics |
279 | | - echo( "\n" ); |
280 | | - foreach ( array( 'count' => 'Found', 'limit' => 'Limit', 'ignored' => 'Ignored', |
281 | | - 'added' => 'Added', 'skipped' => 'Skipped', 'overwritten' => 'Overwritten', |
282 | | - 'failed' => 'Failed' ) as $var => $desc ) { |
283 | | - if ( $$var > 0 ) |
284 | | - echo( "{$desc}: {$$var}\n" ); |
| 287 | + if ( $sleep ) { |
| 288 | + sleep( $sleep ); |
285 | 289 | } |
| 290 | + } |
286 | 291 | |
287 | | - } else { |
288 | | - echo( "No suitable files could be found for import.\n" ); |
| 292 | + # Print out some statistics |
| 293 | + echo( "\n" ); |
| 294 | + foreach ( array( 'count' => 'Found', 'limit' => 'Limit', 'ignored' => 'Ignored', |
| 295 | + 'added' => 'Added', 'skipped' => 'Skipped', 'overwritten' => 'Overwritten', |
| 296 | + 'failed' => 'Failed' ) as $var => $desc ) { |
| 297 | + if ( $$var > 0 ) |
| 298 | + echo( "{$desc}: {$$var}\n" ); |
289 | 299 | } |
290 | 300 | |
291 | 301 | } else { |
292 | | - showUsage(); |
| 302 | + echo( "No suitable files could be found for import.\n" ); |
293 | 303 | } |
294 | 304 | |
295 | 305 | exit( 0 ); |