r100267 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100266‎ | r100267 | r100268 >
Date:20:08, 19 October 2011
Author:reedy
Status:ok
Tags:
Comment:
Add some braces

Unindent most of the code
Modified paths:
  • /trunk/phase3/maintenance/importImages.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/importImages.php
@@ -40,255 +40,265 @@
4141 echo( "Import Images\n\n" );
4242
4343 # Need a path
44 -if ( count( $args ) > 0 ) {
 44+if ( count( $args ) == 0 ) {
 45+ showUsage();
 46+}
4547
46 - $dir = $args[0];
 48+$dir = $args[0];
4749
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+}
5154
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+}
5458
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;
6064
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 );
6367
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;
7176
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;
7681 } else {
77 - $checkUserBlock = false;
 82+ $checkUserBlock = (int)$options['check-userblock'];
7883 }
 84+} else {
 85+ $checkUserBlock = false;
 86+}
7987
80 - # Get --from
81 - $from = @$options['from'];
 88+# Get --from
 89+$from = @$options['from'];
8290
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+}
8696
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+}
90102
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';
93105
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" );
99110 }
100 - elseif ( isset( $options['comment'] ) ) {
101 - $comment = $options['comment'];
102 - }
 111+} elseif ( isset( $options['comment'] ) ) {
 112+ $comment = $options['comment'];
 113+}
103114
104 - $commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
 115+$commentExt = isset( $options['comment-ext'] ) ? $options['comment-ext'] : false;
105116
106 - # Get the license specifier
107 - $license = isset( $options['license'] ) ? $options['license'] : '';
 117+# Get the license specifier
 118+$license = isset( $options['license'] ) ? $options['license'] : '';
108119
109 - # Batch "upload" operation
110 - if ( ( $count = count( $files ) ) > 0 ) {
 120+# Batch "upload" operation
 121+$count = count( $files );
 122+if ( $count > 0 ) {
111123
112 - foreach ( $files as $file ) {
113 - $base = wfBaseName( $file );
 124+ foreach ( $files as $file ) {
 125+ $base = wfBaseName( $file );
114126
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++;
119139 continue;
120140 }
 141+ }
121142
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;
129148 }
 149+ }
130150
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;
137161 }
 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.
138166
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" );
147171 $skipped++;
148172 continue;
149173 }
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+ }
154175
155 - $dupes = $repo->findBySha1( $sha1 );
 176+ echo( "Importing {$base}..." );
 177+ $svar = 'added';
 178+ }
156179
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;
162198 }
163 -
164 - echo( "Importing {$base}..." );
165 - $svar = 'added';
166199 }
 200+ } else {
 201+ # Find comment text
 202+ $commentText = false;
167203
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. " );
180208 } 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 );
194210 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. " );
201212 }
202213 }
 214+ }
203215
204 - if ( !$commentText ) {
205 - $commentText = $comment;
206 - }
 216+ if ( !$commentText ) {
 217+ $commentText = $comment;
207218 }
 219+ }
208220
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;
222232 }
 233+ }
223234
224 - $doProtect = false;
225 - $restrictions = array();
 235+ $doProtect = false;
 236+ $restrictions = array();
226237
227 - global $wgRestrictionLevels;
 238+ global $wgRestrictionLevels;
228239
229 - $protectLevel = isset( $options['protect'] ) ? $options['protect'] : null;
 240+ $protectLevel = isset( $options['protect'] ) ? $options['protect'] : null;
230241
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+ }
241252
242253
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();
255266
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+ }
266273 }
267274
268 - $$svar++;
269 - $processed++;
 275+ } else {
 276+ echo( "failed. (at recordUpload stage)\n" );
 277+ $svar = 'failed';
 278+ }
270279
271 - if ( $limit && $processed >= $limit )
272 - break;
 280+ $$svar++;
 281+ $processed++;
273282
274 - if ( $sleep )
275 - sleep( $sleep );
 283+ if ( $limit && $processed >= $limit ) {
 284+ break;
276285 }
277286
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 );
285289 }
 290+ }
286291
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" );
289299 }
290300
291301 } else {
292 - showUsage();
 302+ echo( "No suitable files could be found for import.\n" );
293303 }
294304
295305 exit( 0 );

Status & tagging log