133 $permissionManager = $services->getPermissionManager();
145 $this->
output(
"Importing Files\n\n" );
147 $dir = $this->
getArg( 0 );
151 $this->
fatalError(
"Cannot specify both protect and unprotect. Only 1 is allowed.\n" );
155 $this->
fatalError(
"You must specify a protection option.\n" );
158 # Prepare the list of allowed extensions
159 $extensions = $this->
hasOption(
'extensions' )
160 ? explode(
',', strtolower( $this->
getOption(
'extensions' ) ) )
161 : $this->
getConfig()->get( MainConfigNames::FileExtensions );
163 # Search the path provided for candidates for import
164 $files = $this->findFiles( $dir, $extensions, $this->
hasOption(
'search-recursively' ) );
165 if ( !$files->valid() ) {
166 $this->
output(
"No suitable files could be found for import.\n" );
170 # Initialise the user for this operation
172 ? User::newFromName( $this->
getOption(
'user' ) )
173 : User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
174 if ( !$user instanceof
User ) {
175 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
177 '@phan-var User $user';
178 StubGlobalUser::setUser( $user );
180 # Get block check. If a value is given, this specified how often the check is performed
181 $checkUserBlock = (int)$this->
getOption(
'check-userblock' );
184 $sleep = (int)$this->
getOption(
'sleep' );
185 $limit = (int)$this->
getOption(
'limit' );
186 $timestamp = $this->
getOption(
'timestamp',
false );
188 # Get the upload comment. Provide a default one in case there's no comment given.
189 $commentFile = $this->
getOption(
'comment-file' );
190 if ( $commentFile !==
null ) {
191 $comment = file_get_contents( $commentFile );
192 if ( $comment ===
false || $comment ===
null ) {
193 $this->
fatalError(
"failed to read comment file: {$commentFile}\n" );
196 $comment = $this->
getOption(
'comment',
'Importing file' );
198 $commentExt = $this->
getOption(
'comment-ext' );
199 $summary = $this->
getOption(
'summary',
'' );
200 $license = $this->
getOption(
'license',
'' );
201 $sourceWikiUrl = $this->
getOption(
'source-wiki-url' );
207 # Batch "upload" operation
208 $restrictionStore = $services->getRestrictionStore();
209 foreach ( $files as $file ) {
211 if ( $sleep && ( $processed > 0 ) ) {
215 $base = UtfNormal\Validator::cleanUp(
wfBaseName( $file ) );
218 $title = Title::makeTitleSafe(
NS_FILE, $base );
221 "{$base} could not be imported; a valid title cannot be produced\n"
227 if ( $from !== $title->getDBkey() ) {
228 $statistics[
'ignored']++;
235 if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
236 $user->clearInstanceCache(
'name' );
237 if ( $permissionManager->isBlockedFrom( $user, $title ) ) {
239 "{$user->getName()} is blocked from {$title->getPrefixedText()}! skipping.\n"
241 $statistics[
'skipped']++;
247 $image = $services->getRepoGroup()->getLocalRepo()
249 if ( $image->exists() ) {
251 $this->
output(
"{$base} exists, overwriting..." );
252 $svar =
'overwritten';
254 $this->
output(
"{$base} exists, skipping\n" );
255 $statistics[
'skipped']++;
259 if ( $this->
hasOption(
'skip-dupes' ) ) {
260 $repo = $image->getRepo();
261 # XXX: we end up calculating this again when actually uploading. that sucks.
263 $dupes = $repo->findBySha1( $sha1 );
266 "{$base} already exists as {$dupes[0]->getName()}, skipping\n"
268 $statistics[
'skipped']++;
273 $this->
output(
"Importing {$base}..." );
277 if ( $sourceWikiUrl ) {
279 $real_comment = $this->getFileCommentFromSourceWiki( $sourceWikiUrl, $base );
280 $commentText = $real_comment !==
false ? $real_comment : $comment;
283 $real_user = $this->getFileUserFromSourceWiki( $sourceWikiUrl, $base );
284 if ( $real_user !==
false ) {
285 $realUser = User::newFromName( $real_user );
286 if ( $realUser ===
false ) {
287 # user does not exist in target wiki
289 "failed: user '$real_user' does not exist in target wiki."
293 StubGlobalUser::setUser( $realUser );
298 $commentText =
false;
301 $f = $this->findAuxFile( $file, $commentExt );
303 $this->
output(
" No comment file with extension {$commentExt} found "
304 .
"for {$file}, using default comment." );
306 $commentText = file_get_contents( $f );
307 if ( !$commentText ) {
309 " Failed to load comment file {$f}, using default comment."
315 if ( !$commentText ) {
316 $commentText = $comment;
323 " publishing {$file} by '{$user->getName()}', comment '$commentText'..."
326 $mwProps =
new MWFileProps( $services->getMimeAnalyzer() );
327 $props = $mwProps->getPropsFromPath( $file,
true );
329 $publishOptions = [];
330 $handler = MediaHandler::getHandler( $props[
'mime'] );
332 $publishOptions[
'headers'] = $handler->getContentHeaders( $props[
'metadata'] );
334 $publishOptions[
'headers'] = [];
336 $archive = $image->publish( $file, $flags, $publishOptions );
337 if ( !$archive->isGood() ) {
338 $this->
output(
"failed. (" .
339 $archive->getMessage(
false,
false,
'en' )->text() .
341 $statistics[
'failed']++;
346 $commentText = SpecialUpload::getInitialPageText( $commentText, $license );
348 $summary = $commentText;
352 $this->
output(
"done.\n" );
353 } elseif ( $image->recordUpload3(
364 $this->
output(
"done.\n" );
368 $protectLevel = $this->
getOption(
'protect' );
369 $restrictionLevels = $this->
getConfig()->get( MainConfigNames::RestrictionLevels );
371 if ( $protectLevel && in_array( $protectLevel, $restrictionLevels ) ) {
381 $this->
output(
"\nWaiting for replica DBs...\n" );
383 sleep( 2 ); # Why
this sleep?
386 $this->
output(
"\nSetting image restrictions ..." );
390 foreach ( $restrictionStore->listApplicableRestrictionTypes( $title ) as $type ) {
391 $restrictions[$type] = $protectLevel;
394 $page = $services->getWikiPageFactory()->newFromTitle( $title );
395 $status = $page->doUpdateRestrictions( $restrictions, [], $cascade,
'', $user );
396 $this->
output( ( $status->isOK() ?
'done' :
'failed' ) .
"\n" );
399 $this->
output(
"failed. (at recordUpload stage)\n" );
403 $statistics[$svar]++;
406 if ( $limit && $processed >= $limit ) {
411 # Print out some statistics
413 foreach ( array_merge(
419 ) as $desc => $number ) {
421 $this->
output( ucfirst( $desc ) .
": $number\n" );